如何等待反应,然后才发送消息[discord.js]

时间:2020-08-13 16:12:44

标签: javascript discord discord.js

我有一条消息,我的机器人将其发送(禁止命令)然后做出反应,然后该机器人检查该反应并发送另一条消息并对它做出反应。无论如何,在此之后,机器人应该发送确认消息,仅此而已。但是,僵尸程序在发送第一条消息后立即发送消息,而无需等待响应。我该如何解决?

(最后一条消息[和GuildMember#ban]在第一个消息之后发送,尽管它应该等待响应):

bannedUser.ban({ days: banDuration, reason: banReason }).catch(err => {

    console.log(err)
    message.channel.send(`An error occured: ${err}`)
})

message.channel.send({ embed: banConfirmation })

整个事情:

        let bannedUser = message.mentions.members.first()
        let banDuration;
        let banReason;

        const noPermsEmbed = new Discord.MessageEmbed()
            .setTitle(":x: You do not have permission to perform this command!")
            .setColor(botconfig.colors.err)

        const UserDoesNotExistEmbed = new Discord.MessageEmbed()
            .setTitle(":warning: This user is not a member of this server.")
            .setColor(botconfig.colors.warn)

        const banEmbedReason = new Discord.MessageEmbed()
            .setTitle(`You are about to ban ${bannedUser.username}, first pick a reason for the ban.`)
            .setDescription("Please pick a reason for your ban first")
            .addFields({ name: ':regional_indicator_a: Use of bad language', value: 'Ban the user for use of inappropriate language in the server' }, { name: ':regional_indicator_b: Insulting a member', value: 'Ban the user for insulting memebers on the server for any reason' }, { name: ':regional_indicator_c: Spamming in the server', value: 'Ban the user for spamming messages on the server' }, { name: ':regional_indicator_d: NSFW/harmful/inappropriate content', value: 'Ban the user for sending inappropriate content on the server' }, { name: ':regional_indicator_e: Other..', value: 'For a differnet reason, write the reason in chat, like "$banreason <reason>"' }, { name: ':x: None', value: '\u200b' }, )
            .setColor(botconfig.colors.err)

        const banEmbedDuration = new Discord.MessageEmbed()
            .setTitle(`Now, please pick a duration for the ban`)
            .setDescription("Please pick a duration for your ban first")
            .addFields({ name: ':regional_indicator_a: 1 day', value: '\u200b' }, { name: ':regional_indicator_b: 3 days', value: '\u200b' }, { name: ':regional_indicator_c: 7 days', value: '\u200b' }, { name: ':regional_indicator_d: 14 days', value: '\u200b' }, { name: ':regional_indicator_e: 28 days', value: '\u200b' }, { name: ':infinity: Forever', value: '\u200b' }, )
            .setColor(botconfig.colors.err)

        const banConfirmation = new Discord.MessageEmbed()
            .setTitle(`You sucessfully banned ${bannedUser.username}.`)
            .setDescription(`You have sucessfuly banned ${bannedUser} from the server.`)
            .addFields({ name: ':timer: Ban duration:', value: `${banDuration} days. (0 days = forever)` }, { name: ':page_with_curl:', value: `"${banReason}"` })
            .setColor(botconfig.colors.success)

        if (!message.member.hasPermission(['BAN_MEMBERS'])) {
            message.channel.send(noPermsEmbed)
            return
        }

        if (!message.guild.member(bannedUser)) {
            message.channel.send(UserDoesNotExistEmbed)
            return
        }

        message.channel.send({ embed: banEmbedReason }).then(embedMessage => {

            embedMessage.react("?");
            embedMessage.react("?");
            embedMessage.react("?");
            embedMessage.react("?");
            embedMessage.react("❌");

            const reasonFilter = (reaction, user) => {
                return ['?', '?', '?', '?', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
            };

            embedMessage.awaitReactions(reasonFilter, { max: 1, time: 120000 }).then(collected => {

                message.channel.send({ embed: banEmbedDuration }).then(embedMessage => {

                    embedMessage.react("?");
                    embedMessage.react("?");
                    embedMessage.react("?");
                    embedMessage.react("?");
                    embedMessage.react("?");
                    embedMessage.react("♾️");

                    const durationFilter = (reaction, user) => {
                        return ['?', '?', '?', '?', '?', '♾️'].includes(reaction.emoji.name) && user.id === message.author.id;
                    };

                    embedMessage.awaitReactions(durationFilter, { max: 1, time: 120000 }).then(collected => {

                        const durationReaction = collected.first()

                        if (durationReaction.emoji.name === '?') {
                            banDuration = 1
                        } else if (durationReaction.emoji.name === '?') {
                            banDuration = 3
                        } else if (durationReaction.emoji.name === '?') {
                            banDuration = 7
                        } else if (durationReaction.emoji.name === '?') {
                            banDuration = 14
                        } else if (durationReaction.emoji.name === '?') {
                            banDuration = 28
                        } else if (durationReaction.emoji.name === '♾️') {
                            banDuration = 0 //infinite
                        }
                    })
                })

                const reasonReaction = collected.first()

                if (reasonReaction.emoji.name === '?') {
                    banReason = "Use of bad language"
                } else if (reasonReaction.emoji.name === '?') {
                    banReason = "Insulting a member"
                } else if (reasonReaction.emoji.name === '?') {
                    banReason = "Spamming in the server"
                } else if (reasonReaction.emoji.name === '?') {
                    banReason = "NSFW/harmful/inappropriate content"
                } else if (reasonReaction.emoji.name === '❌') {
                    banReason = "None specified"
                }
            })
        })

        bannedUser.ban({ days: banDuration, reason: banReason }).catch(err => {

            console.log(err)
            message.channel.send(`An error occured: ${err}`)
        })

        message.channel.send({ embed: banConfirmation })

    }

(图片中未定义的问题与问题无关,只是在用户对第一条消息做出反应之前已发送绿色条纹消息。我已经解决了未定义的问题)

结果为:

enter image description here

更新的代码:

const Discord = require("discord.js")
const botconfig = require("../botconfig.json")

module.exports = {
    name: 'ban',
    description: 'Bans a user from your server.',
    execute(message, args) {

        let bannedUser = message.mentions.members.first()
        let banDuration;
        let banReason;

        const noPermsEmbed = new Discord.MessageEmbed()
            .setTitle(":x: You do not have permission to perform this command!")
            .setColor(botconfig.colors.err)

        const UserDoesNotExistEmbed = new Discord.MessageEmbed()
            .setTitle(":warning: This user is not a member of this server.")
            .setColor(botconfig.colors.warn)

        const banEmbedReason = new Discord.MessageEmbed()
            .setTitle(`You are about to ban ${bannedUser.user.username}, first pick a reason for the ban.`)
            .setDescription("Please pick a reason for your ban first")
            .addFields({ name: ':regional_indicator_a: Use of bad language', value: 'Ban the user for use of inappropriate language in the server' }, { name: ':regional_indicator_b: Insulting a member', value: 'Ban the user for insulting memebers on the server for any reason' }, { name: ':regional_indicator_c: Spamming in the server', value: 'Ban the user for spamming messages on the server' }, { name: ':regional_indicator_d: NSFW/harmful/inappropriate content', value: 'Ban the user for sending inappropriate content on the server' }, { name: ':regional_indicator_e: Other..', value: 'For a differnet reason, write the reason in chat, like "$banreason <reason>"' }, { name: ':x: None', value: '\u200b' }, )
            .setColor(botconfig.colors.err)

        const banEmbedDuration = new Discord.MessageEmbed()
            .setTitle(`Now, please pick a duration for the ban`)
            .setDescription("Please pick a duration for your ban first")
            .addFields({ name: ':regional_indicator_a: 1 day', value: '\u200b' }, { name: ':regional_indicator_b: 3 days', value: '\u200b' }, { name: ':regional_indicator_c: 7 days', value: '\u200b' }, { name: ':regional_indicator_d: 14 days', value: '\u200b' }, { name: ':regional_indicator_e: 28 days', value: '\u200b' }, { name: ':infinity: Forever', value: '\u200b' }, )
            .setColor(botconfig.colors.err)

        const banConfirmation = new Discord.MessageEmbed()
            .setTitle(`You sucessfully banned ${bannedUser.user.username}.`)
            .setDescription(`You have sucessfuly banned ${bannedUser} from the server.`)
            .addFields({ name: ':timer: Ban duration:', value: `${banDuration} days. (0 days = forever)` }, { name: ':page_with_curl: Ban reason:', value: `"${banReason}"` })
            .setColor(botconfig.colors.success)

        if (!message.member.hasPermission(['BAN_MEMBERS'])) {
            message.channel.send(noPermsEmbed)
            return
        }

        if (!message.guild.member(bannedUser)) {
            message.channel.send(UserDoesNotExistEmbed)
            return
        }

        message.channel.send({ embed: banEmbedReason }).then(embedMessage => {

            const reasonFilter = (reaction, user) => {
                return ['?', '?', '?', '?', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
            };

            embedMessage.react("?")
                .then(() => embedMessage.react("?"))
                .then(() => embedMessage.react("?"))
                .then(() => embedMessage.react("?"))
                .then(() => embedMessage.react("❌"))
                .then(() => {
                    embedMessage.awaitReactions(reasonFilter, { max: 1, time: 120000 }).then(collected => {

                        message.channel.send({ embed: banEmbedDuration }).then(embedMessage => {

                            embedMessage.react("?");
                            embedMessage.react("?");
                            embedMessage.react("?");
                            embedMessage.react("?");
                            embedMessage.react("?");
                            embedMessage.react("♾️");

                            const durationFilter = (reaction, user) => {
                                return ['?', '?', '?', '?', '?', '♾️'].includes(reaction.emoji.name) && user.id === message.author.id;
                            };

                            embedMessage.awaitReactions(durationFilter, { max: 1, time: 120000 }).then(collected => {

                                const durationReaction = collected.first()

                                if (durationReaction.emoji.name === '?') {
                                    banDuration = 1
                                } else if (durationReaction.emoji.name === '?') {
                                    banDuration = 3
                                } else if (durationReaction.emoji.name === '?') {
                                    banDuration = 7
                                } else if (durationReaction.emoji.name === '?') {
                                    banDuration = 14
                                } else if (durationReaction.emoji.name === '?') {
                                    banDuration = 28
                                } else if (durationReaction.emoji.name === '♾️') {
                                    banDuration = 0 //infinite
                                }
                            })
                        })

                        const reasonReaction = collected.first()

                        if (reasonReaction.emoji.name === '?') {
                            banReason = "Use of bad language"
                        } else if (reasonReaction.emoji.name === '?') {
                            banReason = "Insulting a member"
                        } else if (reasonReaction.emoji.name === '?') {
                            banReason = "Spamming in the server"
                        } else if (reasonReaction.emoji.name === '?') {
                            banReason = "NSFW/harmful/inappropriate content"
                        } else if (reasonReaction.emoji.name === '❌') {
                            banReason = "None specified"
                        }
                    })
                })
        }).then(() => {

            bannedUser.ban({ days: banDuration, reason: banReason }).catch(err => {
                console.log(err)
                message.channel.send(`An error occured: ${err}`)
            })

            message.channel.send({ embed: banConfirmation })
        })

    }
}

1 个答案:

答案 0 :(得分:1)

请确保在等待机器人发出响应之前等待它们,否则它将检测到自己。 (使用.react函数基本上是在发送诺言,它可以在等待开始后完成。)

您可以通过以下方式执行此操作:

message.react('?')
    .then(() => message.react('?'))
    .then(() => message.react('?'))
    .then(() => message.awaitReactions(<...>).then((...) => {...})

(只有一些伪代码。)