I tried making a discord bot..
I looked for some tutorials but my code doesn't seem to work..
I created a simple ping pong mand but for some reason its not working!
Heres my bot.js code:
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, 'GuildMessages'] });
client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});
client.on('message', (messageCreate) => {
if (message.content === 'ping'){
message.reply('Pong!')
}
});
client.login(process.env.TOKEN);
But the ping pong mand is not working!
I tried making a discord bot..
I looked for some tutorials but my code doesn't seem to work..
I created a simple ping pong mand but for some reason its not working!
Heres my bot.js code:
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, 'GuildMessages'] });
client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});
client.on('message', (messageCreate) => {
if (message.content === 'ping'){
message.reply('Pong!')
}
});
client.login(process.env.TOKEN);
But the ping pong mand is not working!
Share Improve this question asked Aug 15, 2022 at 5:34 RavioliRavioli 331 silver badge6 bronze badges 6-
Dont you need to use it like
messageCreate.content
andmessageCreate.reply
since you writemessageCreate
onclient.on
function? – Batu.Khan Commented Aug 15, 2022 at 6:05 -
In the
client.on('messageCreate')
event listener, you have named the message you get asmessageCreate
. Then you are trying to reply to a variable which doesn't exist which ismessage
. So you just have to change that – Caladan Commented Aug 15, 2022 at 6:29 -
Voted to close as it's typo:
client.on('message', (messageCreate)
should beclient.on('messageCreate', (message) =
. You'll also need theGatewayIntentBits.MessageContent
intent. – Zsolt Meszaros Commented Aug 15, 2022 at 12:17 - @Batu.Khan I did that.. still not working! – Ravioli Commented Aug 15, 2022 at 14:34
- @Caladan still not working – Ravioli Commented Aug 15, 2022 at 14:35
4 Answers
Reset to default 6There are 2 reasons your bot isn't responding to you:
- Your bot doesn't have 'MessageContent' intent
const client = new Client({ intents: ['Guilds', 'GuildMessages', 'MessageContent'] });
client.on('message'
... may result to a DeprecationWarning
Here is the correction:
client.on('messageCreate', (message) => {
if (message.content === 'ping'){
message.reply('Pong!')
}
});
- You need to use the following intents to read and react to messages:
{ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }
- The event you want to listen for is called "messageCreate" (you were listening for "message"):
client.on('messageCreate', (message) => {
if (message.content === 'ping'){
message.reply('Pong!')
}
});
This should work:
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});
client.on('messageCreate', (message) => {
if (message.content === 'ping'){
console.log("!")
message.reply('Pong!')
}
});
client.login(process.env.TOKEN);
2 reasons this is happening;
- You put
message
instead ofmessageCreate
and vice versa in yourclient.on()
. Put this instead:
client.on('messageCreate', message => {
if (message.content === 'ping'){
message.reply('Pong!')
}
});
- You're missing the messageContent intent.
const client = new Client({ intents: ["Guilds", "GuildMessages", "MessageContent"] });
Following above solutions initially gave me this error:
This error was solved by having the 'message content intent' toggled on in the 'Bot' section of the developer portal: