【问题标题】:How do I get a list of all the roles my discord bot has?如何获取我的 discord 机器人拥有的所有角色的列表?
【发布时间】:2020-10-31 11:03:47
【问题描述】:

我正在尝试从上到下获取我的机器人具有的所有角色的列表,以便我可以获得最高角色的颜色。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    不确定“最高角色的颜色”是什么意思,您的帖子也缺少您的代码。

    获取机器人在公会中的角色(通​​过命令)的一种方法是获取公会成员对象,然后循环该成员的角色对象。

    试试这个:

    @bot.command()
    async def list_roles(ctx):
        bot_member = ctx.guild.get_member(bot.user.id)
        for bot_role in bot_member.roles:
            print(f'guild role {bot_role} color {bot_role.color}')
    

    控制台输出:

    guild role @everyone color #000000
    guild role masterbot color #2ecc71
    guild role winnerPicker color #e67e22
    guild role gabAdmin color #ad1457
    

    【讨论】:

      【解决方案2】:

      您可以使用Guild.roles 像这样在所有角色中执行循环

      @bot.command()
      async def get_roles(ctx):
          all_roles = []
          for role in ctx.guild.roles:
              all_roles.append(role.name)
          all_roles.reverse()# to make it higher first 
          print(all_roles)
      

      【讨论】:

      • 这会打印出服务器上所有角色的列表。我如何打印出机器人的角色?
      猜你喜欢
      • 2020-09-22
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2019-08-26
      • 2020-09-24
      相关资源
      最近更新 更多