'Discord.js v13 kick command not working can someone help me?

Hi, im creating a kick command for my discord bot

Here's the code

client.on("message", (message) => {
    if (message.content.startsWith("!kick")){

    }
    
         var utenteKick = message.mentions.members.first();
         
         if(!message.member.permissions.has("KICK_MEMBERS")){
             message.channel.send("Non hai il permesso");
             return;
         }

         if(!utenteKick.kickable) {
             message.channel.send("Il bot non ha il permesso");
             return;
         }

         if(!utenteKick){
             message.channel.send("Non hai menzionato nessun utente");
             return;
         }

         utenteKick.kick()
          .then(() => message.channel.send("<@" + utenteKick + "> è stato kickato"))
        
})

Here's the error:

C:\Users\Admin\Desktop\Discord bot\MiningMC\index.js:50
         if(!utenteKick.kickable) {
                        ^

TypeError: Cannot read properties of undefined (reading 'kickable')
    at Client.<anonymous> (C:\Users\Admin\Desktop\Discord bot\MiningMC\index.js:50:25) 
    at Client.emit (node:events:527:28)
    at MessageCreateAction.handle (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\discord.js\src\client\actions\MessageCreate.js:34:18)
    at module.exports [as MESSAGE_CREATE] (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:527:28)
    at Receiver.receiverOnMessage (C:\Users\Admin\Desktop\Discord bot\MiningMC\node_modules\ws\lib\websocket.js:1160:20)

Can someone help me? i'm using visual studio code discord.js version v13 https://github.com/UnsavorySpirit/MiningMC-Bot



Solution 1:[1]

Before starting using the new discord version please take some time to read the documentation there are some differences between versions. The message event has been renamed to messageCreate, to bring the library in line with Discord's naming conventions. Using message will still work, but you'll receive a deprecation warning until you switch over.

Note the differences in the first line:

- client.on("message", message => { ... });
+ client.on("messageCreate", message => { ... });

You should check if utentekick is undefined before doing anything with it like this :

client.on("messageCreate", (message) => { 
    if (message.content.startsWith("!kick")){

    //} <- this was also a problem : move it to the end
    
         var utenteKick = message.mentions.members.first();
         
         if(!message.member.permissions.has("KICK_MEMBERS")){
             message.channel.send("Non hai il permesso");
             return;
         }

         if(!utenteKick){ // this before the other
             message.channel.send("Non hai menzionato nessun utente");
             return;
         }
         
         if(!utenteKick.kickable) {
               message.channel.send("Il bot non ha il permesso");
               return;
         }

         utenteKick.kick()
         .then(() => message.channel.send("<@" + utenteKick + "> รจ stato kickato"))
        
    } // this must be here
})

edit:

there was also a problem with a curly bracket (the code to kick someone was outside of the condition, every message trigerred the kick)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fernando Filipe