Ad

How To Separate Bot's From Members In Member_count?

- 1 answer

During a command that displays the member count of the server, i'd like to separate the number of bots from the number of human members and display them each. I can output the total number of members in the server but i'm not sure how to get the bot to distinguish a bot user from human user.

if message.content.startswith('<count'):
        channel = message.channel
        members = message.guild.member_count
        msg = discord.Embed(title="Amount of members in this Discord:", description=members, color=0x0000ff)
        await channel.send(embed=msg)

How would I separate the bot users from the count and display that number separately?

Ad

Answer

Didn't really find anything specific in the Discordpy documentation that allows fetching of members.
So I decided to get the entire list of members in the server instead and filter the list by bots.

if message.content.startswith('<count'):    
        membersInServer = message.guild.members
        # 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

        # Whatever you want to do with the count here

# Put this function somewhere...
# Filter the member list to only bots
def filterOnlyBots(member):
    return member.bot

Speed/performance might be a downside as the server gets larger (more members), hopefully someone posts a better solution than mine.

Ad
source: stackoverflow.com
Ad