My Discord bot's kick command isn't working. What's wrong?

I’m having trouble with my Discord bot’s kick command. When I type !kick @user reason, nothing happens. The bot just ignores it. Here’s my code:

bot.on('messageCreate', msg => {
  const args = msg.content.toLowerCase().trim().split(/ +/);
  const cmd = args.shift();

  if (cmd === '!kick') {
    if (!msg.member.permissions.has('KickMembers'))
      return msg.reply('You need kick permissions.');
    if (!msg.guild.me.permissions.has('KickMembers'))
      return msg.reply('I need kick permissions.');

    const target = msg.mentions.members.first();
    if (!target) return msg.reply('Mention a user to kick.');

    const kickReason = args.slice(1).join(' ') || 'No reason provided';

    if (!target.kickable)
      return msg.reply('Cannot kick this user.');

    target.kick(kickReason)
      .then(() => msg.channel.send(`Kicked ${target.user.tag}`))
      .catch(err => msg.reply('Failed to kick user.'));
  }
});

What am I doing wrong? Why isn’t the kick command working?

I’ve encountered this issue before, and it’s often due to Discord’s recent API changes. Have you updated your discord.js library to the latest version? Older versions may not work correctly with newer Discord features.

Another thing to check is your bot’s token. Make sure it’s still valid and hasn’t been regenerated. If it has, you’ll need to update it in your code.

Also, try adding some error handling to your kick function. Something like:

target.kick(kickReason).catch(error => console.error(‘Error kicking user:’, error));

This will help you see any errors that might be occurring. If you’re still stuck, consider using Discord.js’s built-in command handler. It can make managing commands much easier and less prone to errors.

yo, have u checked ur bot’s permissions in the server settings? sometimes ppl forget to give the bot the right roles. also, try using msg.member.kick() instead of target.kick(). it worked for me when i had this problem. good luck man!

I ran into a similar issue with my Discord bot’s kick command. After some troubleshooting, I found a couple of potential problems in your code.

First, make sure your bot has the ‘KICK_MEMBERS’ intent enabled in the Discord Developer Portal. Without this, the bot won’t have permission to kick users, even if it has the role permissions.

Second, check if you’re using the correct intents when initializing your bot. You might need to add:

const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_MEMBERS] });

Lastly, double-check that your bot’s role in the server is higher than the roles of the users you’re trying to kick. Discord’s hierarchy system prevents bots from modifying users with higher or equal roles.

If these don’t solve the issue, try adding some console.log() statements in your code to see where it’s getting stuck. Good luck!