【问题标题】:Discord bot get number of users in all channelsDiscord bot 获取所有渠道中的用户数
【发布时间】:2019-10-31 15:15:00
【问题描述】:

我不擅长使用 javascript,但我一直在尝试计算所有语音频道中的用户数。 例如:如果“语音通道 1”中有 2 个用户,“语音通道 2”中有 1 个用户,我想在控制台中打印数字 3,即语音通道中的所有用户。

var Count;
for(Count in bot.users.array()){
   var User = bot.users.array()[Count];
   console.log(User.username);
}

此代码在控制台中打印所有成员(在线/离线)名称,但我不知道如何获取语音通道中的唯一用户数。

【问题讨论】:

  • 这是用于命令(还是在消息事件中)?
  • @slothiful Inside bot.on('voiceStateUpdate' , 用户加入\离开后获取计数更新,我已经有代码只想计数

标签: javascript discord.js


【解决方案1】:

您可以过滤 (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);

【讨论】:

  • 感谢您的回答,但我收到错误“ReferenceError: newMember is not defined”,对不起,我知道这很容易,但我不知道 javascript,我该如何定义?
  • 它应该被定义为你事件中的第二个参数。 bot.on('voiceStateUpdate' ... 的整行是什么样的?
  • 感谢它的魅力,非常感谢您的帮助。
【解决方案2】:

如果您不想在事件中使用它,而是在嵌入、消息等中使用;这是我的解决方案。我使用了@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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2021-03-15
    • 2019-01-12
    • 2021-08-08
    • 1970-01-01
    • 2020-07-05
    • 2018-10-04
    • 2020-10-02
    相关资源
    最近更新 更多