简体   繁体   中英

DISCORD.JS: Message.awaitReactions not firing

When I run this code sendReact() executes but nothing else—no errors. The.catch() function is the only thing that executes after 5 seconds. When I react to my embed that I created with sendReact(), nothing happens. This happens in my command file as well as my index.js file if I moved the code there. Nothing is being logged to the console.

  var pick = Math.floor(Math.random() * 3) + 1; // 1-rock 2-paper 3-scissors
  let playerPick = 0;

  sendReact() // sends the embed message with the reactions

  message.awaitReactions((reaction, user) => user.id == message.author.id && (reaction.emoji.name == '🌑' || reaction.emoji.name == '📄' || reaction.emoji.name == '✂️'),
  { max: 1, time: 5000 }).then(collected => {

    if (collected.first().emoji.name == '🌑') {
      console.log('rock');
      playerPick = 1;
    } else if (collected.first().emoji.name == '📄') {
      console.log('paper');
      playerPick = 2;
    } else if (collected.first().emoji.name == '✂️') {
      console.log('scissors');
      playerPick = 3;
    }

    console.log("Player pick: " + playerPick);
    console.log("CPU pick: " + pick);

  }).catch(() => {
    message.reply('No reaction after (how many) seconds, game cancelled.');
  });

You're creating an embed with sendReact() , but then you call message.awaitReactions(...) . Which message is that referring to? Most likely you're waiting for reactions on the message that triggered this command, instead of on the message with the embed you sent with sendReact() . The only way you can tell the bot to wait for reactions on the new message is to get a reference to it, and call awaitReactions() on that reference.

So, for example:

//in an async function...
let gameMessage = await sendReact();

gameMessage.awaitReactions(...).then(collected => {
  //handle reaction
});

The reason to this is because you're awaiting reactions on your own message .

You need to make awaitReactions() listen to reactions on the embed you're sending.

const msg = await sendReact()
msg.awaitReactions((reaction, user) => user.id == message.author.id && (reaction.emoji.name == '🌑' || reaction.emoji.name == '📄' || reaction.emoji.name == '✂️'),
  { max: 1, time: 5000 }).then(collected => { })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM