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

Discord机器人报错TypeError [ClientMissingIntents]求助

解决Discord.js机器人缺少合法Intents的问题

我正在开发一款Discord机器人,功能是接收音频流URL并将其输出至语音频道,但运行时出现如下错误:

TypeError [ClientMissingIntents]: Valid intents must be provided for the Client. at Client._validateOptions (/home/runner/LaFiestaMalisimaBot/node_modules/discord.js/src/client/Client.js:489:13) at new Client (/home/runner/LaFiestaMalisimaBot/node_modules/discord.js/src/client/Client.js:78:10) at Object.<anonymous> (/home/runner/LaFiestaMalisimaBot/index.js:2:16) at Module._compile (node:internal/modules/cjs/loader:1105:14)

我的代码如下:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
    console.log('ready to slay')
const player = createAudioPlayer()
  const stream = ("linkhere", { filter: 'audioonly' })
const connection = joinVoiceChannel({
    channelId: "channelidhere",
    guildId: "guildidhere",
    adapterCreator: channel.guild.voiceAdapterCreator,
})
connection.subscribe(player)

player.play("urlhere")});

client.login(resource);

解决步骤

1. 补充客户端Intents配置

Discord.js v14及以上版本强制要求声明客户端Intents,用来指定机器人需要接收的事件数据类型。对于语音播放机器人,至少需要Guilds和GuildVoiceStates两个Intents:

修改客户端初始化代码:

const Discord = require('discord.js');
const { IntentsBitField } = require('discord.js');

// 创建客户端时传入Intents配置
const client = new Discord.Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildVoiceStates
  ]
});

2. 修复代码中的其他问题

你的代码还有几处语法和依赖问题,一起修正才能正常运行:

  • 缺少语音工具的导入(createAudioPlayer、joinVoiceChannel等来自@discordjs/voice库)
  • stream变量写法错误,需用createAudioResource生成音频资源
  • channel变量未定义,需通过ID获取语音频道对象

完整修正后的示例代码:

const Discord = require('discord.js');
const { IntentsBitField } = require('discord.js');
// 导入语音相关工具
const { createAudioPlayer, createAudioResource, joinVoiceChannel } = require('@discordjs/voice');

const client = new Discord.Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildVoiceStates
  ]
});

client.on('ready', async () => {
  console.log('ready to slay');
  
  // 获取目标服务器和语音频道
  const guild = client.guilds.cache.get('guildidhere');
  const channel = guild.channels.cache.get('channelidhere');
  
  // 创建语音连接
  const connection = joinVoiceChannel({
    channelId: channel.id,
    guildId: guild.id,
    adapterCreator: guild.voiceAdapterCreator,
  });
  
  // 创建音频播放器和资源
  const player = createAudioPlayer();
  // 替换为你的音频流URL
  const resource = createAudioResource('your-audio-stream-url', { filter: 'audioonly' });
  
  // 订阅播放器并开始播放
  connection.subscribe(player);
  player.play(resource);
});

// 替换为你的机器人Token
client.login('your-bot-token');

3. 启用特权Intents(按需)

如果你的机器人需要接收成员状态、消息内容等敏感事件,需前往Discord开发者门户的机器人页面,找到「Privileged Gateway Intents」区域,启用对应的Intents(如Server Members Intent、Message Content Intent)。纯语音播放场景无需额外启用。


内容的提问来源于stack exchange,提问作者Malísima

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 15:01:11