1

I am building a Discord bot and I want my bot to respond pong after I type ping but it's not responding. My token is also correct. My code gets connected with bot but there is no response.

const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { Intents } = Discord;

const client = new Discord.Client({ 
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] 
})

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

client.on("message", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
})

client.login("Its correct but I cant reveal")
node_modules
  • 4,790
  • 6
  • 21
  • 37
  • Make sure that the bot has the necessary permissions to send messages in the Discord server where you are trying to use the "ping" command. OR Try using a different Discord server or channel to see if the problem persists. This can help you determine whether the issue is specific to a particular server or channel. – Hamza Manan Dec 21 '22 at 07:22
  • it has administrator rights – Mudit Dagar Dec 21 '22 at 07:23
  • I already tries – Mudit Dagar Dec 21 '22 at 07:25

1 Answers1

2

You also need the MessageContent and GuildMembers intent to be enabled when you want to read the message contents in your messageCreate event.

const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Discord.Client({ 
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});

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

client.on("messageCreate", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
});

client.login("Your token");

Also, double check on your developer portal if the intents are enabled.

! Not sure which version of Discord.js you are using, but the event for receiving message is messageCreate. The event message is deprecated and has been removed in the latest version already.

enter image description here

node_modules
  • 4,790
  • 6
  • 21
  • 37
  • ok but after I added these intents then also same issue is coming – Mudit Dagar Dec 21 '22 at 07:22
  • What are the error(s) you get? – node_modules Dec 21 '22 at 07:23
  • Process exited with code 1 C:\Program Files\nodejs\node.exe .\src\App.js (node:23832) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification. (Use `node --trace-warnings ...` to show where the warning was created) Logged in as X_Mouse#3250! ipt Uncaught ReferenceError ReferenceError: ipt is not defined at (undefined:1:1) – Mudit Dagar Dec 21 '22 at 07:24
  • this error in vs code – Mudit Dagar Dec 21 '22 at 07:24
  • Seems like your network is insecure, because with the same exact code I've posted in my answer, it should work. https://stackoverflow.com/questions/65021836/node26972-warning-setting-the-node-tls-reject-unauthorized-environment-varia – node_modules Dec 21 '22 at 07:28
  • network is secure – Mudit Dagar Dec 21 '22 at 07:29
  • can you send me complete correct code – Mudit Dagar Dec 21 '22 at 07:31
  • @MuditDagar Complete code is in my answer, refresh the page to be sure. Which version of Discord are you using? – node_modules Dec 21 '22 at 07:32
  • yes refreshed . I am on the latest version – Mudit Dagar Dec 21 '22 at 07:36
  • Uncaught DiscordjsError Error [DisallowedIntents]: Privileged intent provided is not enabled or whitelisted. at createShards (c:\Users\mudit\Desktop\react js\my-app\node_modules\discord.js\src\client\websocket\WebSocketManager.js:250:15) No debugger available, can not send 'variables' – Mudit Dagar Dec 21 '22 at 07:42
  • new error is coming – Mudit Dagar Dec 21 '22 at 07:42
  • @MuditDagar I wrote in my answer to double check your [developer portal](https://discord.com/developers) and enable any intents on that page. If you copied the exact same code in my answer, the error shouldn't occur, because I have tested it myself. Open your `package.json` and give me the exact version of Discord.js – node_modules Dec 21 '22 at 07:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250572/discussion-between-node-modules-and-mudit-dagar). – node_modules Dec 21 '22 at 07:56