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
JavaScript
x
37
37
1
const Discord = require('discord.js')
2
3
const client = new Discord.Client()
4
5
const {TOKEN ,PREFIX} = require('./config')
6
7
const fs = require('fs')
8
9
client.commands = new Discord.Collection();
10
11
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
12
for (const file of commandFiles){
13
const command = require(`./commands/${file}`);
14
client.commands.set(command.name, command)
15
}
16
17
client.on('ready', () =>{
18
console.log("I'm ready!!!")
19
})
20
21
client.on('message', message =>{
22
23
if (!message.content.startsWith(PREFIX)|| message.author.bot) return
24
25
const args = message.content.slice(PREFIX.length).split(/ +/);
26
const command = args.shift().toLowerCase();
27
28
if (command == 'ping'){
29
client.commands.get('ping').execute(message,args)
30
}
31
32
});
33
34
client.login(TOKEN)
35
36
37
And my ping.js File
JavaScript
1
11
11
1
module.export = {
2
name: 'ping',
3
description: "ping",
4
execute(message, args){
5
6
message.channel.send('pong')
7
8
}
9
10
}
11
The error i’m getting
JavaScript
1
16
16
1
C:discordmain.js:29
2
client.commands.get('ping').execute(message,args)
3
^
4
5
TypeError: Cannot read property 'execute' of undefined
6
at Client.<anonymous> (C:discordmain.js:29:33)
7
at Client.emit (events.js:314:20)
8
at MessageCreateAction.handle (C:discordnode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
9
at Object.module.exports [as MESSAGE_CREATE] (C:discordnode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
10
at WebSocketManager.handlePacket (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:384:31)
11
at WebSocketShard.onPacket (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
12
at WebSocketShard.onMessage (C:discordnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
13
at WebSocket.onMessage (C:discordnode_moduleswslibevent-target.js:132:16)
14
at WebSocket.emit (events.js:314:20)
15
at Receiver.receiverOnMessage (C:discordnode_moduleswslibwebsocket.js:970:20)
16
Advertisement
Answer
Use module.exports, as the export property does not exist on module (export needs to be pluralised).