如何获得一些在线会员?

时间:2019-08-06 06:38:52

标签: discord python-3.7 discord.py discord.py-rewrite

我有一个命令,该命令计算成员和漫游器并分别输出。然后,我想输出在线用户的数量。这可能吗?

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

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

1 个答案:

答案 0 :(得分:0)

每个Member has a status property,您可以使用它来检查是否status is offline
然后,您可以按离线状态过滤membersInServer

        onlineMembersInServer = list(filter(filterOnlyOnlineMembers, membersInServer))

        onlineMembersCount = len(onlineMembersInServer)

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

请注意,它统计在线用户和漫游器的数量。
如果只想过滤在线用户,则可以将过滤器更改为此:

# 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

请注意,如果服务器很大,则可能会出现性能问题。

编辑

正如@Patrick Haugh所提到的,您可以将其变成1-liner

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