Ravioli
Ravioli

Reputation: 33

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!

Upvotes: 1

Views: 7356

Answers (4)

Saul Lee
Saul Lee

Reputation: 149

Following above solutions initially gave me this error:

enter image description here

This error was solved by having the 'message content intent' toggled on in the 'Bot' section of the developer portal:

enter image description here

Upvotes: 0

Juno
Juno

Reputation: 299

  1. You need to use the following intents to read and react to messages:
{ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }
  1. 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);

Upvotes: 3

KNguyen
KNguyen

Reputation: 139

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!')
    }
});

Upvotes: 6

IDcLuc
IDcLuc

Reputation: 96

2 reasons this is happening;

  1. You put message instead of messageCreate and vice versa in your client.on(). Put this instead:
client.on('messageCreate', message => {
    if (message.content === 'ping'){
        message.reply('Pong!')
    }
});
  1. You're missing the messageContent intent.
const client = new Client({ intents: ["Guilds", "GuildMessages", "MessageContent"] });

Upvotes: 0

Related Questions