运行Discord Bot时遇“Valid intents must be provided for the Client”错误求修复
解决Discord机器人"Valid intents must be provided for the Client"错误
这个错误是因为Discord.js v14及以上版本强制要求显式声明机器人需要的网关意图(Intents),你的代码创建Client实例时未配置任何意图,导致验证失败。按以下步骤修复:
1. 在Discord开发者门户开启对应意图
登录Discord开发者门户,进入你的机器人应用:
- 切换到Bot标签页
- 找到Privileged Gateway Intents区域
- 开启以下三个意图:
- Guilds
- Guild Messages
- Message Content Intent(必须开启,因为你的代码需要读取消息内容)
2. 修改代码添加Intents配置
更新代码,引入IntentsBitField并在创建Client时指定需要的意图,同时修正模板字符串和事件名称的语法问题:
const { Client, IntentsBitField } = require("discord.js"); // 声明需要的意图 const intents = new IntentsBitField() .add(IntentsBitField.Flags.Guilds) .add(IntentsBitField.Flags.GuildMessages) .add(IntentsBitField.Flags.MessageContent); const client = new Client({ intents: intents }); const TOKEN = "你的机器人TOKEN"; // 替换成实际TOKEN client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); }); client.on("messageCreate", msg => { // v14+版本中message事件已改为messageCreate if (msg.content === "Facts about disneyland") { msg.reply("Before Disney chose Anaheim, he almost built his park on a seven-acre studio lot in Burbank. The meager playground would be called “Walt Disney’s America.” Fortunately for us all, his dreams grew quickly."); } }); client.login(TOKEN);
内容的提问来源于stack exchange,提问作者vtf6259
相关产品推荐
相关产品推荐

