Discord Bot - 无法从 Intents 访问 FLAGS

Discord Bot - Cannot access FLAGS from Intents

我开始编写个人机器人程序。我遇到错误

TypeError: Cannot read properties of undefined (reading 'FLAGS')

我假设它无法访问 FLAGS。我不明白为什么,因为我已经安装了最新的节点和 discord.js。我在 Discord Developer Portal 上的个人帐户上为我的机器人允许了 'Privileged Gateway Intents' 的所有权限。我遵循了本教程:https://www.youtube.com/watch?v=Qc9uPgGmQ7I

到目前为止,这是我的代码:

require("dotenv").config();
const { Client, Intents } = require("discord.js")
const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES
  ]
});

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

client.on("interactionCreate", async interaction => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === "ping") {
    await interaction.reply("Pong!");
  }
});

client.login(process.env.SPELL_BOT_TOKEN)

这意味着您使用的是 Discord.js v11 或更早版本,而意图并不存在,因此您可以将其更新到 v12 或更新版本:

  • 使用 npm uninstall discord.jsnpm install discord.js 重新安装模块。
  • 使用 npm install discord.js@dev 将其更新到最新版本或使用 npm install discord.js@13.3.1.
  • 更新到特定版本
  • 编辑 package.json 文件中的模块版本。

我看到的第一个错误是您必须将对象传递给客户端 class,您做到了 new Client(intents: [...]})。应该像 new Client({ intents: [...] })