问题
I wanted to make an command to show the membercount but it doesnt 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 didnt find an working option.
回答1:
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.
回答2:
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
回答3:
Simple for
loop should work.
member_count = 0;
for member in ctx.guild.members:
member_count += 1
await ctx.send(member_count)
回答4:
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
回答5:
What Ron did would work but that is just adding another step wouldnt doing
await ctx.send(ctx.guild.member_count)
be easier
来源:https://stackoverflow.com/questions/62115099/how-to-get-the-member-count-in-discord-py-rewrite