简体   繁体   English

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

[英]Discord Bot, Discord.js | Get bot to send message after clicking reaction

I'm trying to get my first discord bot to send a message after clicking a reaction,我正在尝试让我的第一个不和谐机器人在点击反应后发送消息,

One for yes reaction, one for no一个表示是反应,一个表示否

I have my code already send an embed with the reactions我的代码已经发送了一个带有反应的嵌入

I created a Reaction collector but the only thing now its that it instantly reacts with (reacted no) twice, even before I click the reaction我创建了一个反应收集器,但现在唯一的事情是它会立即与(没有反应)反应两次,甚至在我点击反应之前

Help is greatly appreciated!非常感谢帮助!

My Code so far:到目前为止我的代码:

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



};
});

You have a several problems here.你在这里有几个问题。 The first issue is you are only looking at messages sent by a bot if(message.author.bot) and then later trying to filter by that message author which will always be a bot, not you or anyone else user.id === message.author.id .第一个问题是您只查看机器人发送的消息if(message.author.bot)然后稍后尝试通过该消息作者进行过滤,该作者将始终是机器人,而不是您或其他任何人user.id === message.author.id .作者user.id === message.author.id I think your intention may have been to not collect bot reactions.我认为您的意图可能是收集机器人反应。

The second issue you have is that the asynchronous execution is causing the collector to get created before the bot adds the initial reaction.您遇到的第二个问题是异步执行导致收集器在机器人添加初始反应之前被创建。

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

After this call to .react , the code below it starts immediate async execution, before the reactions complete.在调用.react ,它下面的代码在反应完成之前立即开始异步执行。 If you aren't listening to the bots reaction this shouldn't be an issue, but if you enclose the collector creating in a second .then statement it will ensure it doesn't create it until the second reaction is complete and you won't need to filter the user.id because the bot shouldn't react after that, thus eliminating both problems.如果您没有听机器人的反应,这应该不是问题,但是如果您将创建的收集器包含在第二个.then语句中,它将确保它在第二个反应完成之前不会创建它,并且您不会“不需要过滤 user.id 因为机器人不应该在那之后做出反应,从而消除这两个问题。

So the cause of the problem is the bot is collecting it's own two reactions.所以问题的原因是机器人正在收集它自己的两个反应。 Why is it always saying 'React No' then?为什么它总是说“不反应”呢? This is the third issue:这是第三个问题:

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

Here you have forgotten to call out the reactions emoji.在这里,您忘记了呼出反应表情符号。 This line should be:这一行应该是:

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

In conclusion, this should be the changes described above:总之,这应该是上面描述的变化:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM