【问题标题】:Discord Bot, Discord.js | Get bot to send message after clicking reactionDiscord Bot,Discord.js |点击反应后让机器人发送消息
【发布时间】:2020-04-29 14:55:53
【问题描述】:

我正在尝试让我的第一个不和谐机器人在点击反应后发送消息,

一个表示是的反应,一个表示否的反应

我的代码已经发送了一个带有反应的嵌入

我创建了一个反应收集器,但是 现在唯一的事情是它立即反应(反应没有)两次,甚至在我点击反应之前

非常感谢您的帮助!

到目前为止我的代码:

const {Client, RichEmbed } = require('discord.js');
const client = new Client();
const a =
client.once('ready', () => {
    console.log('boogie time!');
});
client.on('message', message => {
    if(message.author.bot)
    {
        if(message.embeds)
        {
            const embedMsg = message.embeds.find(msg=> msg.title ==='Boogie Time!?');
            if(embedMsg)
            {
                embedMsg.message.react('✅')
                .then(reaction => reaction.message.react('❌'))

                // This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌') && user.id === message.author.id;

// Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
const collector = embedMsg.message.createReactionCollector(filter, {time: 10000});

// This event is emitted when a reaction passes through the filter
collector.on('collect', r => r.name === '✅' ? 
console.log('Reacted Yes') : console.log('Reacted No'));



            }
        }
        return;
    }


if(message.content.toLowerCase() === 'boogie')
{
    const embed = new RichEmbed();
    embed.setTitle("Boogie Time!?")
    embed.setColor("GREEN")
    embed.setDescription("Are you sure?")
    message.channel.send(embed);



};
});

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    这里有几个问题。第一个问题是您只查看机器人 if(message.author.bot) 发送的消息,然后尝试由该消息作者过滤,该作者始终是机器人,而不是您或其他任何人 user.id === message.author.id。我认为您的意图可能是收集机器人反应。

    您遇到的第二个问题是异步执行导致收集器在机器人添加初始反应之前被创建。

    embedMsg.message.react('✅')
       .then(reaction => reaction.message.react('❌'))
    

    在调用.react 之后,它下面的代码在反应完成之前立即开始异步执行。如果您没有听到机器人的反应,这应该不是问题,但是如果您将收集器创建在第二个 .then 语句中,它将确保在第二个反应完成并且您赢之前它不会创建它'不需要过滤 user.id,因为机器人不应该在那之后做出反应,从而消除了这两个问题。

    所以问题的原因是机器人正在收集它自己的两个反应。那为什么总是说'React No'呢? 这是第三期:

    collector.on('collect', r => r.name === '✅' ? 
    console.log('Reacted Yes') : console.log('Reacted No'));
    

    在这里,您忘记了调出反应表情符号。这一行应该是:

    collector.on('collect', r => r.emoji.name === '✅' ? 
    console.log('Reacted Yes') : console.log('Reacted No'));
    

    总之,这应该是上面描述的变化:

    if(embedMsg)
    {
        embedMsg.message.react('✅')
        .then(reaction => reaction.message.react('❌')
            .then(() => {
                // This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
                const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌');
    
                // Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
                const collector = embedMsg.message.createReactionCollector(filter, {time: 10000});
    
                // This event is emitted when a reaction passes through the filter
                collector.on('collect', r => r.emoji.name === '✅' ? 
                console.log('Reacted Yes') : console.log('Reacted No'));
        }));
    }
    

    【讨论】:

    • 这有很大帮助,事情进展顺利,只剩下一个问题,机器人需要一些时间来添加这两个反应,所以一旦它添加了 X 反应,它开始读取速度过快并读取机器人添加X 并回应(回应否)
    • @AbandonedAccount 检查你的括号,我注意到我的一个丢失了,我修复了它。第二个 .then 语句应该是 X 反应的方法,而不是检查反应。我在示例中添加了一些缩进,可能会使其更清晰。
    • 那行得通,谢谢,如果我想更改console.log(反应是/否),我可以将其更改为message.channel.send()吗?还是我需要创建其他东西??
    • 把它改成 message.channel.send 让它回到同样的问题,它对自己的反应反应很快,我非常感谢你帮助我解决这个问题,很抱歉有这么多问题,我自己努力做到这一点,但似乎无法做到 100%
    • 也许我可以延迟一下?但我不确定是否可以使用所使用的命令类型?
    猜你喜欢
    • 2021-07-06
    • 2021-06-05
    • 2021-09-17
    • 2021-08-19
    • 2020-12-03
    • 2021-01-08
    • 2021-04-19
    • 2018-05-17
    • 2022-01-23
    相关资源
    最近更新 更多