discord.js报[BITFIELD_INVALID]无效位域标志错误如何解决
报错原因
[BITFIELD_INVALID]: Invalid bitfield flag or number: undefined 报错的直接触发原因是,传入Client初始化配置的intents数组包含值为undefined的项,BitField模块无法解析undefined类型的权限标记,直接抛出异常。
对应给出的代码,问题出在第三个intent的写法错误:前两个intent均正确读取Intents.FLAGS下的枚举属性,第三个写为Intents.GUILD_VOICE_STATES,漏写了.FLAGS层级。discord.js的所有intent枚举值都挂载在Intents.FLAGS对象下,直接从Intents根对象读取GUILD_VOICE_STATES得到的返回值就是undefined。
修复方法
根据你使用的discord.js版本选择对应修复方案:
- 若使用discord.js v13版本,仅需修正第三个intent的拼写,补上
.FLAGS层级即可,修正后代码如下:
const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES ] });
- 若使用discord.js v14版本,Intents的导出规则有调整,不再使用
Intents.FLAGS挂载枚举值,需要改用GatewayIntentBits取值,对应代码如下:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates ] });
补充说明:代码里声明的intents如果涉及特权权限(比如消息内容读取、服务器成员列表读取),需要提前到Discord开发者后台对应机器人的Bot配置页,手动开启对应Privileged Gateway Intents开关,否则机器人启动后会出现权限不足、无法接收对应事件的问题。
内容的提问来源于stack exchange,提问作者Siddharth Sinha
相关产品推荐
相关产品推荐

