【问题标题】:How to add a reaction to bot's message如何添加对机器人消息的反应
【发布时间】:2021-09-12 20:31:05
【问题描述】:

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

我只知道响应命令消息的代码。

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

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 不要使用 .then()。等待显然更干净。这将使意大利面条代码具有类似 6 层的嵌套承诺处理程序。
    • .then() 只是处理承诺的同步方式。我的回答指出“处理每个 Message#reply() 的承诺”,将 OP 指向正确的方向。我只是提供了一个 Example 并没有直接说明只使用 .then(),因为您的答案已经显示为 async/await。即使我提供了正确的解决方案,你却否决了我的回答是不公平的。请阅读Privileges - When To Downvote
    • 此外,当回调都适合自己的一行时,回调地狱不会成为问题
    • 我现在理解并且会删除反对票,但它已被锁定(也许稍微编辑您的答案?)。如果 API 延迟或无法添加反应,您不想等到反应添加完毕后再添加处理程序吗?
    • 已编辑答案以包含两种方法
    【解决方案2】:

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

    例如:

    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,但我会留给你。

    【讨论】:

    • 我试过了,但它不起作用,我没有看到我在代码中犯的错误。想知道为什么
    • 好吧,我们需要错误之类的吗?运行 message.reply() 创建一个 new 消息对象,您的代码似乎使用原始代码。你确定这个函数和它的所有调用栈都是异步的吗?
    • 错误是TypeError:guessage.react is not a function
    • 我做了一个名为guessage的函数
    • 我会用有效的答案编辑我的答案
    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 2022-01-05
    • 2022-01-05
    • 1970-01-01
    • 2020-02-23
    • 2020-07-28
    • 2020-03-13
    • 2020-01-11
    相关资源
    最近更新 更多