You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js中CLIENT_MISSING_INTENTS错误排查:未知缺失Intents问题咨询

解决Discord.js中的CLIENT_MISSING_INTENTS错误

嘿,我一眼就看出问题出在哪了——你在apps命令的执行函数里重复创建了一个全新的Discord Client实例,这完全是画蛇添足,而且正是导致这个错误的核心原因!

为什么会报错?

你的主机器人已经在index.js里初始化了一个Client,所有命令都应该依赖这个已启动的客户端来运行。在命令里重新创建Client不仅会浪费资源,还会因为这个新实例的配置逻辑冲突触发Intents错误——哪怕你写了Intents,这种重复初始化的做法本身就不符合Discord.js的使用规范。

修复步骤

1. 删掉命令里多余的Client创建代码

把这段完全没必要的代码从execute函数里移除:

const { Client, Intents, Message, Collection} = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES], partials: ['MESSAGE', 'CHANNEL'] });
client.commands = new Collection();

2. 让命令使用主客户端实例

你的命令的execute函数需要接收主客户端作为参数。修改命令模块的导出结构:

module.exports = {
 name: 'apps',
 description: 'Apply for staff team',
 // 添加client参数,接收主客户端实例
 async execute(message, args, Discord, client) { 
 const setup = require('../setup');
 const guild = client.guilds.cache.get(setup.GuildID())
 let channelID = setup.AppChannel();
 message.channel.send(`${message.author}, check your dm!`);
 console.log(`New application has started by --> ${message.author.tag}`);
 try {
 setTimeout(embed, 8000)
 console.log(`sending information embed to ${message.author.tag}`)
 function embed() {
 const embed = new Discord.MessageEmbed()
 .setTitle('APPLICATION')
 .setFooter('Made by Navid_16#6966')
 .setColor('RANDOM')
 .addFields(
 { name: 'First Name', value: 'Your name' },
 { name: 'Last Name', value: 'surname' },
 { name: 'How old are you', value: 'Your age' },
 { name: 'Your email', value: 'example@example.com' },
 { name: 'What is your time zone', value: 'GMT+2' },
 { name: 'Where are you from', value: 'Your country' },
 { name: 'What languages are you speaking', value: 'englinsh' },
 { name: 'Have your ever been punished on our server', value: 'did you ever get ban,mute,etc' },
 { name: 'On which server your most active on', value: 'what mode do you play most' },
 { name: 'Have you ever been in a staff team', value: 'do you have any experience' },
 { name: 'Tell us about your self', value: 'tell us something you would like to know about you' }
 )
 message.author.send({ embeds: [embed] })
 }
 } catch (err) {
 console.log(err)
 }
 }
}

然后去你的主文件index.js里,确保调用命令时把主客户端传进去:

client.on('messageCreate', async message => {
 // ... 其他逻辑(比如解析命令名和参数)
 const command = client.commands.get(commandName);
 if (command) {
  try {
   // 将client作为第四个参数传递给execute函数
   await command.execute(message, args, Discord, client); 
  } catch (error) {
   console.error(error);
   message.reply('There was an error executing that command!');
  }
 }
});

3. 验证主客户端的Intents配置

最后检查index.js里的主客户端初始化代码,确保它包含了必要的Intents:

const { Client, Intents } = require('discord.js');
const client = new Client({
 intents: [
  Intents.FLAGS.GUILDS,
  Intents.FLAGS.GUILD_MESSAGES,
  Intents.FLAGS.DIRECT_MESSAGES
 ],
 partials: ['MESSAGE', 'CHANNEL'] // 处理私信需要配置此项
});

额外提醒

  • 永远不要在命令模块里创建新的Client实例,所有命令都应该共享同一个主客户端,这是Discord.js开发的基本规范。
  • 如果之后需要用到特权Intents(比如获取成员列表、在线状态),记得去Discord开发者门户的机器人页面开启对应的开关。

内容的提问来源于stack exchange,提问作者Navid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:42:42