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

discord.js中GuildChannel无members属性,如何获取语音频道已连接用户列表?

问题原因与解决方法

1. 遗漏配置项

你缺少的核心配置是Discord 网关的GuildVoiceStates意图,discord.js v13及以上版本要求开发者主动声明需要的网关权限,未开启该意图的情况下无法正常获取语音频道的成员连接状态,也不会返回members相关属性。
另外如果你使用的是discord.js v14及以上版本,VoiceChannel实例的members属性已经被官方移除,无法直接从频道实例上读取成员列表。

2. 可行实现方案

第一步:开启必要网关意图

在客户端初始化时添加GuildVoiceStates意图,示例代码:

const { Client, GatewayIntentBits, ChannelType } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds, // 基础公会意图必填
    GatewayIntentBits.GuildVoiceStates, // 语音状态专属意图
    // 其余你业务需要的其他意图
  ]
});

第二步:获取指定语音频道已连接用户列表

你可以直接从公会的语音状态缓存中过滤对应频道的连接用户,该方法兼容所有discord.js v13+版本:

module.exports = async (client, oldState, newState) => {
  const TARGET_VOICE_CHANNEL_ID = '719030671384772609';
  // 拉取目标频道实例
  const targetChannel = await newState.guild.channels.fetch(TARGET_VOICE_CHANNEL_ID);
  // 校验频道存在且为语音频道
  if (!targetChannel || targetChannel.type !== ChannelType.GuildVoice) return;

  // 过滤出当前频道内的所有语音状态,映射为成员对象
  const connectedMembers = newState.guild.voiceStates.cache
    .filter(voiceState => voiceState.channelId === TARGET_VOICE_CHANNEL_ID)
    .map(voiceState => voiceState.member)
    .filter(Boolean); // 过滤掉无效的空成员数据

  // 可按需读取用户信息,示例打印所有用户的用户名+标签
  console.log('频道内已连接用户:', connectedMembers.map(member => member.user.tag));
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 11:27:05