From ef7d0bdcf3d2790126169779332c32c6bcfe56b3 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 21:08:15 +0530 Subject: [PATCH 1/6] Add method to fetch and rebase custom theme colors (hex codes) --- main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 6ce10dd..fe49011 100644 --- a/main.py +++ b/main.py @@ -49,6 +49,15 @@ def parse_rating(user_id: Union[int, str]) -> float: aggregated_rating = round(total_stars/number_of_ratings, 1) return aggregated_rating +def get_custom_color(user_id: Union[int, str]) -> int: + """Fetches the custom theme color of the specified user id as base 16 `int`.\n\nReturns `None` if user has no custom color set.""" + theme_color = profile_metadata[str(user_id)]["profile_theme_color"] + if theme_color is not None: + theme_color = int(theme_color, 16) + else: + theme_color = discord.Color.default() + return theme_color + # Events @client.event async def on_ready(): @@ -62,7 +71,8 @@ async def on_message(ctx): if str(ctx.author.id) not in user_ratings: user_ratings[str(ctx.author.id)] = {} if str(ctx.author.id) not in profile_metadata: profile_metadata[str(ctx.author.id)] = { "profile_description": "", - "profile_banner_url": None + "profile_banner_url": None, + "profile_theme_color": None } save() From 10ee51bf496139e846b5ea22ded9f68625b2c6cf Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 21:09:19 +0530 Subject: [PATCH 2/6] Add `/customization profile_color` command This command allows users to set a custom theme color (hex code) to personalize their `/profile` command, embeds, and their bot experience. --- main.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/main.py b/main.py index fe49011..57e7e11 100644 --- a/main.py +++ b/main.py @@ -185,6 +185,26 @@ async def profile_description(ctx: ApplicationContext, description: str = ""): else: localembed = discord.Embed(description=":white_check_mark: Your profile description has been successfully set! Check it out using `/profile`.", color=discord.Color.green()) await ctx.respond(embed=localembed) +@customization.command( + name="profile_color", + description="Set a custom theme color for your /profile command!" +) +@option(name="hex_code", description="The hex code for your custom color", type=str, default=None) +async def profile_color(ctx: ApplicationContext, hex_code: str = None): + """Set a custom theme color for your /profile command!""" + final_hex_code = None + + # Cleaning hex code + if hex_code is not None: + cleaned_hex_code = hex_code.lower().replace("#", "") + final_hex_code = f"0x{cleaned_hex_code}" + + profile_metadata[str(ctx.author.id)]["profile_theme_color"] = final_hex_code + save() + if hex_code is None: localembed = discord.Embed(description=":white_check_mark: Your profile theme color has successfully been removed.", color=discord.Color.green()) + else: localembed = discord.Embed(description=":white_check_mark: Your profile theme color has been successfully set! Check it out using `/profile`.", color=discord.Color.green()) + await ctx.respond(embed=localembed) + # User Commands @client.user_command(name="View Profile") async def _profile(ctx: ApplicationContext, user: discord.User): From 20842908686b5622a96d75a5ab70d6e54ab43492 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 21:23:29 +0530 Subject: [PATCH 3/6] Switch to returning `discord.Embed.Empty` for `None` custom theme colors `discord.Color.default()` will return a color of `#000000` (black), wheras `discord.Embed.Empty` will set the embed to use Discord's default embed color (blank). --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 57e7e11..8a086a0 100644 --- a/main.py +++ b/main.py @@ -50,12 +50,12 @@ def parse_rating(user_id: Union[int, str]) -> float: return aggregated_rating def get_custom_color(user_id: Union[int, str]) -> int: - """Fetches the custom theme color of the specified user id as base 16 `int`.\n\nReturns `None` if user has no custom color set.""" + """Fetches the custom theme color of the specified user id as base 16 `int`.\n\nReturns `discord.Embed.Empty` if user has no custom color set.""" theme_color = profile_metadata[str(user_id)]["profile_theme_color"] if theme_color is not None: theme_color = int(theme_color, 16) else: - theme_color = discord.Color.default() + theme_color = discord.Embed.Empty return theme_color # Events From 88a79b11ae45facd6ca859e7adbf2f2b171e0dce Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 21:24:27 +0530 Subject: [PATCH 4/6] Update embeds to use user custom theme color instead of random embed colors --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 8a086a0..38d3687 100644 --- a/main.py +++ b/main.py @@ -129,7 +129,7 @@ async def profile(ctx: ApplicationContext, user: discord.User = None): localembed = discord.Embed( title=f"{user.display_name}'s profile", description=f"`AKA` {user.name}\n\n{f'*{profile_desc}*' if profile_desc != '' else ''}", - color=discord.Color.random() + color=get_custom_color(user.id) ) localembed.set_thumbnail(url=user.display_avatar) localembed.add_field(name="Profile Picture URL", value=f"[Click to view]({user.display_avatar})") @@ -150,7 +150,7 @@ async def rating(ctx: ApplicationContext, user: discord.User = None): if user == None: user = ctx.author localembed = discord.Embed( description=f":star: {user.name} has been rated {str(parse_rating(user.id))} stars", - color=color + color=get_custom_color(user.id) ) await ctx.respond(embed=localembed) From e4a7be569ed339110d3899aa914cd9ec2fab3fb2 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 21:47:35 +0530 Subject: [PATCH 5/6] Add logic gates to `profile_color()` to check the validity of custom hex color code --- main.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.py b/main.py index 38d3687..e005f2e 100644 --- a/main.py +++ b/main.py @@ -197,6 +197,15 @@ async def profile_color(ctx: ApplicationContext, hex_code: str = None): # Cleaning hex code if hex_code is not None: cleaned_hex_code = hex_code.lower().replace("#", "") + + # Checking hex code validity + if len(cleaned_hex_code) != 3 or len(cleaned_hex_code) != 6: # Character length should either be 3 or 6 + return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must either be 3 or 6 characters long!") + if any(c not in 'abcdef0123456789' for c in cleaned_hex_code): # Only letters a, b, c, d, e, f should be in the hex color code + return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must only contain letters from `a` to `f`!") + if any(c in '`~!@$%^&*()-_=+[{]}\|;:\'",<.>/?' for c in cleaned_hex_code): # The hex color code cannot contain any special symbols except # + return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color cannot contain any special symbols except `#`!") + final_hex_code = f"0x{cleaned_hex_code}" profile_metadata[str(ctx.author.id)]["profile_theme_color"] = final_hex_code From 67d0db0901b4ed551f99fdb30459f0997f2f0feb Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 3 May 2024 22:27:32 +0530 Subject: [PATCH 6/6] Fix logic gates for checking hex color code validity --- main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index e005f2e..8cb84a2 100644 --- a/main.py +++ b/main.py @@ -199,12 +199,12 @@ async def profile_color(ctx: ApplicationContext, hex_code: str = None): cleaned_hex_code = hex_code.lower().replace("#", "") # Checking hex code validity - if len(cleaned_hex_code) != 3 or len(cleaned_hex_code) != 6: # Character length should either be 3 or 6 - return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must either be 3 or 6 characters long!") - if any(c not in 'abcdef0123456789' for c in cleaned_hex_code): # Only letters a, b, c, d, e, f should be in the hex color code - return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must only contain letters from `a` to `f`!") - if any(c in '`~!@$%^&*()-_=+[{]}\|;:\'",<.>/?' for c in cleaned_hex_code): # The hex color code cannot contain any special symbols except # + if not len(cleaned_hex_code) == 6: # Character length should either be 6 + return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must either be 6 characters long!") + elif any(c in '`~!@$%^&*()-_=+[{]}\|;:\'",<.>/?' for c in cleaned_hex_code): # The hex color code cannot contain any special symbols except # return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color cannot contain any special symbols except `#`!") + elif any(c not in 'abcdef0123456789' for c in cleaned_hex_code): # Only letters a, b, c, d, e, f should be in the hex color code + return await ctx.respond(":x: **Incorrectly Formatted Hex Code:** Your hex color code must only contain letters from `a` to `f`!") final_hex_code = f"0x{cleaned_hex_code}"