【问题标题】:How to get only name of member.roles Discord.py如何仅获取 member.roles Discord.py 的名称
【发布时间】:2021-04-18 22:33:09
【问题描述】:

如果我在message.author 上使用.roles,我会得到角色名称,但我也会得到他们的ID。是否可以只显示这些角色的名称?

【问题讨论】:

    标签: python discord.py roles


    【解决方案1】:

    您会获得discord.Role 实例,要简单地获得角色名称,您可以使用简单的列表推导

    roles = message.author.roles
    role_names = [role.name for role in roles]
    

    如果你想要一个普通的 for 循环

    roles = message.author.roles
    role_names = []
    
    for role in roles:
        role_names.append(role.name)
    

    示例输出:

    ["role1", "role2", "role3", ...]
    

    【讨论】:

    • 这正是我想要的。非常感谢。
    • 如果有帮助记得采纳答案
    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2021-04-08
    • 2019-03-25
    • 2020-10-30
    • 1970-01-01
    • 2021-12-26
    • 2021-09-25
    相关资源
    最近更新 更多