Messenger bot does not respect the message order - bots

I'm having an issue with Messenger dev platform and guessed I could ask you guys for some help.
I would like my bot to send multiple messages in a row, for exemple :
sendTextMessage(user, "Hello");
sendTextMessage(user, "Goodbye");
But since Node is asynchrone, the order is not always respected and sometimes 'Goodbye' ends up showing before 'Hello'.
What is the best solution to remedy this issue ?
Thanks for your help!

You can do this in NodeJS by using either callbacks or promises. If you are not familiar with either of them, you can read callbacks and promises.
For further more steps, please check this out:
https://stackoverflow.com/a/47497119/6874563
I used above and get solved.

Related

Adding integers with the eval function is returning an error. (Nodejs)

Please don't ask me not to use eval, this isn't going to be public anyways.
I've made a chatting website, and I have implemented a "!eval" command (admin only), whatever is after it is run. I can use "!eval '2'+'2'" (Strings added), but not "!eval 2+2." The error returned is .
I've console.logged the input to the eval, and it returned exactly what I wanted: "1+1." I've looked around for this, but it seems like no one else had this problem before. A solution (more like a debugging one) is that I tried "eval('1+1')" and returned the same error. Any thoughts? Thanks in advance.
(I forgot to add what I was expecting)
I was expecting this.
VLAZ pointed out in the comments that it must be another piece of code, which he is correct. I was encrypting the messages so it can be sent securely to the client, and it only takes a string. I added
if (typeof(results) != 'string'){
results = String(results)
}
and it seemed to work, Thanks!

Make JDA Discord Bot write a message with a delay

I'm currently trying to make my JDA Bot write a message if it leaves a voice channel. Then 3 seconds after the message is sent it should follow with another message.
I've already tried to do it with the RestAction but I could only figure out how to delete a message with delay.
The code would look something like this :
channel.sendMessage("You told me to leave, so I left"); //I shortened the message command here
//Then with a delay of 3 seconds it should do this
channel.sendMessage("That was mean");
I hope someone will be able to help me out here. Any help is greatly appreciated!
Every RestAction has a queueAfter(long, TimeUnit) which can be used to delay it:
channel.sendMessage("Hello").queueAfter(10, TimeUnit.SECONDS);

How to check if AXIOS POST request is sent?

I can't seem to figure out why my console.log(); isn't going off. It seems that everything after the await axios.post(); method doesn't go off? Even my handleClearData(); function isn't going off.
I'm using React with Axios for my contact form to send data to a NodeJS server to then email the data to my email.
Could anyone please explain to me or point me in the right direction on solving this problem? Also, the console.log(res); obviously doesn't return or log anything as well.
Thanks a ton!
UPDATED:
Here's more info. I even took out my function thinking it may have been the problem. It wasn't...
SECOND UPDATE:
Another update!
You'll want to put the console log under handleClearData(); within the (first) try block.
I managed to fix it because await method was waiting for a response from the server. So, on my backend I put this simple line of code, res.send() which then allowed the await function to get out of its infinite loop; therefore, executing the rest of the code that came after it.
Thanks to all that tried to help me! =)
&& happy coding!

How to mention the author of a message in another message

I am using repl.it to develop a bot. I am trying to make a command that makes the bot behave like this:
Someone: !slap #someoneelse
Bot: #Someone slapped #someoneelse
How can I get the bot to mention #someone without using ID? Multiple people will use the command and I can't just use ID since it will only work with one person. I haven't found anything that helped me, and the documentation was no help either. Hopefully, I can get help! Thank you.
Users and members have a .toString() method that is automatically called every time they are concatenated with a string: that means that if you type "Hey " + message.author you will get "Hey #author"
That's how I would do the command:
// First store the mentioned user (it will be undefiend if there's none, check that)
let mentionedUser = message.mentions.users.first();
// Reply by directly putting the User objects in the string:
message.channel.send(`${message.author} slapped ${mentionedUser}`);

Nodejs: Do additional stuff after res.send

I'm using Node as webserver and I want to log every request to it into a database. I also want the user to receive the response as quickly as possible, so I came up with this code:
// ... putting together the response_data
res.send(response_data);
// ... now log the request into the DB and maybe do additional stuff
It works and I like the idea of putting some of the (time) expensive stuff behind the send. But as I'm new to Node I'm asking if this is a common pattern?
On Stackoverflow I just find people having problems bc they try to send additional data after res.send - but I never heard anybody saying "yeah this is a great feature for your responsiveness" so I'm not sure if there's a major flaw with this solution I just don't see yet...
As long as you don't need to send anything back to the user as a result of the "additional" stuff then your approach is fine.
The problem most people come across is trying to send data down the response after the response has already been sent e.g.
res.send(response_data);
// do additional stuff
res.send(additional_data); // KABOOM!

Resources