Max Rheault
Max Rheault

Reputation: 27

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:\discord\main.js:29
     client.commands.get('ping').execute(message,args)
                                ^

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\discord\main.js:29:33)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (C:\discord\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\discord\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\discord\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\discord\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:314:20)
    at Receiver.receiverOnMessage (C:\discord\node_modules\ws\lib\websocket.js:970:20)

Upvotes: 0

Views: 76

Answers (1)

Ibz
Ibz

Reputation: 437

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

Upvotes: 2

Related Questions