如何统计服务器所有语音通道的所有人

How to count all people in all voice channel of server

我正在使用打字稿并且我使用了这段代码:但它不起作用

let count = 0;
const userCount = client.guilds.cache.get("MY Server_ID")?.channels.cache.filter(ch => ch.type === "GUILD_VOICE").map((ch) => count  +=  ch.members.size);

错误:

Property 'size' does not exist on type 'Collection<string, GuildMember> | ThreadMemberManager'.
  Property 'size' does not exist on type 'ThreadMemberManager'.

文档说 ch.members 有 said 但它给出了一个错误

https://discord.js.org/#/docs/main/stable/class/GuildChannel?scrollTo=members https://discord.js.org/#/docs/collection/main/class/Collection

你可以使用 .isVoice() 类型保护和 Array#reduce()

let channels = client.guilds.cache.get("MY Server_ID")?.channels.cache.filter(ch => ch.type === "GUILD_VOICE")
let userCount = channels.reduce((a, c) => {
  if (!c.isVoice()) return;
  return a + c.members.size
}, 0) // should be member count in all channels

但是,检查 VC 中人数的更好方法是过滤成员

await client.guilds.cache.get("MY Server_ID")?.members.fetch()
let userCount = client.guilds.cache.get("MY Server_ID")?.members.cache.filter(m => m.voice.channel).size