繁体   English   中英

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

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

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

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

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

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

非常感谢帮助!

到目前为止我的代码:

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



};
});

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

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

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

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

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

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

暂无
暂无

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

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