python - 试图让机器人在其活动中显示成员数
问题描述
我希望这可以更改机器人状态的成员计数,但似乎无法弄清楚。
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!"))
解决方案
这是将在多个服务器中的机器人的示例:
# 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)
话虽如此,第一个示例将与仅在一个服务器中的机器人一起使用,但它们只是有点矫枉过正。
参考:
Guild.members
Guild.member_count
Guild.chunked
- 如果您因任何原因在获取成员数时遇到问题,您可能需要查看此和client.request_offline_members()
.Client.get_guild()
推荐阅读
- javascript - 使用javascript将div内的文本更改为负html代码
- python - 如何处理音频字节以从实时音频流中提取 MFCC?
- elasticsearch - 无法在 Elasticsearch 集群上注册 s3 存储库
- selenium-webdriver - 无法使用 switchTo() 方法获取子窗口的标题
- android - 地图未出现在 Android 模拟器中
- javascript - 如何在 Vue.js 中修复页面底部的页脚以及如何在另一页显示额外的书籍?
- php - 如何将多个域添加到同一个虚拟主机以及如何正确使用 MVC url 重定向
- python - 如何在 Python 中将多行字符串合并为一行,并用空格分隔它们?
- asp.net - 局部视图数据到表
- java - IllegalStateException:无法订阅。处理器已终止