简体   繁体   中英

discord bot when connected is not responding according to my code

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")

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM