Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  115] [ 6]  / answers: 1 / hits: 5268  / 4 Years ago, thu, august 13, 2020, 12:00:00

I have a message that my bots sends (ban command) and then reacts to, then the bot checks the reaction and sends another message and reacts to it as well. Anyway, after that the bot should send a confirmation messages and that's it. But, the bot sends the message right after it sends the first messages, without waiting for a reaction. How can I fix this?


(the last message [and GuildMember#ban] being sent right after the first message that although it should wait for a reaction):


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

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

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

the whole thing:


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

}

(the undefined(s) in the picture are not related to the problem, it's just that the green stripe message get's sent before the user can react to the first message. i fixed the undefined problem already)


It results with:


enter


updated code:


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

}
}

More From » discord

 Answers
4

Be sure to wait for your bot to put on the reactions before waiting for them, otherwise it will detect itself. (Using the .react function is basically sending a promise, and it can finish after your await starts.)


You can do this in such way:


message.react('🍎')
.then(() => message.react('🍊'))
.then(() => message.react('πŸ‡'))
.then(() => message.awaitReactions(<...>).then((...) => {...})

(It's only some pseudo code.)


[#2903] Tuesday, August 11, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
;