Home > database >  discord create role with hex color (pycord)
discord create role with hex color (pycord)

Time:03-11

so im trying to create a role with a hex color code

async def createrole(server, name, hexcolor):
    role = await server.create_role(name=name, color=hexcolor, mentionable=False)
    return role

but im getting this error:

Traceback (most recent call last):
  File "D:\PyCharm Projects\TWD-Booster\venv\lib\site-packages\discord\commands\core.py", line 113, in wrapped
    ret = await coro(arg)
  File "D:\PyCharm Projects\TWD-Booster\venv\lib\site-packages\discord\commands\core.py", line 766, in _invoke
    await self.callback(ctx, **kwargs)
  File "D:/PyCharm Projects/TWD-Booster/main.py", line 99, in boosterrole
    role = await createrole(ctx.author.guild, name, colorformat)
  File "D:/PyCharm Projects/TWD-Booster/main.py", line 242, in createrole
    role = await server.create_role(name=name, color=hexcolor, mentionable=False)
  File "D:\PyCharm Projects\TWD-Booster\venv\lib\site-packages\discord\guild.py", line 2568, in create_role
    fields["color"] = actual_colour.value
AttributeError: 'str' object has no attribute 'value'

this was the color string:

hexcolor = "0xffff00"

my question is, is it possible to create a role with a hex code or do i have to use rgb?

CodePudding user response:

There's no need for you to put the hexcolor value in quotes

hexcolor = 0xffff00
await server.create_role(name=name, color=hexcolor, mentionable=False)

CodePudding user response:

You could also just use discord.Color to set the color. It would be:

async def createrole(server, name, hexcolor):
    hexcolor = discord.Color(0xffff00)
    role = await server.create_role(name=name, color=hexcolor, mentionable=False)
    return role
  • Related