Skip to content

Kick command | discord.js

I’ve made a kick command for my bot, everything works fine but everyone can use it. I want that only people who have perms to use this command. Is there any solution to fix this problem? This is my code:

const Discord = require('discord.js');
const prefix = require('../config.json');

module.exports = {
  name: 'kick',
  description: 'kick',
  aliases: ['kick'],
  execute: async (client, message, args) => {


    if (message.content.startsWith('m!kick')) {
  message.channel.send(embed);
    const user = message.mentions.users.first();
    if (user) {
      const member = message.guild.members.resolve(user);
      if (member) {
        member
          .kick({
            reason: 'They were bad!',
          })
          .then(() => {
            const embed = new Discord.MessageEmbed()
            .setColor('GREEN')
            .setTitle(`**Successfully kicked ${user.tag}.**`,)
            .setFooter(`This feature is still in beta, so there can be issues.`)

            message.channel.send(embed);
          })
          .catch(err => {
            const embed = new Discord.MessageEmbed()
            .setColor('RED')
            .setTitle(`**I can't kick this member!**`,)
            .setFooter(`This feature is still in beta, so there can be issues.`)

            message.channel.send(embed);
            console.error(err);
          });
      } else {
        const embed = new Discord.MessageEmbed()
        .setColor('RED')
        .setTitle(`**There is no user with this username in this server!**`,)
        .setFooter(`This feature is still in beta, so there can be issues.`)

        message.channel.send(embed);
      }
    } else {
        const embed = new Discord.MessageEmbed()
        .setColor('RED')
        .setTitle(`**Please mention someone.**`,)
        .setFooter(`This feature is still in beta, so there can be issues.`)
    
      message.channel.send(embed);
    }
  }
  }
}

Please help me! Thank you!

Answer

Use:

if (!message.member.hasPermission('KICK_MEMBERS')) { 
    const embed = new MessageEmbed
    embed.setTitle("You don't have the `kick_members` permission!")
    embed.setColor("RED")
    return message.channel.send(embed)
    }