DiscordJS开发报错:voiceChannel.join不是函数 机器人无法加入语音频道
问题原因
TypeError: voiceChannel.join is not a function 报错的核心原因是你使用的 discord.js 版本为 v13 及以上版本,该版本开始官方将语音相关功能从主库中拆分到独立的
@discordjs/voice包,原有VoiceChannel.join()方法已被正式移除,修改message.member.voice.channel属性的取值方式无法解决该问题。
解决方案
步骤1:安装必要依赖
首先安装语音功能相关的依赖包:
npm install @discordjs/voice libsodium-wrappers
同时你需要确认Discord开发者门户中,你的机器人已经开启Voice State Intent权限,且机器人被授予了目标语音频道的连接、说话权限。
步骤2:修改现有代码
将原有语音连接逻辑替换为@discordjs/voice提供的joinVoiceChannel方法即可,修改后的完整代码如下:
// 顶部导入语音包方法 const { joinVoiceChannel } = require('@discordjs/voice'); module.exports = { name: 'play', aliases: ['skip', 'stop'], description: 'YT music commands', async execute(message, args, cmd, client, Discord){ const voiceChannel = message.member.voice.channel; if(!voiceChannel) return message.channel.send('You need to join a voice channel to use this command.'); if(cmd === 'play'){ //Boring music bot searching song in YouTube stuff try{ // 替换原有join方法 const connection = joinVoiceChannel({ channelId: voiceChannel.id, guildId: voiceChannel.guild.id, adapterCreator: voiceChannel.guild.voiceAdapterCreator, }); queueConstructor.connection = connection; } catch(err){ queue.delete(message.guild.id); message.channel.send("There was an error connecting to voice channel."); throw err; } } } }
内容的提问来源于stack exchange,提问作者Vaishnav Sunil
相关产品推荐
相关产品推荐

