【问题标题】:How to make bot status say member count for all the servers that its in (discord.py)如何让机器人状态说它所在的所有服务器的成员计数(discord.py)
【发布时间】:2021-04-13 17:01:48
【问题描述】:

抱歉,标题太长了。我希望我的机器人告诉我我的机器人在服务器中有多少成员处于机器人的状态。换句话说,我希望我的机器人雕像说Watching [member count] people and [server count] servers,但我不知道怎么做。有人可以帮忙吗?

@client.event
async def on_ready():
    print('Potato Cat is ready :D')
    await client.change_presence(
        activity=discord.Activity(
            type=discord.ActivityType.watching,
            name=f'{len(client.guilds)} servers and {len(client.members)} people'))

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    改为client.change_presence

    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'{len(client.guilds)} servers and {len(client.members)} people')
    

    【讨论】:

      【解决方案2】:

      获取服务器数量:

      servers = len(client.guilds)
      

      获取成员数量:

      members = 0
      for guild in client.guilds:
          members += guild.member_count - 1    # I've added a '-1' because guild.member_count includes all users and bots including your own bot
      

      因此,您的 on_ready() 事件将如下所示:

      @client.event
      async def on_ready():
          print('Potato Cat is ready :D')
      
          servers = len(client.guilds)
          members = 0
          for guild in client.guilds:
              members += guild.member_count - 1
      
          await client.change_presence(activity = discord.Activity(
              type = discord.ActivityType.watching,
              name = f'{servers} servers and {members} members'
          ))
      

      【讨论】:

      • 等一下 OMG ITS U :O hi
      【解决方案3】:
         client.event
      async def on_ready():
          print('Potato Cat is ready :D')
          client.loop.create_task(status_task())
          
      
      async def status_task():
          while True:
              await bot.change_presence(
                  activity=discord.Activity(
                      type=discord.ActivityType.watching,
                      name=f'Prefix: {prefix} | {len(bot.guilds)} server'),
                  status=discord.Status.online)
              await asyncio.sleep(1800)
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2020-10-29
      • 2020-06-26
      • 1970-01-01
      • 2021-06-02
      • 2021-03-19
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多