Discord Bot,Discord.js |单击反应后让机器人发送消息

时间:2020-01-12 19:28:38

标签: javascript discord discord.js

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

一个是肯定的反应,一个不是肯定的

我的代码已经发送了包含响应的嵌入

我创建了一个反应收集器,但是 现在唯一的事情是即使在我单击反应之前,它也会立即与两次反应(反应为否)

非常感谢您的帮助!

到目前为止,我的代码:

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);



};
});

1 个答案:

答案 0 :(得分: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'));
    }));
}