Reputation: 1922
I have a ban command that bans a user yada yada.. But, somehow, a different command in a different file gets triggered. (I use a command handler).
This is the switch
statement in index.js
:
bot.on("message", message => {
let args = message.content.substring(botconfig.prefix.length).split(" ")
switch (args[0]) {
case "ban":
bot.commands.get('ban').execute(message, args)
case "unban":
bot.commands.get('unban').execute(message, args)
}
})
This is ban.js
(the file that triggers unban.js
)
const Discord = require("discord.js")
const botconfig = require("../botconfig.json")
const ms = require("ms")
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)
if (!message.member.hasPermission(['BAN_MEMBERS']) || bannedUser.hasPermission(['MANAGE_GUILD'])) {
message.channel.send(noPermsEmbed)
return
}
if (!message.guild.member(bannedUser)) {
message.channel.send(UserDoesNotExistEmbed)
return
}
message.channel.send({ embed: banEmbedReason }).then(embedMessage => {
// GETS TRIGGERED HERE
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 => {
const otherCollector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 60000 });
otherCollector.on('collect', msg => {
if (msg.content.startsWith(`${botconfig.prefix}banreason`)) {
banReason = msg.content
}
})
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"
}
message.channel.send({ embed: banEmbedDuration }).then(embedMessage => {
const durationFilter = (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.react("βΎοΈ"))
.then(() => {
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
}
})
.then(() => {
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)
bannedUser.ban({ days: 7, reason: banReason }).catch(err => {
console.log(err)
message.channel.send(`An error occured: ${err}`)
})
message.channel.send({ embed: banConfirmation })
if (banDuration == 0) {} else {
console.log('Starting countdown untill ban ends.')
setTimeout(() => {
message.guild.members.unban(bannedUser, "The ban duration has passed.")
}, ms(`${banDuration}d`))
}
})
})
})
})
})
})
}
}
This is the file that get unban unban.js
:
const Discord = require("discord.js")
const botconfig = require("../botconfig.json")
module.exports = {
name: 'unban',
description: 'Unban a user from the server',
execute(message, args) {
let unbannedUser = message.mentions.members.first()
const confirmEmbed = new Discord.MessageEmbed()
.setTitle(`:white_check_mark: You have sucessfully unbanned ${unbannedUser.user.username}.`)
.setColor(botconfig.colors.success)
const errEmbed = new Discord.MessageEmbed()
.setTitle(`:x: A problem occcured while trying to unbaning ${unbannedUser.user.username}.`)
.setColor(botconfig.colors.err)
try {
// THIS GETS TRIGGERED
message.guild.members.unban(unbannedUser)
message.channel.send({ embed: confirmEmbed })
} catch (e) {
message.channel.send({ embed: errEmbed })
}
}
}
This is the result: (when the first message gets sent it sends the message that says you ubnanned someone right after it, which is in a different file(?))
Upvotes: 0
Views: 57
Reputation: 6625
This is because you haven't added break
to your switch
statement.
bot.on("message", message => {
let args = message.content.substring(botconfig.prefix.length).split(" ")
switch (args[0]) {
case "ban":
bot.commands.get('ban').execute(message, args)
break
case "unban":
bot.commands.get('unban').execute(message, args)
}
})
If you omit the break statement, the next case will be executed even if the evaluation does not match the case. It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
Upvotes: 2