def hex_or_rgb(arg):
s = arg.split(' ')
if len(s) == 1:
color = s[0]
if len(color) == 6:
color = f'0x{color}'
elif len(color) == 7:
color = color.replace('#', '0x')
try:
return discord.Color(int(color, 0))
except ValueError:
raise commands.BadArgument('A single argument must be passed as hex (`0x7289DA`, `#7289DA`, `7289DA`)')
elif len(s) == 3:
try:
rgb = [*map(int, s)]
except ValueError:
raise commands.BadArgument('Three arguments must be passed as RGB (`114 137 218`, `153 170 181`)')
if any(c < 0 or c > 255 for c in rgb):
raise commands.BadArgument('RGB colors must be in the range `[0, 255]`')
return discord.Color.from_rgb(*rgb)
raise commands.BadArgument('You must pass 1 (hex) or 3 (RGB) arguments.')
评论列表
文章目录