Home > database >  Is there any way to convert the hex code from string to hex colour which discord.py supports?
Is there any way to convert the hex code from string to hex colour which discord.py supports?

Time:02-13

I am trying to figure this out for so long, but I am not able to convert the hex code from string to actual hex code like this 0xFFFFFF since discord.py doesn't take hex code in str datatype. Code Snippet:-

@client.command()
async def testing(ctx):
    # color = lgd.hexConvertor(colorCollection.find({},{"_id":0,"Hex":1}))
    c = "0xFFFFFF"
    inte = int(c,16)
    color = hex(inte)
    await ctx.send(embed = discord.Embed(description = "testing",color = color))

Error:-

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 221, in testing
    await ctx.send(embed = discord.Embed(description = "testing",color = color))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 115, in __init__
    self.colour = colour
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 230, in colour
    raise TypeError('Expected discord.Colour, int, or Embed.Empty but received %s instead.' % value.__class__.__name__)
TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.

This is how I stored the hex codes in my mongodb atlas database-> mongodb hex codes

Edit:- It worked on using inte instead of color

CodePudding user response:


>>> color = "0xFFFFFF"
>>> int_color = int(color,16)
>>> hex(int_color)
'0xffffff'
>>> # basically the same thing
>>>
>>> from discord import Color
>>>
>>> Color(int_color)
<Colour value=16777215>
>>>
>>>
>>>
>>> teal = int("0x1abc9c",16)
>>> Color(teal)
<Colour value=1752220>
>>> Color.teal()     #classmethod for teal color
<Colour value=1752220>
>>> #just ints work

discord.py documentation for colours

CodePudding user response:

Look at the error.

TypeError: Expected discord.Colour, int, or Embed.Empty

An int would suffice, no need for extra conversions.

@client.command()
async def testing(ctx):
    # color = lgd.hexConvertor(colorCollection.find({},{"_id":0,"Hex":1}))
    c = "0xFFFFFF"
    int_colour = int(c,16)
    await ctx.send(embed = discord.Embed(description = "testing",color = int_colour))
  • Related