Hey, I'm trying to make the colors of my embeds random, so I made this:
`import random
col = lambda: random.randint(0,255)
colour = '0x' + '%02X%02X%02X' % (col(),col(),col())`
So that generates a random Hexadecimal colour with the added "0x" in front of it for discord embed
However when testing this out in an embed I get the following error
Expected discord.Colour, int, or Embed.Empty but received str instead.
However if I print the generated hex (for example: 0x36AFDF )
And I replace the embed parameter color=colour by color="0x36AFDF"
It will work.
How can I format my code so that I can randomely generate a hex colour but avoid the str error?
Why not use random int?
colour=random.randint(0, 16777215)
(0 = #000, 16777215 = #FFF)
This way of handling this problem is very strange and overcomplicated (did you find it somewhere on the internet?).
discord.Colour does not accept strings. The reason what you mentioned at the end works is because you are actually using a hexadecimal literal, not a string:
- color="0x36AFDF"
+ color=0x36AFDF
This is a way that Python lets you specify integers in hexadecimal. Compare the behavior in an interactive interpreter session:
>>> "0x36AFDF"
'0x36AFDF'
>>> 0x36AFDF
3583967
Considering the end goal allows us to vastly simplify what you are attempting to achieve. There are a few key points:
#000000 and #FFFFFF inclusive.Fortunately, we can solve all of these with a single expression (notice the hexadecimal literal!):
colour = random.randint(0, 0xFFFFFF)
This gives us back a random integer between 0 (#000000) and 0xFFFFFF (16777215, #FFFFFF), avoiding a lambda definition, 3 function calls, string concatenation and string formatting, which we can now feed into the Embed.
@Gorialis Oh alright, yeah I never quite understood what 0x whas meant for, I researched a bit and that was what I found http://forum.sa-mp.com/showthread.php?t=69755, which gives no further explanation
So I thought you know, make a random Hex colour, and just add "0x" in front because that was what is required
Anyway thanks for the help!
Most helpful comment
This way of handling this problem is very strange and overcomplicated (did you find it somewhere on the internet?).
discord.Colourdoes not accept strings. The reason what you mentioned at the end works is because you are actually using a hexadecimal literal, not a string:This is a way that Python lets you specify integers in hexadecimal. Compare the behavior in an interactive interpreter session:
Considering the end goal allows us to vastly simplify what you are attempting to achieve. There are a few key points:
#000000and#FFFFFFinclusive.Fortunately, we can solve all of these with a single expression (notice the hexadecimal literal!):
This gives us back a random integer between 0 (#000000) and 0xFFFFFF (16777215, #FFFFFF), avoiding a lambda definition, 3 function calls, string concatenation and string formatting, which we can now feed into the Embed.