Reaction in a order discord js

5.8k views Asked by At

i want to add a message with reactions but i want reaction to have certain order. The problem is when i execute command to add that message, the reactions add randomly.

Code:

        case "helptest":
 indexTest = 0;
 comandUser = message.author.username;
 message.channel.send(mesajeTest[indexTest]).then(function (message) {
  message.react("⏪")
  message.react("▶")
  message.react("◀")
  message.react("⏩")
 });
 break;

I want that order which is in code to be in reactions message, any solution ?

3

There are 3 answers

0
Gradin98 On BEST ANSWER

Solved the problem with async function

  case "helptest":
 indexTest = 0;
 comandUser = message.author.username;
 message.channel.send(mesajeTest[indexTest]).then(async function (message) {
  await message.react("⏪")
  await message.react("◀")
  await message.react("▶")
  await message.react("⏩")
 });
 break;

0
ledicjp On

What exactly does your send function return? A Promise of what? Maybe multiple Promises get resolved before the callback of the first one is finished.

Did you try using Promise.each()?

With Promise.each() Iteration happens serially. If the iterator function returns a promise or a thenable, then the result of the promise is awaited before continuing with next iteration.

0
Olian04 On

As OP states, the accepted answer works for this specific case, however i just ran into a similar problem, however my emoji:s are generated, so typing them out on a line isn't an option. So i naturally tried to look over them all and await each in time. However node seems to smart for this and optimized the code to run in parallel instead of sequentially. I ended up having to fold all the promises into a single promise using reduce like this:

[...array of emojis...].reduce((promise, emoji) => promise.then(() => message.react(emoji)), Promise.resolve());

Even though there's an accepted answer I'm going to leave this answer here in hopes that others might find it helpful.