Quang Hiển
Quang Hiển

Reputation: 25

How do I create a kick commands for a discord bot?

i can't kick users with this code, can someone help me?

client.on('kick', message => {
  if(message.content === '/kick'){
    const target = message.mentions.users.first();
    if(target){
        const memberTarget = message.guild.members.cache.get(target.id);
        memberTarget.kick();
        message.channel.send("đã kick");
    }else{
        message.channel.send(`không thể kick`);
    }
  }
})

Upvotes: 0

Views: 76

Answers (2)

Behemoth
Behemoth

Reputation: 9310

First of all your event is wrong. You need to use client.on("message" ... instead of client.on("kick", .... Next you don't want to check if your message.content equals /kick but only if it .startsWith("/kick"). Otherwise you won't be able to access the member you want to kick.

Additionally I suggest using message.mentions.members instead of message.mentions.users because it includes non-users (e. g. other bots) as well.

client.on("message", (message) => {
  if (message.content.startsWith("/kick")) {
    const member = message.mentions.members.first();
    if (member) {
      member.kick();
      message.channel.send("đã kick");
    } else {
      message.channel.send(`không thể kick`);
    }
  }
});

Also don't forget to make sure your bot has the proper permission to kick somebody.

Upvotes: 2

MrMythical
MrMythical

Reputation: 9041

"kick" is not the event you want. "message" is what you use instead

client.on("message", message => {
//...
})

Upvotes: 0

Related Questions