You need to enable JavaScript to run this app. discord.js v11用户bot能否在私信中调用message.member.voiceChannel.join()?
优惠活动
大模型
产品
解决方案
定价
更多

discord.js v11用户bot能否在私信中调用message.member.voiceChannel.join()?

问题根因

你当前调用失败的核心原因是:message.member属性仅存在于服务器(Guild)上下文的消息对象中,私信(DM)场景下的消息不属于任何服务器,不存在服务器成员的对应属性,直接调用message.member会返回undefined,自然无法调用后续的语音加入方法。

可行实现方案

首先确认前置条件:你使用的是用户账号的token而非官方Bot账号的token,且初始化Client时已关闭Bot模式,初始化代码如下:
const client = new Discord.Client({ bot: false })

具体实现步骤如下:

  • 跳过私信消息的message.member属性,直接遍历客户端已缓存的所有服务器,匹配发信用户对应的服务器成员对象,再提取其所在的语音频道
  • 如果已经可以拿到目标语音频道的ID,也可以直接通过client.channels.get('语音频道ID')获取频道对象后调用加入方法

参考实现代码:

client.on('message', async message => {
  // 仅处理私信场景
  if (message.channel.type !== 'dm') return;
  let targetVoiceChannel = null;
  // 遍历所有已缓存服务器查找用户所在的语音频道
  client.guilds.forEach(guild => {
    const matchedMember = guild.members.get(message.author.id);
    if (matchedMember && matchedMember.voiceChannel) {
      targetVoiceChannel = matchedMember.voiceChannel;
    }
  })
  if (!targetVoiceChannel) return message.reply('未找到你当前所在的语音频道');
  // 加入语音频道
  try {
    const voiceConnection = await targetVoiceChannel.join();
    message.reply('已成功加入你所在的语音频道');
  } catch (err) {
    console.error('加入语音频道失败', err);
  }
})
注意事项
  • 你需要提前安装语音依赖包node-opus或opusscript,否则语音加入方法会抛出依赖缺失错误
  • 使用用户账号开发自动程序违反Discord官方服务条款,操作存在账号被封禁的风险

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 12:15:05