【问题标题】:how do i make the bot do something after i react to its message?在我对它的消息做出反应后,如何让机器人做某事?
【发布时间】:2020-12-13 20:11:03
【问题描述】:

我试图让机器人等待用户的反应,但我不知道如何设置它。我已经尝试过了,我也将const filter 放在了第一部分,但是当它以 1️⃣ 对消息做出反应时,机器人会说回复。

message.channel.send(`the current number is:  ${number}`).then(message => {
    message.react('1️⃣')
    message.react('2️⃣')
    message.react('3️⃣')
});

const filter = (reaction, user) => {
    return ['1️⃣', '2️⃣','3️⃣'].includes(reaction.emoji.name)
        && user.id === message.author.id;
}

message.awaitReactions(filter, { max: 1, time: 20000, errors: ['time'] })
    .then(collected => {
        const reaction = collected.first();
        if (reaction.emoji.name === '1️⃣') {
            message.reply('you reacted with 1');
        } else if (reaction.emoji.name === '2️⃣') {
            message.reply('you reacted with 2');
        } else {
            message.reply('you reacted with 3');
        }
    })
    .catch(collected => {
        message.reply('game has ended');
    });

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    我看不到表情符号,但我会告诉你我是怎么做的(注意:我使用的是异步函数,所以我不必使用大量回调,我个人更喜欢它而不是 @987654321 @链接):

    const msg = await message.channel.send(`the current number is:  ${number}`);
    await Promise.all([
        message.react('1️⃣'),
        message.react('2️⃣'),
        message.react('3️⃣')
    ]);
    
    const filter = (reaction, user) => ['1️⃣', '2️⃣','3️⃣'].includes(reaction.emoji.name) && user.id === message.author.id;
    const collector = msg.createReactionCollector(filter, { time: 120000, max: 1 }); //setup here
    
    collector.on('collect', async () => {
        //... your logic after reaction here
    });
    

    【讨论】:

      【解决方案2】:

      Discord.js 客户端对象具有 messageReactionAdd(以及 Remove、RemoveAll 以及当机器人删除表情符号反应时)事件触发器,这为您提供了一个相当简单的解决方案。

      let targetUser = "some-long-number"; 
      // replace this with the author of the person who fires your 
      // reaction related command in your client.on('message') def.
      client.on('messageReactionAdd', (reaction, user)) {
        console.log('sample message to verify base functionality');
        if (user != targetUser) return null;
        switch (reaction.emoji.name) {
          case "1️⃣":
            message.reply("you reacted with 1️⃣");
            break;
          case "2️⃣":
            message.reply("you reacted with 2️⃣");
            break;
          case "3️⃣":
            message.reply("you reacted with 3️⃣");
            break;
          default:
            // in case you want to log when they don't reply with the correct emoji.
            break;
        }
      }
      

      我可能在某个地方打错了,请随时纠正我。 如果message.reply()有效,我的第一个建议是将用户命令的通道ID记录在全局变量中(例如channel-id),并使用channel-id.send(content);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 2020-12-14
        • 2020-11-07
        • 2021-04-11
        • 1970-01-01
        • 2020-01-22
        • 2020-11-21
        相关资源
        最近更新 更多