Jakooob
Jakooob

Reputation: 37

Discord.js | TypeError: Cannot read property '0' of undefined

Okay so I'm trying to create a discord bot command (broadcast) and I want to get the args[0] as an ID to the broadcast channel, but when I try it this is what it logs:

TypeError: Cannot read property '0' of undefined
    at Object.execute (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\commands\broadcast.js:8:40)
    at Client.<anonymous> (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\index.js:69:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\ws\lib\event-target.js:125:16)        
    at WebSocket.emit (events.js:315:20)

Here is my code in the command:

module.exports = {
    name: 'broadcast',
    aliases: ["bc"],
    usage: "[channel id] [message]",
    description: "Broadcasts a message in a channel!",
    execute(client, message, args){

        const bcchannel = parseInt(args[0]);
        const bcchannelsend = client.channels.cache.get(bcchannel);

        if (isNaN(bcchannel)) {
            return message.reply('that doesn\'t seem to be a valid channel ID!');
        } else if(args){
            bcchannelsend.send(args.join(' '));
            }   
    }
}

Upvotes: 0

Views: 856

Answers (1)

Dorian349
Dorian349

Reputation: 1589

Cannot read property '0' of undefined means that args is undefined.

You need to define the args variable before using it.

You can do something like this: const args = message.content.slice(PREFIX.length).trim().split(" ");

Next time you see this kind of error, make sure to check which variable is affected by the undefined issue. ^

For your index.js file, I should replace:

    try {
        command.execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }

By:

    try {
        command.execute(client, message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }

Since your execute() method has 3 parameters and not two.

Upvotes: 1

Related Questions