簡體   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