misc.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:lagbot 作者: mikevb1 项目源码 文件源码
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.')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号