Reputation: 5
So, I'm making a kick command, what happens is, when you don't specify a reason or you dont mention a user to kick, the bot responds back to you correctly, but the bot DOESNT kick the mentioned user.
Here's my code.
const { MessageEmbed } = require('discord.js')
module.exports = {
name: "kick",
description: 'Kicks a user.',
usage: "<user mention> < reason>",
execute(message, args){
let kickedMember = message.mentions.users.first() || "No reason specified."
let reason = args.slice(1).join(" ")
if(!message.member.hasPermission(["KICK_MEMBERS"])) return message.reply("You don't have the
required permissions to run this command.")
if(!kickedMember) return message.channel.send('Please mention a user to kick.')
if(!reason) return message.reply("Please specify a reason.")
kickedMember.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the
rules to avoid being kicked next time`).then(() =>
kickedMember.kick()).catch(err => console.log(err))
const embed = new MessageEmbed()
.setTitle('User Kicked')
.addField("User Kicked", kickedMember)
.addField("Reason", reason)
.addField("Moderator", message.author.tag)
.addField("Kicked Date", message.createdAt.toLocaleString())
.setThumbnail(kickedMember.displayAvatarURL())
.setColor()
.setFooter(`Kicked by ${message.author.tag}`)
.setTimestamp(Date.now())
message.channel.send(embed);
}
}
Yes, I did define MessageEmbed but it wont show up in this code for some reason.
Upvotes: 0
Views: 587
Reputation: 761
You cannot kick a user, you can only kick a member, and you should also check if the member is kickable using .kickable
. Discord.js's GuildMember
has a property called GuildMember#kickable
which will return true if the user can be kicked, what you want to do is add it to your code:
// Define Discord and use it for the MessageEmbed(), because it may be an error at that part.
const Discord = require('discord.js');
module.exports = {
name: "kick",
description: 'Kicks a user.',
usage: "<user mention> <reason>",
execute(message, args){
// Get the `member` property not the `user` property.
let kickedMember = message.mentions.members.first()
// I think you meant to put the "No Reason Specified." here?
let reason = args.slice(1).join(" ") || "No Reason Specified"
// `.hasPermission()` is deprecated
if(!message.member.permissions.has(["KICK_MEMBERS"])) return message.reply("You don't have the required permissions to run this command.")
if(!kickedMember) return message.channel.send('Please mention a member to kick.')
// You don't need this, since you added "No Reason Specified above."
// if(!reason) return message.reply("Please specify a reason.")
// Avoid using whatever you did here, because it might break.
// kickedMember.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the rules to avoid being kicked next time`).then(() =>
// kickedMember.kick()).catch(err => console.log(err))
// Check if the bot has permission
const botMember = message.guild.members.cache.get(client.user.id);
if (!botMember.permissions.has(['KICK_MEMBERS'])) return message.reply('The bot doesn't have the required permission /`KICK_MEMBERS/`');
// Check if the member is kickable
if (!kickedMember.kickable) return message.reply('This member cannot be kicked, check if they have a higher role than the bot!')
// Kick the member and then send the message, not the other way around.
kickedMember.kick({
reason: reason
}).then(member => { // This passes `member` as the member that was kicked.
// Send the message to the kicked member
member.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the rules to avoid being kicked next time`).catch(err => console.log(err));
const embed = new MessageEmbed()
.setTitle('User Kicked')
.addField("User Kicked", member.tag) // Don't pass their data, just tag, because
// once you kick them, they're no longer
// in the cache.
.addField("Reason", reason)
.addField("Moderator", message.author.tag)
.addField("Kicked Date", message.createdAt) // `.createdAt` is already a string.
.setThumbnail(member.displayAvatarURL())
.setColor()
.setFooter(`Kicked by ${message.author.tag}`)
.setTimestamp() // You don't need `Date.now()` discord automatically parses
// the timestamp into string.
// Send the embed
message.channel.send(embed);
})
}
}
For more info, check out these links:
GuildMember
propertyGuildMember#kickable
propertyUpvotes: 1