【问题标题】:Discord Python Bot Add Color to RoleDiscord Python Bot 为角色添加颜色
【发布时间】:2021-04-29 16:14:08
【问题描述】:

设置现有角色的颜色?

这是我创建角色的代码:

roles = ["SSS | Weeb", "SS Rang | Weeb", "S Rang | Weeb", "A Rang | Weeb", "B Rang | Weeb",
                 "C Rang | Weeb", "D Rang | Weeb", "E Rang | Weeb", "F Rang | Weeb", "Junior Rang | Weeb"]
        for i in roles:
            seasons = discord.utils.get(message.guild.roles, name=str(i))
            if str(seasons) == str(i):
                pass
            else:
                await message.guild.create_role(name=str(i))

现在我想在那里设置类似的东西

roles = ["SSS | Weeb", "SS Rang | Weeb", "S Rang | Weeb", "A Rang | Weeb", "B Rang | Weeb",
                 "C Rang | Weeb", "D Rang | Weeb", "E Rang | Weeb", "F Rang | Weeb", "Junior Rang | Weeb"]
        colors = ["red, green", "blue"] # and much more
        for i in range(len(roles)):
            seasons = discord.utils.get(message.guild.roles, name=str(roles[i]))
            if str(seasons) == str(roles[i]):
                pass
            else:
                await message.guild.create_role(name=str(roles[i]))
            ->  await message.guild.add_color_to_role(role_name=str(roles[i]), color=colors[i])

我什么都没找到……

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    没有Guild.add_color_to_role这样的东西,它是Role.edit并将颜色作为kwargs传递,你也可以在创建角色时传递颜色:

    # Editing the role
    await role.edit(colour=discord.Colour.blue())
    # Creating a role with the color
    await guild.create_role(name="whatever", colour=discord.Colour.blue())
    

    您还可以将十六进制值传递给颜色 kwarg

    await guild.create_role(name="whatever", colour=0xff0000) # Red color
    

    因此,您的颜色列表必须是 discord.Colour 实例列表或整数列表

    colors = [0xff0000, discord.Colour.blue(), 0x00ff00]
    

    要同时浏览两个列表,您可以使用zip 函数(注意:两个列表的长度应该相同)

    role_names = ["name1", "name2", "name3"]
    role_colors = [0xff0000, 0x00ff00, 0x0000ff] 
    
    for name, color in zip(role_names, role_colors):
        print(f"Name: {name}, color: {color}")
    
    # Name: name1, color: 0xff0000
    # Name: name2, color: 0x00ff00
    # ...
    

    你的代码看起来像这样

    role_names = ["name1", "name2", "name3"]
    role_colors = [0xff0000, 0x00ff00, 0x0000ff] # The default color is 0x000000 (white)
    
    for name, color in zip(role_names, role_colors):
        print(f"Name: {name}, color: {color}") # -> Name: name1, color: 0xff0000 ...
    
        # Getting the role
        role = discord.utils.get(message.guild.roles, name=name)
        # Checking if the role exists (in other words - if the `role` variable is not a NoneType)
        if role is not None: 
            # Role does not exist, create it here
            role = await message.guild.create_role(name=name, colour=color)
    

    编辑

    使用像#5482a5这样的颜色

    color = "#5482a5"
    color = color[1:] # removing the initial `#`
    color = int(color, 16) # pass this as the colour kwarg
    

    参考:

    【讨论】:

    • 非常感谢 :) 我也可以使用这种格式的颜色吗:#5482a5
    • 是的!但是您必须将它们转换为以 16 为底的整数,让我编辑答案
    • 太感谢你了 :)
    • 通过转换颜色得到这个错误:ValueError: invalid literal for int() with base 16: '#b1afc8'
    • color = colors[i] color = color[1:] # removing the initial #` color = int(color, 16) # 将此作为颜色 kwarg 传递`
    【解决方案2】:

    您可以在创建角色时指定颜色。

    await message.guild.create_role(name=str(roles[i], colour=discord.Color.blue()))
    

    既然您实际上询问了如何编辑颜色,您可以这样做:

    role = await message.guild.create_role(name=str(roles[i]))
    await role.edit(colour = discord.Colour.orange())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2021-04-21
      • 2020-06-29
      • 2023-03-19
      • 2018-11-26
      • 2020-07-13
      • 2020-09-02
      相关资源
      最近更新 更多