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

Discord.js v13.1.0 Bot无法加入语音频道,join/leave方法不存在如何解决?

问题原因

Discord.js v13版本全面重构了语音相关功能,原本挂载在GuildVoiceChannel实例上的join()、leave()方法已被完全移除,所有语音操作都需要通过独立安装的@discordjs/voice包提供的API实现,这就是你调用对应方法报错"is not a function"的核心原因。

前置检查
  • 确保已安装全部必要依赖:
npm install discord.js@13.1.0 @discordjs/voice @discordjs/opus libsodium-wrappers
  • 确保初始化Client时已开启GUILD_VOICE_STATES意图,否则无法正常获取语音频道相关状态:
const { Client, Intents } = require('discord.js');
const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_VOICE_STATES // 必须开启
  ]
});
可运行示例代码

首先导入@discordjs/voice的必要方法:

const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');

指令实现逻辑:

client.on('messageCreate', async message => {
  // 忽略机器人消息
  if (message.author.bot) return;

  // 加入语音频道指令 !join
  if (message.content === '!join') {
    const voice_channel = message.member.voice.channel;
    // 先判断用户是否在语音频道
    if (!voice_channel) {
      return message.reply('你需要先加入一个语音频道');
    }

    // 加入语音频道的正确写法
    const connection = joinVoiceChannel({
      channelId: voice_channel.id,
      guildId: voice_channel.guild.id,
      adapterCreator: voice_channel.guild.voiceAdapterCreator
    });

    message.reply(`已成功加入语音频道 ${voice_channel.name}`);
  }

  // 离开语音频道指令 !leave
  if (message.content === '!leave') {
    const guildId = message.guild.id;
    // 获取当前服务器的语音连接
    const connection = getVoiceConnection(guildId);
    if (connection) {
      // 销毁连接即可离开频道
      connection.destroy();
      message.reply('已离开语音频道');
    } else {
      message.reply('我当前没有加入任何语音频道');
    }
  }
});

client.login('你的机器人Token');
注意事项
  • 请确保机器人在对应语音频道所在服务器拥有连接、说话权限,否则会加入失败
  • 若需要播放音频,可以使用@discordjs/voice提供的createAudioPlayer、createAudioResource方法实现播放逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 04:27:03