Create Kick and Ban Command Using discord.js v12 1

Create Kick and Ban Command Using discord.js v12

If you having problem in creating the kick and ban command with discord.js new version which is v12 then this post will be very helpful for you. Before you start i shall tell you i will be using handler if you do not know what is handler than read this post – COMMAND HANDLER, So we will start with kick command.

Creating Kick Command

Given below is my command handler setup, i created a file with name kick.js and now i am gonna code in it

const discord = require("discord.js");

module.exports = {
  name: "kick",
  category: "moderation",
  description: "Kick anyone with one shot xD",
  usage: "kick <@user> <raeson>",
  run: (client, message, args) => {
    
   
    }
 }

First of all we should let our bot check author permission and his own permissions.

    if(!message.member.hasPermission("KICK_MEMBERS")) {
      return message.channel.send(`**${message.author.username}**, You do not have enough permission to use this command`)
    }
    
    if(!message.guild.me.hasPermission("KICK_MEMBERS")) {
      return message.channel.send(`**${message.author.username}**, I do not have enough permission to use this command`)
    }

Now we will be defining the Target(person who we want to kick), then add the error message if there author did not mentioned the target.

 let target = message.mentions.members.first();
    
    if(!target) {
      return message.channel.send(`**${message.author.username}**, Please mention the person who you want to kick`)
    }

We need to make sure that author did not kick himself -_-, so we will add the error message for that also

   if(target.id === message.author.id) {
     return message.channel.send(`**${message.author.username}**, You can not kick yourself`)
    }

we also need to make sure that author must give the reason for kicking member

 if(!args[1]) {
    return message.channel.send(`**${message.author.username}**, Please Give Reason to ban`)
  }

Now we are ready to add main thing, first we will send the embed message then kick the target.

 let embed = new discord.MessageEmbed()
    .setTitle("Action: Kick")
    .setDescription(`Banned ${target} (${target.id})`)
    .setColor("#ff2050")
    .setFooter(`Banned by ${message.author.username}`);
    
    message.channel.send(embed)
    
    target.kick(args[1]);

we are done here and you are ready to kick anyone with amazing embed kick command.

Creating Ban Command

Creating ban command is same as kick command but you need to change single thing which is given below

target.kick(args[1]); 
TO
target.ban(args[1]);

Do not forget to change small things like permissions and embed message 🙂

Table of Contents

Resulting Code

Here is the both ban and kick command – CLICK

Leave a Reply

Your email address will not be published. Required fields are marked *