Darshan B
Darshan B

Reputation: 807

discord.js simple kick users command

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}`)

  }
}

Upvotes: 0

Views: 651

Answers (1)

AnanthDev
AnanthDev

Reputation: 1808

It is because .get() requires ID as a parameter

First method

replace your user constant with this const member = message.mentions.members.first() || message.guild.members.cache.get(args[1]) this way you can support both ID and mentions

Second method

use a regex

const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {}); This regex removes all non digit characters from the string, in discord mentions are made up like this <@USERID>, the regex removes <@> and returns the user ID which is then passed into .fetch() and returns the member that you need to kick.

This would be the correct code iirc

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;
    }

    if (!args[1) return message.reply('provide a member for me to kick');

    const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {});

    if (!member) return message.reply('Member not found :(')

    const reason = args.slice(2).join(' ')

    await member.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
    .catch(err => {
      console.log(err)
    });
    
    await member.kick(reason);

    await message.channel.send(`**${message.author.username}** kicked **${member.user.username}**! Reason : ${reason}`)

  }
}

Upvotes: 2

Related Questions