How to get the member count in discord.py rewrite?

Viewed 21695

I wanted to make an command to show the membercount but it doesn't work.

This is what I have:

@bot.command(name='membercount')
async def membercount(ctx):
    await ctx.send(len(guild.member_count))

and this is the error i get: Module 'discord.guild' has no 'member_count' member

I tried searching on the internet but didn't find a working option.

9 Answers

Although Ron's example does work, it is not a very elegant way considering that ctx.guild.members is just a list, meaning you can use len() on it as such:

member_count = len(ctx.guild.members) # includes bots

true_member_count = len([m for m in ctx.guild.members if not m.bot]) # doesn't include bots

Also don't forget that if an answer solved your problem, you should mark it as "accepted" with that checkmark next to it, as to show other users coming from search machines to this question, what worked for you.

You can just do

@c.command(aliases=["mc"])

async def member_count(ctx):

    a=ctx.guild.member_count
    b=discord.Embed(title=f"members in {ctx.guild.name}",description=a,color=discord.Color((0xffff00)))
    await ctx.send(embed=b)

This is the best and the easiest way in my opinion

you do not need to use len(guild.member_count) The member_count attribute of the ctx.guild provides the number of the membercount. Here an example:

@bot.command(name='membercount')
async def membercount(ctx):
    await ctx.send(ctx.guild.member_count)

Enable Developer mode in Discord (User Settings -> Accessibility) then right click the server icon and click Copy ID, then type this line of code after async def: guild = client.get_guild(paste the copied ID here) Also you don't need len(guild.member_count) it will return number of digits of member count. Use guild.member_count only.

Hope this helped

What Ron did would work but that is just adding another step. This would be easier.

await ctx.send(ctx.guild.member_count) 

you should use ctx.message.guild.member_count

Simple for loop should work.

member_count = 0;

for member in ctx.guild.members:
    member_count += 1
await ctx.send(member_count)

If you want it to work then this is what you can do. It's also a embed so it looks good, you can change the colour and the footer, and a thumbnail picture if you want, and make the title a URL. Best of both worlds, also deletes the message after the command is executed.

@client.command(name='membercount')
    async def membercount(ctx):
        embed = discord.Embed(
            title=('Membercount'),
            description =(f'There are currently **{ctx.guild.member_count}** members in the server!'),
            url = (''),
            timestamp = datetime.now(),
            color= discord.Colour.dark_blue()
        )
        embed.set_footer(text='Our Community')
        embed.set_thumbnail(url='')
        await ctx.send(embed=embed)
        await ctx.message.delete() 

This should work.

@bot.command(name='membercount')
async def membercount(ctx):
   await ctx.send(ctx.guild.member_count)
Related