设置现有角色的颜色?
这是我创建角色的代码:
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])
我没有发现任何东西......
答案 0 :(得分:2)
没有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
答案 1 :(得分:1)
您可以在创建角色时指定颜色。
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())