提问者:小点点

Discord Bot,Discord.js获得Bot在点击反应后发送消息


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

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

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

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

非常感谢您的帮助!

到目前为止我的代码:

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个答案

匿名用户

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

第二个问题是异步执行导致在bot添加初始反应之前创建收集器。

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

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

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

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