how i can count members in python i need full code

Tags: , , ,



i want to count members in discord but how using async await message.channel.send(f"""# of Members: {id.member_count}""") i try

@client.event
async def on_message(message):
    #id = client.get_guild(ID)

    if message.content.find("!hello") != -1:
        await message.channel.send("Hi") 
    elif message.content == "!users":
        await message.channel.send(f"""# of Members: {id.member_count}""") 

i know this is copy code my code is

@bot.command()
async def countmember(ctx):
    ctx.guild.members
    len(ctx.guild.members)
    await ctx.send(f""" of member: {id.member_count}""") 

Answer

To get the amount of members from a guild can be retrieved with member_count, with this you also need to properly define guild or just simply using ctx.guild.member_count

This is an example in your command, I would also recommend to use a command instead of an on_message event to use as a command, there’s just loss of benefits using that way.

@bot.command()
async def countmember(ctx):
    guild = ctx.guild
    await ctx.send(f"Member count: {guild.member_count}") 

Even if you still wanted to get the guild count with an on_message, it can be done this way,

@bot.event
async def on_message(message):
    guild = message.guild
    if message.content.find("!hello") != -1:
        await message.channel.send("Hi") 
    elif message.content == "!users":
        await message.channel.send(f"""# of Members: {guild.member_count}""") 

You have also used bot and client you should only be using one but i’d assume your code works



Source: stackoverflow