ban command discord.js v12 code example
Example 1: Discord.js ban command
if (msg.member.hasPermission("BAN_MEMBERS")) {
if (msg.members.mentions.first()) {
try {
msg.members.mentions.first().ban();
} catch {
msg.reply("I do not have permissions to ban" + msg.members.mentions.first());
}
} else {
msg.reply("You do not have permissions to ban" + msg.members.mentions.first());
}
}
Example 2: Discord.js ban command
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
if (!message.guild) return;
if (message.content.startsWith('!ban')) {
const user = message.mentions.users.first();
if (user) {
const member = message.guild.members.resolve(user);
if (member) {
member
.ban({
reason: 'They were bad!',
})
.then(() => {
message.channel.send(`Successfully banned ${user.tag}`);
})
.catch(err => {
message.channel.send('I was unable to ban the member');
console.error(err);
});
} else {
message.channel.send("That user isn't in this guild!");
}
} else {
message.channel.send("You didn't mention the user to ban!");
}
}
});
client.login('your token here');
Example 3: discord.js v12 command ban
if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
let User = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
if (!User) return message.channel.send("Invalid User")
if (User.hasPermission("BAN_MEMBERS")) return message.reply("Invalid Permissions")
let banReason = args.join(" ").slice(22);
if (!banReason) {
banReason = "None"
}
User.ban({reason: banReason})
Example 4: discord.js ban user
let member = message.mentions.members.first();
if(!member) return message.reply("Please mention a valid member of this server");
if(!member.bannable) return message.reply("I cannot ban this member!");
member.ban();
Example 5: Discord.js ban command
if (msg.member.hasPermission("KICK_MEMBERS")) {
if (msg.members.mentions.first()) {
try {
msg.members.mentions.first().kick();
} catch {
msg.reply("I do not have permissions to kick " + msg.members.mentions.first());
}
} else {
msg.reply("You do not have permissions to kick " + msg.members.mentions.first());
}
}