首页 > 解决方案 > 试图让机器人在其活动中显示成员数

问题描述

我希望这可以更改机器人状态的成员计数,但似乎无法弄清楚。

members = 31

@client.event
async def on_member_join(member):
        members + 1 

# updates members
@client.event
async def on_ready():
        await client.change_presence(activity=discord.ActivityType.watching, name="Server Name " + str(members) + " Members!"))

标签: pythonbotsdiscorddiscord.py

解决方案


这是将在多个服务器中的机器人的示例:

# Excludes bots
member_count = sum([len([m for m in g.members if not m.bot]) for g in client.guilds])

# Includes bots
member_count = sum([g.member_count for g in client.guilds])

但是,如果您只是要在一台服务器中使用机器人,那么这就足够了:

# Excludes bots
member_count = len([m for m in Guild.members if not m.bot])

# Includes bots
Guild.member_count

# Side-note:
# Guild is simply the guild object that your bot is in.
# Depending on what you've called paramters -
# If you're using command decorators, you'd want ctx.guild,
# or if you're in an on_message() event, you'll want message.guild,
# or if you're somewhere else, you can use get_guild(112233445566778899)

话虽如此,第一个示例与仅在一个服务器中的机器人一起使用,但它们只是有点矫枉过正。


参考:


推荐阅读