通过ID获取消息时Discord.js awaitReactions不起作用

时间:2020-11-01 18:24:21

标签: javascript discord.js

我的机器人很久以前发送的消息中的“ message.awaitReactions”有问题。

这是代码:

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");
        });
    });

问题在于,当用户在邮件中添加响应并且我不知道为什么:(

有人知道为什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

message.awaitReactions(filter, { time: 15000 })将等待15秒以做出反应,然后输入.then(). 您没有指定任何内容,因此它将永远不会输入.then()

您应该使用的是:

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);
});

或者使用事件messageReactionAdd,您仍需要获取该事件触发的消息。

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>');
}