i want to count members in discord but how
using async
await message.channel.send(f"""# of Members: {id.member_count}""")
i try
JavaScript
x
9
1
@client.event
2
async def on_message(message):
3
#id = client.get_guild(ID)
4
5
if message.content.find("!hello") != -1:
6
await message.channel.send("Hi")
7
elif message.content == "!users":
8
await message.channel.send(f"""# of Members: {id.member_count}""")
9
i know this is copy code my code is
JavaScript
1
6
1
@bot.command()
2
async def countmember(ctx):
3
ctx.guild.members
4
len(ctx.guild.members)
5
await ctx.send(f""" of member: {id.member_count}""")
6
Advertisement
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.
JavaScript
1
5
1
@bot.command()
2
async def countmember(ctx):
3
guild = ctx.guild
4
await ctx.send(f"Member count: {guild.member_count}")
5
Even if you still wanted to get the guild count with an on_message
, it can be done this way,
JavaScript
1
9
1
@bot.event
2
async def on_message(message):
3
guild = message.guild
4
if message.content.find("!hello") != -1:
5
await message.channel.send("Hi")
6
elif message.content == "!users":
7
await message.channel.send(f"""# of Members: {guild.member_count}""")
8
9
You have also used bot
and client
you should only be using one but i’d assume your code works