Discord Bot运行报错:TypeError: Cannot read properties of undefined (reading 'FLAGS')
解决Discord Bot的
Intents.FLAGS未定义报错 原因分析
这个错误是因为你使用的discord.js v14+版本已移除Intents.FLAGS写法,改用GatewayIntentBits枚举定义机器人意图。
解决方案
1. 确认discord.js版本
先检查当前安装的discord.js版本:
npm list discord.js
若版本为v14及以上,按以下步骤修改代码;若为旧版本,需检查导入逻辑是否有误。
2. 修改意图的导入与使用方式
如果你的原有代码是这样的:
const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, // 其他意图配置 ] });
直接替换为v14兼容写法:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, // 其他意图示例:GatewayIntentBits.GuildMessages、GatewayIntentBits.GuildMembers ] });
3. 适配旧版本(可选)
如果你的代码是基于v13或更早版本开发,想保留Intents.FLAGS写法,可安装对应稳定版本:
npm install discord.js@13.17.1
但不建议长期使用旧版本,官方已停止维护,存在安全风险。
4. 检查导入正确性
确保未拼写错误GatewayIntentBits,且确实从discord.js模块中导入了该对象。
验证修改
修改完成后重新运行机器人:
node index.js
若仍有问题,检查是否遗漏必要的权限意图,或机器人Token配置是否正确。
内容的提问来源于stack exchange,提问作者Motofo20
相关产品推荐
相关产品推荐

