JavaScript编写的Discord音乐机器人无法加入语音频道问题求助
问题根因
该报错由discord.js版本不兼容导致:
你使用的代码是discord.js v11版本的写法,获取用户所在语音频道调用的属性为message.member.voiceChannel;但v12及之后的新版本discord.js已将该属性重构为message.member.voice.channel,旧写法读取时会始终返回undefined,因此即使用户已进入语音频道,判断条件也会触发报错。
修复步骤
1. 确认当前discord.js版本
在项目根目录执行以下命令查看版本:npm list discord.js
2. 对应版本修改代码
若使用v12/v13版本
- 将判断用户是否在语音频道的代码:
if(!message.member.voiceChannel){ message.channel.send("You must be in a voice channel to use the bot!"); return; }
修改为:
if(!message.member.voice.channel){ message.channel.send("You must be in a voice channel to use the bot!"); return; }
- 同步修改加入语音频道的调用代码:
if(!message.guild.voiceConnection) message.member.voice.channel.join().then(function(connection){ play(connection, message); })
若使用v14版本
除修改上述属性外,还需要额外完成两项配置:
- 机器人初始化代码中添加
Guilds、GuildVoiceStates两个网关意图 - 进入Discord开发者后台的机器人设置页面,开启「Privileged Gateway Intents」下的语音状态权限
3. 权限排查
确认机器人在当前服务器已被授予以下权限:
- 连接语音频道(Connect)
- 语音发言(Speak)
内容的提问来源于stack exchange,提问作者K.C.L.
相关产品推荐
相关产品推荐

