简体   繁体   English

我正在尝试在重装时在 javascript 中部署 discord 机器人,但它只是在我提到机器人而不是任何正常消息时才回复

[英]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.我在 python 中开发了一个 discord 机器人,但在 javascript 中遇到了问题。

I have tried a few solutions from StackOverflow itself but I can't figure out how to make it work.我已经尝试了 StackOverflow 本身的一些解决方案,但我不知道如何使它工作。 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.由于在线提供了有关 discord bot 的解决方案以及每当您尝试部署它时,由于它显示 nodejs 和 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.问题是我的机器人代码如下仅在我提及它们时回复,但我希望它回复任何消息,例如我将"boomer"作为我的消息,但机器人根本不回复它。

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:根据您当前的意图32509 ,启用了以下意图:

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

The problem is you've added lots of unnecessary ones but missing the MESSAGE_CONTENT intent.问题是您添加了许多不必要的内容,但缺少MESSAGE_CONTENT意图。 It means message.content doesn't have any value , but message.mentions has.这意味着message.content没有任何 value ,但message.mentions有。 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" .这就是为什么当你提到机器人时它起作用,而当你检查message.content是否包含字符串"boomer"时它不起作用。

To solve this, you can add 32768 ( 1 << 15 ) to the current number:要解决此问题,您可以将 32768 ( 1 << 15 ) 添加到当前数字:

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,
  ],
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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