Search code examples
javascriptnode.jsdiscorddiscord.js

Why is my messageCreate event not working? (discord.js)


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 command 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 command is not working!


Solution

  • There are 2 reasons your bot isn't responding to you:

    1. Your bot doesn't have 'MessageContent' intent
    const client = new Client({ intents: ['Guilds', 'GuildMessages', 'MessageContent'] });
    
    1. client.on('message'... may result to a DeprecationWarning
      Here is the correction:
    client.on('messageCreate', (message) => {
        if (message.content === 'ping'){
            message.reply('Pong!')
        }
    });