机器人不等待反应或消息

Posted

技术标签:

【中文标题】机器人不等待反应或消息【英文标题】:Bot doesn't wait for reaction or for message 【发布时间】:2020-08-16 02:24:58 【问题描述】:

如您所见,这是一个带有 2 个选项的建议命令,但是当我输入 .sug 时,机器人会发送嵌入内容,这很好,但随后他会立即发送 A 表情符号的消息,然后立即发送“您的建议我什至没有做任何事情。也没有错误。我认为机器人在某种程度上将他自己对自己的消息的反应作为用户的反应,然后他认为由于他自己的反应(可能)而发送的“请为...建议一些东西”是来自的建议消息用户,所以他发送“您的建议已被填充到材料团队”这是一个屏幕截图的链接:https://ibb.co/KwMBmtc

execute(message, client, args) 
        const Discord = require('discord.js');

        let Embed = new Discord.MessageEmbed()
            .setColor('0x0099ff')
            .setDescription(`Suggestion categories`)
            .addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)

        message.channel.send(Embed)
        .then(function (message) 
            message.react("????")
            message.react("????")
            const filter = (reaction, user) => 
                return ['????', '????'].includes(reaction.emoji.name) && user.id;
        ;

        message.awaitReactions(filter,  max: 1 )
                .then(collected => 
                    const reaction = collected.first();

                    if (reaction.emoji.name === '????') 
                        const filter = m => m.author.id === message.author.id;

                        message.channel.send(`Please provide a suggestion for the Website/Servers/Discord Server or cancel this command with "cancel"!`)

                        message.channel.awaitMessages(filter,  max: 1, )
                            .then(async (collected) => 
                                if (collected.first().content.toLowerCase() === 'cancel') 
                                    message.reply("Your suggestion has been cancelled.")
                                
                                else 
                                    let embed1 = new Discord.MessageEmbed()
                                        .setColor('0x0099ff')
                                        .setAuthor(`$message.author.tag`)
                                        .addField(`New Suggestion:`, `$collected.first().content`)
                                        .setFooter(client.user.username, "attachment://CloudX.png")
                                        .setTimestamp();

                                    const channel = await client.channels.fetch("705781201469964308")
                                    channel.send(embed: embed1, files: [
                                        attachment:'CloudX.png',
                                        name:'CloudX.png'
                                        ])

                                    message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
                                 
                        )
                    
                    if (reaction.emoji.name === '????') 
                        const filter = m => m.author.id === message.author.id;

                        message.channel.send(`Please provide a suggestion for the CloudX Bot or cancel this command with "cancel"!`)

                        message.channel.awaitMessages(filter,  max: 1, )
                            .then(async (collected) => 
                                if (collected.first().content.toLowerCase() === 'cancel') 
                                    message.reply("Your suggestion has been cancelled.")
                                
                                else 
                                    let embed2 = new Discord.MessageEmbed()
                                        .setColor('0x0099ff')
                                        .setAuthor(`$message.author.tag`)
                                        .addField(`New Suggestion:`, `$collected.first().content`)
                                        .setFooter(client.user.username, "attachment://CloudX.png")
                                        .setTimestamp();

                                    const channel = await client.channels.fetch("702825446248808519")
                                    channel.send(embed: embed2, files: [
                                        attachment:'CloudX.png',
                                        name:'CloudX.png'
                                        ])

                                    message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
                                 
                        )
                        
                )
        )
    ,

【问题讨论】:

【参考方案1】:

我认为机器人在某种程度上将自己对自己消息的反应作为用户的反应

你完全正确。该机器人正在收集自己的消息和反应,因为您在发送它们后没有使用.then,所以awaitMessagesawaitReactions 大致在您发送消息的同时被调用。

要解决此问题,您需要为机器人执行的每个操作(发送消息、响应)使用 .then

message.channel.send(Embed).then((message) => 
    message.react("?").then(() => 
        message.react("?").then(() => 
            message.awaitReactions(filter,  max: 1 ).then(collected => 
                // Do stuff with the collected reaction
            )
        )
    )
)

这样,它只在最后一段代码完成后才运行下一段代码,避免收集自己的动作。

【讨论】:

谢谢,但我知道这听起来很愚蠢,但你知道我必须在哪里放 .then 因为他现在会立即发送 B 消息,但我不能在 if 之前放 .then 跨度> 或者在if之后,...我认为

以上是关于机器人不等待反应或消息的主要内容,如果未能解决你的问题,请参考以下文章

Discord bot 添加并等待问答游戏的反应

如何让 Discord 机器人异步等待对多条消息的反应?

Bot 不等待反应,并且由于 Emded 错误而在收集时不发送消息

Discord.JS 如何等待会员反应

Discord 机器人消息不收集反应

Discord bot 查找来自用户的消息