Search code examples
javascriptdiscorddiscord.js

Cannot read properties of undefined (reading 'execute)


On my discord bot, I am making a "nickname" command. My code on the nickname.js:

const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
module.exports = {
    name: 'nick',
    description: 'This command with change a users nickname.',
    execute(message, args){
        const memberToEdit = message.mentions.members.first();
        const newNickname = message.content.replace(`${prefix}changenick`, '').split(' ').pop().trim();
        memberToEdit.setNickname(newNickname);
        }
}

The error I get whilst trying to run the command:

TypeError: Cannot read properties of undefined (reading 'execute')
    at Client.<anonymous> (C:\Users\Admin\Desktop\DiscordBot\main.js:79:36)
    at Client.emit (node:events:520:28)
    at MessageCreateAction.handle (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:520:28)
    at Receiver.receiverOnMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:1137:20)

Node.js v17.4.0

My main.js file is the same as the rest of the commands which work fine...

  if(command === 'nick'){
    client.commands.get('nickname').execute(message, args)
  }

I can't seem to understand why this command specifically just wont work.


Solution

  • As @PLASMA chicken said, the command you created is called nick, not nickname. So change nickname to nick where you call the execute function. For example, something like this:

    if (command === 'nick') {
        client.commands.get('nick').execute(message, args);
    }
    

    Hoped this helped!