如何获得多个在线会员? [英] How to get a number of online members?

查看:70
本文介绍了如何获得多个在线会员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令可以计算成员和机器人并分别输出它们.然后我想输出在线用户的数量.这可能吗?

I have a command that counts members and bots and outputs them separately. I would then like to output the number of online users. Is this possible?

此命令获取成员和机器人数量

This command gets member and bot count

if message.content.startswith('<count'):    
        membersInServer = message.guild.members
        channel = message.channel
        # Filter to the list, returns a list of bot-members
        botsInServer = list(filter(filterOnlyBots, membersInServer))
        botsInServerCount = len(botsInServer)
        # (Total Member count - bot count) = Total user count
        usersInServerCount = message.guild.member_count - botsInServerCount
        msg = discord.Embed(title="Amount of Human Members in this Discord:", description=usersInServerCount, color=0x00FD00)
        msg.add_field(name="Amount of Bot Users in this Discord:",value=botsInServerCount, inline=False)
        await channel.send(embed=msg)
def filterOnlyBots(member):
    return member.bot

我已经尝试过 client.member.status 并且只返回 Online

I've tried client.member.status and that just returns Online

推荐答案

Each Member 有一个 status 属性,你可以用它来检查 状态是否离线.
然后您可以通过离线状态过滤您的 membersInServer.

Each Member has a status property, which you can use it to check if the status is offline or not.
You can then filter your membersInServer by offline status.

        onlineMembersInServer = list(filter(filterOnlyOnlineMembers, membersInServer))

        onlineMembersCount = len(onlineMembersInServer)

# Somewhere...
def filterOnlyOnlineMembers(member):
    return member.status != 'offline'

请注意,它计算在线用户和机器人
如果您只想过滤到在线用户,您可以将过滤器更改为:

Note that it count online users and bots
If you want to filter to only online users, you can change the filter to this:

# Set the filter to be a non-offline member, and the member not being a bot.
def filterOnlyOnlineMembers(member):
    return member.status != 'offline' and not member.bot

请注意,如果服务器很大,这可能会导致性能问题.

Note that this might have performance issues if the server is large.

正如@Patrick Haugh 提到的,你可以把它做成单行

As @Patrick Haugh mentioned, you can make this into a 1-liner

sum(member.status!=discord.Status.offline and not member.bot for member in message.guild.members)

这篇关于如何获得多个在线会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆