【问题标题】:discord.py How to get a server the bot is in with the most membersdiscord.py 如何获取 bot 所在的服务器,其中成员最多
【发布时间】:2021-05-14 09:41:03
【问题描述】:

我想知道是否有办法获得一个拥有最多成员的服务器,该机器人所在的服务器。例如,如果机器人位于服务器 a、b、c 和 d 中,而服务器 A 有会员最多,我怎样才能让它显示它是最大的服务器。

【问题讨论】:

  • 你尝试了什么?
  • 至今想不出什么。

标签: discord discord.py


【解决方案1】:

我假设这将是一个命令。以下是回答您提示的示例代码:

@bot.command() # your client/bot variable. IDK which you have so I put bot.
async def biggest_server(ctx):
    guilds = {len(guild.members):guild for guild in bot.guilds}
    max_members, max_guild = max(guilds.items())
    await ctx.send(f"{max_guild.name} is the biggest server I am in! They have {max_members} members!")

快速文档链接:

  • bot.guilds - 获取机器人所在的公会。我使用字典理解,以便公会对象和它的成员长度保持配对。
  • max - 获取最大成员数(索引 0)。这将返回最大服务器的元组。我将其解压缩为两个变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2021-04-24
    相关资源
    最近更新 更多