简体   繁体   中英

I am trying do deploy discord bot in javascript on replit but its just replying only when i mention bot instead of any normal message

I have developed a discord bot in python but getting problems in javascript one.

I have tried a few solutions from StackOverflow itself but I can't figure out how to make it work. And much confusion due to solutions available online about discord bot and whenever you try to deploy it as it shows many version/deprecation errors of nodejs and discord.

Problem is that my bot code as follows replying only when I mention them but I want it to reply on any message like for example I take "boomer" as my message but the bot doesn't respond to it at all.

const Discord = require("discord.js");
const { Client } = require('discord.js');

const client = new Discord.Client({
  intents: [
    32509
  ]
})
client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`);
  client.user.setActivity('Running a test, hopefully.');
})

client.on("messageCreate", (message) => {
  if (message.content.toLowerCase().includes("boomer")) {
    message.channel.send("how are you there!");
  }

  if (message.author.bot) return false;

  if (message.content.includes("@here") || message.content.includes("@everyone") || message.type == "REPLY") return false;

  if (message.mentions.has(client.user.id)) {
    message.channel.send("Hello there!");
  }

});

client.login(process.env.TOKEN)

With your current intent 32509 the following ones are enabled:

  • GUILDS (1 << 0)
  • GUILD_BANS (1 << 2)
  • GUILD_EMOJIS_AND_STICKERS (1 << 3)
  • GUILD_INTEGRATIONS (1 << 4)
  • GUILD_WEBHOOKS (1 << 5)
  • GUILD_INVITES (1 << 6)
  • GUILD_VOICE_STATES (1 << 7)
  • GUILD_MESSAGES (1 << 9)
  • GUILD_MESSAGE_REACTIONS (1 << 10)
  • GUILD_MESSAGE_TYPING (1 << 11)
  • DIRECT_MESSAGES (1 << 12)
  • DIRECT_MESSAGE_REACTIONS (1 << 13)
  • DIRECT_MESSAGE_TYPING (1 << 14)

The problem is you've added lots of unnecessary ones but missing the MESSAGE_CONTENT intent. It means message.content doesn't have any value , but message.mentions has. That's why it works when you mention the bot and doesn't work when you check if the message.content includes the string "boomer" .

To solve this, you can add 32768 ( 1 << 15 ) to the current number:

const client = new Client({
  intents: [65277],
});

or even better, just use the ones you actually need:

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});

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