ccdue 1
ccdue 1

Reputation: 11

My kick command lets anyone in the server kick someone

I need my kick command for my discord bot only be able to work for moderators and admins. Does anyone have any more coding that could make it so only mods or admins could kick?

My coding for the kick command:

client.on('message', (message) => {
 if (!message.guild) return;

 if (message.content.startsWith('!kick')) {
  const user = message.mentions.users.first();

  if (user) {
   const member = message.guild.member(user);

   if (member) {
    member
     .kick('Optional reason that will display in the audit logs')
     .then(() => {
      message.reply(`Successfully kicked ${user.tag}`);
     })
     .catch((err) => {
      message.reply('I was unable to kick the member');

      console.error(err);
     });
   } else {
    message.reply("That user isn't in this guild!");
   }
  } else {
   message.reply("You didn't mention the user to kick!");
  }
 }
});

Upvotes: 0

Views: 116

Answers (1)

Lioness100
Lioness100

Reputation: 8402

You can use GuildMember.hasPermission to check if a user has a certain permission. You can see the valid permission flags here, although I think you'll want to use KICK_MEMBERS in this case.

if (!message.member.hasPermission('KICK_MEMBERS'))
  return message.channel.send('Insufficient Permissions');

You can also restrict access via the roles someone has, for which I urge you to read this existing answer

Upvotes: 1

Related Questions