如何在Discord.js v14版本中为机器人添加所有Intents?
Discord.js 启用所有Intents的正确方式
直接用固定数字(比如你代码里的7796)设置Intents很容易出问题,因为Discord的Intents列表会更新,数字对应的权限可能不全或过时。下面是两种可靠的设置方法:
方法一:使用枚举值组合(推荐)
这种方式清晰直观,能确保你启用所有可用的Intents,包括特权Intents。需要注意的是,部分特权Intents需要在Discord开发者后台手动开启。
代码示例(Discord.js v14+)
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 ] });
代码示例(Discord.js v13)
v13版本使用Intents.FLAGS代替GatewayIntentBits:
const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_BANS, Intents.FLAGS.GUILD_EMOJIS, Intents.FLAGS.GUILD_INTEGRATIONS, Intents.FLAGS.GUILD_WEBHOOKS, Intents.FLAGS.GUILD_INVITES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MESSAGE_TYPING, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGE_TYPING, Intents.FLAGS.MESSAGE_CONTENT ] });
方法二:使用全权限数值(不推荐)
如果你一定要用数字,可以计算所有Intents的数值总和,但要注意Discord新增Intents后这个数值会失效。当前全Intents(包括特权)的数值是65535,但前提是你已经在开发者后台开启了特权Intents。
代码示例:
const { Client } = require('discord.js'); const client = new Client({ intents: 65535 });
关键注意事项
- 特权Intents必须手动开启:
GuildMembers、GuildPresences、MessageContent这三个是特权Intents,需要登录Discord开发者平台,进入你的Bot页面,找到「Privileged Gateway Intents」板块,开启对应的开关,否则客户端会无法获取相关数据或直接报错。 - 使用对应版本的Discord.js:v13及以上版本才需要设置Intents,确保你的依赖包版本正确。
内容的提问来源于stack exchange,提问作者izzzzthewiz
相关产品推荐
相关产品推荐

