我不擅长使用javascript,但我一直在尝试获取所有语音渠道的用户数。例如:如果两个用户在“语音通道1”中,1个用户在“语音通道2”中,我想在控制台中打印数字3,这是语音通道中的所有用户。
var Count;
for(Count in bot.users.array()){
var User = bot.users.array()[Count];
console.log(User.username);
}
此代码打印控制台中的所有成员(在线/离线)名称,但我不知道如何获取语音通道中的唯一用户数量。
发布于 2019-06-18 03:23:21
您可以过滤(Collection.filter()
)公会中的所有通道(Guild.channels
)以检索仅包含语音通道的Collection。然后,可以遍历每个通道,并将连接到该通道的成员数添加到计数中。
// Assuming 'newMember' is the second parameter of the event.
const voiceChannels = newMember.guild.channels.filter(c => c.type === 'voice');
let count = 0;
for (const [id, voiceChannel] of voiceChannels) count += voiceChannel.members.size;
console.log(count);
发布于 2020-06-02 17:46:11
如果你不想在事件中使用它,而是在嵌入、消息等中使用它,这是我的解决方案。我使用了@slothiful的解决方案,但做了一点改动。
// I used my "message" property. You can change it with yours.
const voiceChannels = message.guild.channels.cache.filter(c => c.type === 'voice');
let count = 0;
for (const [id, voiceChannel] of voiceChannels) count += voiceChannel.members.size;
message.channel.send(count);
https://stackoverflow.com/questions/56641273
复制