From 02fccf2edd5aee4180c3b29f412c4a05081cf52f Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Tue, 22 Sep 2020 01:50:08 -0400 Subject: [PATCH] [RSS] Better hex code matching --- rss/color.py | 23 ++++++++--------------- rss/rss.py | 10 +++++++++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/rss/color.py b/rss/color.py index 06004d1..2428c8e 100644 --- a/rss/color.py +++ b/rss/color.py @@ -1,4 +1,5 @@ import discord +import re from scipy.spatial import KDTree import webcolors @@ -6,25 +7,17 @@ import webcolors class Color: """Helper for color handling.""" - @staticmethod - async def _color_converter(hex_code_or_color_word: str): + async def _color_converter(self, hex_code_or_color_word: str): """ Used for user input on rss embed color Input: discord.Color name, CSS3 color name, 0xFFFFFF, #FFFFFF, FFFFFF Output: 0xFFFFFF """ - # #FFFFFF to 0xFFFFFF - hex_code = hex_code_or_color_word.replace("#", "0x") - - # FFFFFF to 0xFFFFFF - # 0xFFFFFF checking - try: - int(hex_code, 16) - if hex_code[:2] != "0x": - if len(hex_code) == 6: - hex_code = f"0x{hex_code}" - except ValueError: - pass + # #FFFFFF and FFFFFF to 0xFFFFFF + hex_match = re.match(r"#?[a-f0-9]{6}", hex_code_or_color_word.lower()) + if hex_match: + hex_code = f"0x{hex_code_or_color_word.lstrip('#')}" + return hex_code # discord.Color checking if hasattr(discord.Color, hex_code_or_color_word): @@ -40,7 +33,7 @@ class Color: except ValueError: pass - return hex_code + return None async def _hex_to_css3_name(self, hex_code: str): """ diff --git a/rss/rss.py b/rss/rss.py index 034fa58..13e5f49 100644 --- a/rss/rss.py +++ b/rss/rss.py @@ -25,7 +25,7 @@ from .tag_type import INTERNAL_TAGS, VALID_IMAGES, TagType log = logging.getLogger("red.aikaterna.rss") -__version__ = "1.1.2" +__version__ = "1.1.3" class RSS(commands.Cog): @@ -376,6 +376,14 @@ class RSS(commands.Cog): color = color.replace(" ", "_") hex_code = await Color()._color_converter(color) + if not hex_code: + await ctx.send( + "Not a valid color code. Use a hex code like #990000, a " + "Discord color name or a CSS3 color name.\n" + "\n" + "" + ) + return user_facing_hex = hex_code.replace("0x", "#") color_name = await Color()._hex_to_css3_name(hex_code)