-1

Recently, I have made a Discord bot using node.js and VS Code. I can see my bot being online. However, it does not respond to my messages. (The bot has the required permissions.)
I could not understand the problem, I would be so delighted if you gave me a hand.

Here is my bot.js code

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const config = require("./config.json");


client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message' , message => {
    if(message === 'ping') {
        message.channel.send('Pong!');
    }
})

client.login(config.token);

Here is my config.json code

{
 "token": "I wrote my token in here"
}
gulinte
  • 3
  • 2
  • Add `console.log(message)` to your `client.on('message' , message => {` function and see what you got – Konrad Aug 26 '22 at 19:56
  • 1
    You have to include the Guild Messages intent in your client's intents – Elitezen Aug 26 '22 at 19:58
  • I have taken a look at the documentation (https://discordjs.guide/popular-topics/intents.html#the-intents-bitfield) and tried to add Guild intents. I ended up like this : const { Client, IntentsBitField } = require('discord.js'); const myIntents = new IntentsBitField(); myIntents.add(IntentsBitField.Flags.GuildPresences, IntentsBitField.Flags.GuildMembers); const client = new Client({ intents: myIntents }); Unfortunately , it did not work. Anyways, thank you all for help :D – gulinte Aug 26 '22 at 20:29
  • How about adding both GuildMessages and MessageContent? `const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });` – Crytek1012 Aug 26 '22 at 20:37
  • 1
    I had added both GuildMessages and MessageContent. I had taken a look at @ZsoltMeszaros 's link and I noticed something. I should have written `client.on('messageCreate'` instead of writing `client.on('message' `. Thanks, it is working now :D – gulinte Aug 31 '22 at 10:03

1 Answers1

0

Your code dosent work because your not checking for message.content which holds the message's content.

It should be somthing like this:

client.on('message' , message => {
    if(message.content === 'ping') {
        message.channel.send('Pong!');
    }
})
Kingerious
  • 16
  • 3
  • Thanks for the help, I solved the problem by writing `client.on('messageCreate'` (instead of `client.on('message'` . And also I added message.content, like you said. – gulinte Aug 31 '22 at 09:58