Discord.js 14如何沿用V13的32767全Intent配置?
Discord.js v14 开启全部意图解决方案
在v14中无法直接沿用v13的32767数值方式配置全意图,但可以通过以下两种方式实现:
方法一:手动枚举所有意图
将GatewayIntentBits下的所有枚举值添加到intents数组中,这种方式清晰可控,适合需要明确知道开启哪些意图的场景:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildBans, GatewayIntentBits.GuildEmojisAndStickers, GatewayIntentBits.GuildIntegrations, GatewayIntentBits.GuildWebhooks, GatewayIntentBits.GuildInvites, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMessageTyping, GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessageReactions, GatewayIntentBits.DirectMessageTyping, GatewayIntentBits.MessageContent ] });
方法二:自动合并所有意图值
通过Object.values获取所有GatewayIntentBits枚举值,再用按位或操作合并成一个数值,适合快速配置全意图的场景:
const { Client, GatewayIntentBits } = require('discord.js'); // 合并所有意图的数值 const allIntents = Object.values(GatewayIntentBits).reduce((total, bit) => total | bit, 0); const client = new Client({ intents: allIntents });
关键注意事项
- 特权意图需手动开启:
GuildMembers、GuildPresences、MessageContent属于特权意图,必须登录Discord开发者门户,进入你的机器人应用的「Bot」页面,找到「Privileged Gateway Intents」板块手动开启这些选项,否则机器人无法正常获取对应数据或启动失败。 - 按需开启更优:虽然全意图配置方便,但建议只开启机器人实际依赖的意图,减少不必要的数据传输,提升机器人性能和稳定性。
内容的提问来源于stack exchange,提问作者Finniedj
相关产品推荐
相关产品推荐

