简体   繁体   中英

Discord.js awaitReactions on message fetch by id not working

I have a problem with "message.awaitReactions" from a message that was send by my bot a long ago.

Here's the code :

let channel = client.guilds.cache
    .get("<guild_id>")
    .channels.cache.get("<channel_id>");

  channel.messages
    .fetch(<message_id_that_was_sent_a_long_ago_by_my_bot>)
    .then((message) => {
      message
        .awaitReactions(
          (reaction, user) =>
            reaction.emoji.name == "👍" || reaction.emoji.name == "👎"
        )
        .then((collected) => {
          console.log("collected", collected);
          if (collected.first().emoji.name == "👍") {
            user.setNickname("🏡 " + user.username);
          } else {
          //TODO
          }
          // reaction.remove(user);
        })
        .catch(() => {
          message.reply("No reaction, operation canceled");
        });
    });

The problem is that nothing append when the user add reactions on the message and I don't know why :(

Anybody knows why ?

Thanks

message.awaitReactions(filter, { time: 15000 }) would wait 15s for reactions, then enter the .then(). You have nothing specified, so it will never enter the .then() .

What you should use instead is:

const filter = (reaction, user) => reaction.emoji.name == "👍" || reaction.emoji.name == "👎"
const collector = message.createReactionCollector(filter);
collector.on('collect', (messageReaction, user) => { 
    console.log(`Collected ${messageReaction.emoji.name}`) 
    if (r.emoji.name == "👍") {
        messageReaction.message.guild.members.fetch(user.id).then(member => member.setNickname("🏡 " + user.username));
    } else {
        // TODO
    }
    // reaction.remove(user);
});

Or use the event messageReactionAdd, you would still need to fetch the message for this event to fire.

client.on('messageReactionAdd', (messageReaction, user) => {
     if(messageReaction.message.id != '<message_id_that_was_sent_a_long_ago_by_my_bot>') return;
     if (messageReaction.emoji.name == "👍") {
         messageReaction.message.guild.members.fetch(user.id).then(member => member.setNickname("🏡 " + user.username));
     }
     // reaction.remove(user);
})
client.on('ready', () => {
     let channel = client.guilds.cache
         .get("<guild_id>")
         .channels.cache.get("<channel_id>");
     channel.messages.fetch('<message_id_that_was_sent_a_long_ago_by_my_bot>');
}

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