I get this error when I try to run my code ReferenceError: message is not defined
at Client.<anonymous> (C:\Users\Diego M\Desktop\DiscordBot\index.js:14:1)
at Client.emit (node:events:390:28)
at MessageCreateAction.handle (C:\Users\Diego M\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:33:18)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Diego M\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\Diego M\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31)
at WebSocketShard.onPacket (C:\Users\Diego M\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\Diego M\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (C:\Users\Diego M\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:390:28)
at Receiver.receiverOnMessage (C:\Users\Diego M\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:1022:20)
const {
Client,
Intents
} = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
const prefix = '!';
client.on("ready", () => {
console.log("Im Ready!");
});
client.on('message', messageCreate => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
}
});
client.login("Token");
You are getting the error because you are not providing the proper event and the parameter to the function. 'messageCreate' is an event which emits whenever a message is created and followed by a parameter. Have a look here
The code should look like this:
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const prefix = '!';
client.on('ready', () => {
console.log('Im Ready!');
});
client.on('messageCreate', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
}
});
client.login('Token');