I recently made a kick command for my bot using arguments. I didn't want to use message.mentions.users.first()
as that would kick a user if his/her message is replied with !kick
. I have given my code below. The bot only asks me to mention a user even though I've already mentioned a user.
module.exports = {
name: 'kick',
description: 'kicks a user',
async execute(message, args){
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply('Insufficient Permissions')
return;
}
if (!message.guild.me.hasPermission('KICK_MEMBERS')) {
message.reply('I need the `KICK_MEMBERS` permission to execute this!')
return;
}
const user = message.guild.members.cache.get(args[1]);
if (!user) {
message.reply('Mention the uer to kick!')
return;
}
if (message.guild.members.cache.get(args[1]) == undefined) {
message.reply('Couldn\'t find the user')
return;
}
const reason = args.slice(2).join(' ')
await user.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
.catch(err => {
console.log(err)
});
await user.kick(reason);
await message.channel.send(`**${message.author.username}** kicked **${user.username}**! Reason : ${reason}`)
}
}