Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

Discord Python Bot Add Color to Role

to set the color of an existing role?

This is my code that create roles:

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))

Now I want there to set sth like

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])

I didn't found anything ...

question from:https://stackoverflow.com/questions/65891851/discord-python-bot-add-color-to-role

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no such thing as Guild.add_color_to_role, it's Role.edit and pass the color as a kwargs, also you can pass the color when creating the role:

# 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())

You can also pass hex values to the colour kwarg

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

So, your colour list must be either a list of discord.Colour instances or a list of ints

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

To go through both of the list at the same time you can use the zip function (Note: both lists should be the same length)

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
# ...

Your code would look something like this

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)

EDIT

To use colors like #5482a5

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

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...