Scoooolzs
Scoooolzs

Reputation: 50

discord.js command handler Bugs

I want to trying a Command Handler, But i have an error. If i run ,ping the bot is response, But if i run , i get an error, this is the logs:

TypeError: Cannot read property 'execute' of undefined
at Client.<anonymous> (C:\Users\User\disbot\app.js:35:33)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (C:\Users\User\disbot\node_modules\discord.js\src\client\actions\MessageCreate.js:23:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\User\disbot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\User\disbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31)
at WebSocketShard.onPacket (C:\Users\User\disbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\User\disbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (C:\Users\User\disbot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (C:\Users\User\disbot\node_modules\ws\lib\websocket.js:978:20)

this the code in app.js (main)

const Discord = require('discord.js')
const { Collection } = require('discord.js')
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const fs = require("fs")

const PREFIX = ','

client.commands = new Collection()

const files = fs
    .readdirSync("./src/commands")
    .filter((file) => file.endsWith(".js"))

for(const file of files) {
    const command = require (`./src/commands/${file}`)
    client.commands.set(command.name, command);
}

if(process.env.NODE_ENV !==  `production`) {
    require('dotenv').config()
}

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
    client.user.setActivity(process.env.STATUS_NAME, { type: process.env.STATUS_TYPE })
    client.user.setStatus(`${process.env.PROFILE_TYPE}`)
});

client.on('messageCreate', message => {
    if(message.author.bot) return;
    if(!message.content.startsWith(PREFIX)) return;
    let args = message.content.substring(PREFIX.length).split(" ");
    client.commands.get(args[0]).execute(message);
});

client.login(process.env.TOKEN)

this in the ping.js

module.exports = {
     name: 'ping',
     description: 'Ping commands adalah mengetes koneksi bot.',
     execute(message) {
         message.channel.send(`pong`);
     }
 }

Upvotes: 0

Views: 149

Answers (1)

Zero
Zero

Reputation: 2286

Not a good approach to get command from arguments, args[0] here without any context like in your case if you just type out , would not be registered as a command, instead you can try this:


    const command = client.commands.get(args[0])
    if (!command) {
        return
    } else {
        try {
            command.execute(message);
        } catch (e) {
            console.log(e)
        }
    }

aliter


    const commandName = args.shift().toLowerCase();
    
    if (!client.commands.has(commandName)) return;
    const command = client.commands.get(commandName)
     try {
        command.execute(message);
    } catch (error) {
        console.log(error);
    }

Upvotes: 1

Related Questions