如何修复discord.js中'Discord.Intents.FLAGS读取未定义'错误?
问题解决:Discord.Intents.FLAGS 未定义的 TypeError
原因分析
你遇到的问题核心是discord.js版本不匹配:参考的YouTube教程用的是v13或更早版本,而你当前安装的是v14+版本。v14彻底重构了Intents的调用逻辑,Discord.Intents.FLAGS写法已被移除,因此触发“无法读取undefined的FLAGS属性”错误。
两种解决方法
方法1:适配v14+版本修改代码
将代码中的Intents调用替换为v14的新写法,同时注意其他语法变更:
// 单独导入v14所需的核心模块 const { Client, IntentsBitField, ActivityType } = require("discord.js"); const config = require("./config.json"); const client = new Client({ restTimeOffset: 0, allowedMentions: { parse: [], repliedUser: false, }, partials: [ "MESSAGE", "CHANNEL", "REACTION" ], // 改用IntentsBitField.Flags,注意首字母为驼峰格式(Guilds而非GUILDS) intents: [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, ], presence: { // v14中活动项改为数组形式(activities而非activity) activities: [{ name: `Private Bot`, type: ActivityType.Watching, }], status: "online", }, });
也可直接使用Intents对应的数值(简洁但可读性稍差):
intents: [ 1 << 0, // 对应Guilds权限 1 << 9, // 对应GuildMessages权限 ],
方法2:回退到教程对应的v13版本
若不想修改代码,可直接卸载当前discord.js,安装v13.x稳定版:
npm uninstall discord.js npm install discord.js@13.17.1
安装完成后,你原有的代码即可和教程保持一致并正常运行。
内容的提问来源于stack exchange,提问作者Aranaywa
相关产品推荐
相关产品推荐

