如果该反应是特定反应,我该如何使我的机器人检测到反应,然后发送消息?

时间:2020-03-31 10:26:36

标签: node.js bots discord discord.js

我正在创建一个具有反应的帮助命令,该机器人将添加一个反应,然后该用户将做出反应,并且该机器人将发布相应的帮助消息。我很困惑为什么我的代码没有抛出错误而无法正常工作。

const filter = (reaction, user) => {
    return [':zany_face:'].includes(reaction.emoji.name) && user.id === message.author.id;
};

message.awaitReactions(filter, { max: 1, time: 5000, errors: ['time'] })
    .then(collected => {
        const reaction = collected.first();
        if (reaction.emoji.name === ':zany_face:') {
            message.reply('test.');
        }
    })
    .catch(collected => {
        message.reply('You didn\'t react in time');
    });

收到任何帮助,谢谢:)

Ps:我在嵌入的后面使用它,所以我希望嵌入对它有反应,尽管我做了一点,只是不确定如何在嵌入和awaitreaction之间有效地链接代码

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

const filter = (reaction, user) => !user.bot && user.id ==usr.id; //makes sure only the message author can react

let msg = message.channel.send("React to me!")

//creates a collector on the message for 5 seconds
let collector = msg.createReactionCollector(filter, { time: 5000 });
//opens the reaction collector
collector.on('collect', async (reaction, collector) => {
    //checks which reaction was given
    const chosen = reaction.emoji.name;

    if(chosen == "EMOJIHERE"){
        //removes the user's reaction
        msg.reactions.get("EMOJIHERE").remove(message.author.id).catch(allerrors)
        //sends a message back
        message.channel.send("This is an answer.")
    }
})