如何添加对机器人消息的反应

时间:2021-06-30 17:00:16

标签: javascript visual-studio-code discord discord.js

所以我想在机器人的消息中添加表情符号反应。但不知道用什么代码来制作它。

我只知道对命令消息做出反应的代码。

else if(command === "guessage"){
    message.channel.send({ embed: {
      color: 16758465,
      title: "Are you...",
      description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
      }
    })
    message.react('?').then(() => message.react('?'));

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

message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '?') {
            message.reply('you reacted with a thumbs up.');
        } else {
            message.reply('you reacted with a thumbs down.');
        }
    })
    .catch(collected => {
        message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
    });
  }

2 个答案:

答案 0 :(得分:1)

处理每个Message#reply()的承诺

使用回调的示例:

message.reply('you reacted with a thumbs up.').then(botsMessage => botsMessage.react('EMOJI-HERE'));

使用异步/等待的示例 (推荐维持反应秩序):

// Inside an async function
const botsMessage = await message.reply('you reacted with a thumbs up.');

await botMessage.react('EMOJI-1');
await botMessage.react('EMOJI-2');
await botMessage.react('EMOJI-3');

Understanding Promises - Discord.JS

答案 1 :(得分:0)

您需要等待消息的发送并使用它的消息对象。

例如:

else if (command === "guessage") {
    (async () => {
            let bmsg = await message.channel.send({
                embed: {
                    color: 16758465,
                    title: "Are you...",
                    description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
                }
            })
            await bmsg.react('?');
            await bmsg.react('?');

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

            bmsg.awaitReactions(filter, {
                    max: 1,
                    time: 60000,
                    errors: ['time']
                })
                .then(collected => {
                    const reaction = collected.first();

                    if (reaction.emoji.name === '?') {
                        message.reply('you reacted with a thumbs up.');
                    } else {
                        message.reply('you reacted with a thumbs down.');
                    }
                })
                .catch(collected => {
                    message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
                });
        })();
    }

我正在使用异步 IIFE 来允许使用 await。还有其他地方应该使用 await,但我会留给你。

相关问题