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

如何使用discord.js获取用户当前语音频道(适配交互场景)

解决方法
  • 在Discord.js v13及以上版本中,旧版本的voiceChannel属性已被移除,需使用interaction.member.voice.channel获取用户当前所在的语音频道。
  • 必须先判断用户是否处于语音频道内,避免代码报错。

可运行的完整代码示例:

async execute(interaction) {
  // 校验用户是否在语音频道中
  if (!interaction.member.voice.channel) {
    return interaction.reply('请先加入一个语音频道!');
  }

  const connection = joinVoiceChannel({
    channelId: interaction.member.voice.channel.id,
    guildId: interaction.guild.id,
    adapterCreator: interaction.guild.voiceAdapterCreator,
  });
}
  • 注意:确保你的Discord.js版本为v13或更高,joinVoiceChannel是该版本新增的语音连接方法。
  • 机器人需要拥有目标语音频道的CONNECT权限才能成功加入。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 17:40:53