Bot不等待反应或消息

时间:2020-05-01 15:09:28

标签: javascript async-await bots discord.js message

因此,如您所见,这是一个带有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 个答案:

答案 0 :(得分: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
            })
        })
    })
})

这样,它仅在完成最后一个代码后才运行下一部分代码,从而避免收集自己的动作。