1

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!

Ravioli
  • 33
  • 1
  • 6
  • Dont you need to use it like `messageCreate.content` and `messageCreate.reply` since you write `messageCreate` on `client.on` function? – Batu.Khan Aug 15 '22 at 06:05
  • In the `client.on('messageCreate')` event listener, you have named the message you get as `messageCreate`. Then you are trying to reply to a variable which doesn't exist which is `message`. So you just have to change that – Caladan Aug 15 '22 at 06:29
  • Voted to close as it's typo: `client.on('message', (messageCreate)` should be `client.on('messageCreate', (message) =`. You'll also need [the `GatewayIntentBits.MessageContent` intent](https://stackoverflow.com/questions/73036854/message-content-doesnt-have-any-value-in-discord-js-v14/73037243#73037243). – Zsolt Meszaros Aug 15 '22 at 12:17
  • @Batu.Khan I did that.. still not working! – Ravioli Aug 15 '22 at 14:34
  • @Caladan still not working – Ravioli Aug 15 '22 at 14:35
  • Instead of the `GuildMessages` intent in the client, use `GatewayIntentBits.GuildMessages` – Caladan Aug 15 '22 at 15:09

3 Answers3

4

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!')
    }
});
KNguyen
  • 119
  • 1
  • 7
3
  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);
Juno
  • 211
  • 5
  • 17
0

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"] });
IDcLuc
  • 96
  • 8