如何修复Discord.js开发中出现的CLIENT_MISSING_INTENTS错误
问题原因
你声明的 const client = new Client({ intents: [...] }) 是完全未被使用的独立变量,你自定义的EconomyClient继承Client类时,构造函数调用super()没有传入任何intents配置,导致父类Client初始化时缺少必填的意图参数,触发报错。
修复方案
修改你的client.js代码如下:
const { Client, Intents, Collection } = require('discord.js'); // 删除原来无用的const client = new Client(...)代码行 class EconomyClient extends Client { constructor() { // 给父类构造函数传入intents配置 super({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); this.discord = require('discord.js'); this.fs = require('fs'); this.path = require('path'); this.ms = require('ms'); this.mongoose = require('mongoose'); this.commands = new Collection(); this.timeouts = new Collection(); this.config = { prefix: 'h!', } } commandHandler(path) { this.fs.readdirSync(this.path.normalize(path)).map((f) => { const File = require(this.path.join(__dirname, '..', path, f)); this.commands.set(File.name, File); }); }; start(token, path) { this.commandHandler(path); this.login(token); this.mongoose.connect('mongodb+srv://dbUser:@cluster0.awhb3.mongodb.net/Data?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true }); this.mongoose.connection.on('connected', () => console.log("DB connected")); } }; module.exports = EconomyClient
额外注意事项
- 如果你使用的是discord.js v14及以上版本,intents的写法需要调整为:
const { Client, GatewayIntentBits, Collection } = require('discord.js'); // super里的配置替换为以下内容 super({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
- 涉及读取消息内容的功能,需要提前到Discord开发者后台的Bot设置页,开启
Message Content Intent权限。
内容的提问来源于stack exchange,提问作者oktaywhite
相关产品推荐
相关产品推荐

