SrVertigu
SrVertigu

Reputation: 83

Some problems with the !kick command code

I'm having some problem with my Discord.js kick command.

My code:

const Discord = require('discord.js');

const { prefix, token } = require('../config.json');

module.exports = {
 name: 'kick',
 description: 'kick users',
 execute(message, args) {
  if (!message.member.hasPermission('KICK_MEMBERS')) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You are not allowed to use this command.`,
     footer: {
      text: ` | Required permission: KICK_MEMBERS`,
     },
    },
   });
  }
  if (!message.guild.me.permissions.has('KICK_MEMBERS')) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, I am not allowed to use this command.`,
     footer: {
      text: ` | Required permission: KICK_MEMBERS`,
     },
    },
   });
  }

  if (!args[0]) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You need to mention a user first.`,
     footer: {
      text: ` | Example: !kick @Bot`,
     },
    },
   });
  }
  const member =
   message.mentions.members.first() || message.guild.members.cache.get(args[0]);
  if (member.user.id === message.author.id) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You cannot expel yourself.`,
     footer: {
      text: ` | Example: !kick @Bot`,
     },
    },
   });
  }
  try {
   member.kick();
   message.channel.send(`${member} has been kicked!`);
  } catch (e) {
   return message.channel.send(`User isn't in this server!`);
  }
 },
};

Ignore the code not being complete, I'm still thinking about the design of the embeds!

I'm trying to do 3 things:

Upvotes: 0

Views: 108

Answers (1)

Lioness100
Lioness100

Reputation: 8402

I'm going to try to solve your problems one by one:


  • First of all, I would like if someone tried to use the command by mentioning the bot, they would say something like "you are not allowed to do this"

You can execute an if statement to detect if the member mentioned shares the same ID as your bot using the client.user property (the user your client is logged in as)

if (member.id === client.user.id)
 return message.channel.send('You cannot ban me');

  • The other thing I want is that it is not possible for a user to kick someone above him

You can solve this by comparing the roles.highest.position property of both members. This property will return a number. the higher the number, the higher the role in priority.

if (message.member.roles.highest.position <= member.roles.highest.position)
 return message.channel.send(
  'Cannot kick that member because they have roles that are higher or equal to you.'
 );

  • And lastly, I want the member to be kicked you have to react with yes or no

For this you'll need to use a reaction collector. This is how you can do it using Message.awaitReactions. This method will wait for someone to react to a message, then log the emoji reacted.

// first send the confirmation message, then react to it with yes/no emojis
message.channel
 .send(`Are you sure you want to kick ${member.username}?`)
 .then((msg) => {
  msg.react('👍');
  msg.react('👎');

  // filter function
  const filter = (reaction, user) =>
   ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id; // make sure it's the correct reaction, and make sure it's the message author who's reacting to it

  message
   .awaitReactions(filter, { time: 30000 }) // make a 30 second time limit before cancelling
   .then((collected) => {
    // do whatever you'd like with the reactions now

    if (message.reaction.name === '👍') {
     // kick the user
    } else {
     // don't kick the user
    }
   })
   .catch(console.log(`${message.author.username} didn't react in time`));
 });

Upvotes: 1

Related Questions