Skip to content
Advertisement

error : Cannot read property ‘execute’ of undefined

hi I try to make a command handler for my discord bot but I always get an error when on discord I try my !ping command

Here’s my main.js file

const Discord = require('discord.js')

const client = new Discord.Client()

const {TOKEN ,PREFIX} = require('./config')

const fs = require('fs')

client.commands = new Discord.Collection();

const commandFiles  = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command)
}

client.on('ready', () =>{
    console.log("I'm ready!!!")
})
  
client.on('message', message =>{ 

if (!message.content.startsWith(PREFIX)|| message.author.bot) return

 const args = message.content.slice(PREFIX.length).split(/ +/);
 const command = args.shift().toLowerCase();

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

});

client.login(TOKEN)


And my ping.js File

module.export = {
    name: 'ping',
    description: "ping",
    execute(message, args){
    
    message.channel.send('pong')

    }

}

The error i’m getting

C:discordmain.js:29
     client.commands.get('ping').execute(message,args)
                                ^

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:discordmain.js:29:33)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (C:discordnode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:discordnode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
    at WebSocket.onMessage (C:discordnode_moduleswslibevent-target.js:132:16)
    at WebSocket.emit (events.js:314:20)
    at Receiver.receiverOnMessage (C:discordnode_moduleswslibwebsocket.js:970:20)

Advertisement

Answer

Use module.exports, as the export property does not exist on module (export needs to be pluralised).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement