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: