From 90625a1d323e052d8509f55fa49958a2b88bbd8e Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Sun, 11 Feb 2024 19:25:38 +0400 Subject: [PATCH 01/17] Add 4 different case commands + helper function --- bot/exts/fun/fun.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ bot/utils/helpers.py | 29 +++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 66c4851729..ef87b6bd37 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -79,6 +79,90 @@ def conversion_func(text: str) -> str: if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) + + @commands.command(name="snakecase", aliases=("scase",)) + async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: + """Attempts to convert the provided string to snake_case.""" + text = helpers.neutralise_string(text) + def conversion_func(text: str) -> str: + """Converts the provided string to snake_case.""" + return "_".join( + text.split() + ) + text = await clean_text_or_reply(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = messages.convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) + + @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) + async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: + """Attempts to convert the provided string to pascalCase.""" + text = helpers.neutralise_string(text) + def conversion_func(text: str) -> str: + """Converts the provided string to pascalCase.""" + return "".join( + word[0].upper()+word[1:] if i != 0 else word for i, word in enumerate(text.split()) + ) + text = await clean_text_or_reply(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = messages.convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) + + @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake","screamingsnake",)) + async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: + """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" + text = helpers.neutralise_string(text) + def conversion_func(text: str) -> str: + """Converts the provided string to SCREAMING_SNAKE_CASE.""" + return "_".join( + word.upper() for word in text.split() + ) + text = await clean_text_or_reply(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = messages.convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) + + @commands.command(name="camelcase", aliases=("ccase", "camel",)) + async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: + """Attempts to convert the provided string to CamelCase.""" + text = helpers.neutralise_string(text) + def conversion_func(text: str) -> str: + """Converts the provided string to CamelCase.""" + return "".join( + word[0].upper()+word[1:] for word in text.split() + ) + text = await clean_text_or_reply(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = messages.convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) @commands.group(name="caesarcipher", aliases=("caesar", "cc",)) async def caesarcipher_group(self, ctx: Context) -> None: diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 74c2ccd040..e6e27f852d 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -1,4 +1,6 @@ +# imports import re +from string import punctuation def suppress_links(message: str) -> str: @@ -6,3 +8,30 @@ def suppress_links(message: str) -> str: for link in set(re.findall(r"https?://[^\s]+", message, re.IGNORECASE)): message = message.replace(link, f"<{link}>") return message + +def neutralise_string(txt: str) -> str: + """Attempts to neutralise all punctuation and cases and returns a string of lowercase words""" + # take out punctuation + + for c in punctuation: + words = txt.split(c) + txt = " ".join(words) + + words = txt.split() + # full caps words + words = [word.lower() if word.isupper() else word for word in words] + txt = " ".join(words) + + words = [] + old_i = 0 + for i, char in enumerate(txt): + if char.isupper(): + words.append(txt[old_i:i]) + old_i = i + words.append(txt[old_i:]) + + # strip white spaces + words = [word.strip() for word in words] + txt = " ".join(words) + # return everything lower case + return " ".join(word.lower() for word in txt.split()) \ No newline at end of file From 494f7c891b9aeba7a47b75a69f1c50624bb6397d Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Mon, 12 Feb 2024 14:40:06 +0400 Subject: [PATCH 02/17] Updated neutralise string for slight optimization. --- bot/utils/helpers.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index e6e27f852d..1070724472 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -1,7 +1,5 @@ # imports import re -from string import punctuation - def suppress_links(message: str) -> str: """Accepts a message that may contain links, suppresses them, and returns them.""" @@ -12,26 +10,22 @@ def suppress_links(message: str) -> str: def neutralise_string(txt: str) -> str: """Attempts to neutralise all punctuation and cases and returns a string of lowercase words""" # take out punctuation + txt = re.sub(r'([^\w\s]|_)',' ',txt) - for c in punctuation: - words = txt.split(c) - txt = " ".join(words) - - words = txt.split() - # full caps words - words = [word.lower() if word.isupper() else word for word in words] + # full caps words but leaves CamelCase / pascalCase + words = [word.lower() if word.isupper() else word for word in txt.split()] txt = " ".join(words) + # attempt to split pascalCase and CamelCase words = [] old_i = 0 for i, char in enumerate(txt): - if char.isupper(): + # to avoid CamelCase getting leading empty append + if char.isupper() and i != 0: words.append(txt[old_i:i]) old_i = i words.append(txt[old_i:]) - - # strip white spaces - words = [word.strip() for word in words] - txt = " ".join(words) - # return everything lower case - return " ".join(word.lower() for word in txt.split()) \ No newline at end of file + + # strip white spaces and make lowercase + words = [word.strip().lower() for word in words] + return " ".join(words) \ No newline at end of file From 54a462223407975ab3c6464bd40ddb005edd1700 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Mon, 12 Feb 2024 15:25:14 +0400 Subject: [PATCH 03/17] Removed spaces --- bot/exts/fun/fun.py | 4 ++-- bot/utils/helpers.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index ef87b6bd37..abc2588986 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -79,7 +79,7 @@ def conversion_func(text: str) -> str: if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) - + @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" @@ -245,4 +245,4 @@ async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck" async def setup(bot: Bot) -> None: """Load the Fun cog.""" - await bot.add_cog(Fun(bot)) + await bot.add_cog(Fun(bot)) \ No newline at end of file diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 1070724472..8bca9d9b61 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -25,7 +25,7 @@ def neutralise_string(txt: str) -> str: words.append(txt[old_i:i]) old_i = i words.append(txt[old_i:]) - + # strip white spaces and make lowercase words = [word.strip().lower() for word in words] return " ".join(words) \ No newline at end of file From a03b682c3c68487bde4bfc4f6fe4846956e47376 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Mon, 12 Feb 2024 15:45:03 +0400 Subject: [PATCH 04/17] Fixed end of file. --- bot/exts/fun/fun.py | 2 +- bot/utils/helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index abc2588986..e99fd7b362 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -245,4 +245,4 @@ async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck" async def setup(bot: Bot) -> None: """Load the Fun cog.""" - await bot.add_cog(Fun(bot)) \ No newline at end of file + await bot.add_cog(Fun(bot)) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 8bca9d9b61..708fbe721d 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -28,4 +28,4 @@ def neutralise_string(txt: str) -> str: # strip white spaces and make lowercase words = [word.strip().lower() for word in words] - return " ".join(words) \ No newline at end of file + return " ".join(words) From 7bb7be4e8c2e851fc90ea44eb931764210b2a7d8 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Mon, 12 Feb 2024 16:09:36 +0400 Subject: [PATCH 05/17] Fixed ruff feedback. --- bot/exts/fun/fun.py | 2 +- bot/utils/helpers.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index e99fd7b362..2c80f322f0 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -122,7 +122,7 @@ def conversion_func(text: str) -> str: converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) - @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake","screamingsnake",)) + @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" text = helpers.neutralise_string(text) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 708fbe721d..6ca0b79f05 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -1,6 +1,6 @@ -# imports import re + def suppress_links(message: str) -> str: """Accepts a message that may contain links, suppresses them, and returns them.""" for link in set(re.findall(r"https?://[^\s]+", message, re.IGNORECASE)): @@ -8,9 +8,9 @@ def suppress_links(message: str) -> str: return message def neutralise_string(txt: str) -> str: - """Attempts to neutralise all punctuation and cases and returns a string of lowercase words""" + """Attempts to neutralise all punctuation and cases and returns a string of lowercase words.""" # take out punctuation - txt = re.sub(r'([^\w\s]|_)',' ',txt) + txt = re.sub(r"([^\w\s]|_)", " ", txt) # full caps words but leaves CamelCase / pascalCase words = [word.lower() if word.isupper() else word for word in txt.split()] From d70125577028ee08ba05c521f45c0a9bba851e41 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Mon, 12 Feb 2024 17:23:20 +0400 Subject: [PATCH 06/17] Extracted duplicate code into function. --- bot/exts/fun/fun.py | 77 +++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 2c80f322f0..8bc139f2b6 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -1,6 +1,6 @@ import json import random -from collections.abc import Iterable +from collections.abc import Callable, Iterable from pathlib import Path from typing import Literal @@ -51,6 +51,21 @@ def _get_random_die() -> str: die_name = f"dice_{random.randint(1, 6)}" return getattr(Emojis, die_name) + @staticmethod + async def _clean_fun_cog_text(ctx: Context, text: str, conversion_func: Callable[[str], str]) -> None: + """This groups the clean and convert functions into one so we can reuse this without duplicated code.""" + text = await clean_text_or_reply(ctx, text) + text, embed = await messages.get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = messages.convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) + @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" @@ -68,17 +83,7 @@ def conversion_func(text: str) -> str: return "".join( char.upper() if round(random.random()) else char.lower() for char in text ) - text = await clean_text_or_reply(ctx, text) - text, embed = await messages.get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = messages.convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + await self._clean_fun_cog_text(ctx, text, conversion_func) @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: @@ -89,17 +94,7 @@ def conversion_func(text: str) -> str: return "_".join( text.split() ) - text = await clean_text_or_reply(ctx, text) - text, embed = await messages.get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = messages.convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + await self._clean_fun_cog_text(ctx, text, conversion_func) @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: @@ -110,17 +105,7 @@ def conversion_func(text: str) -> str: return "".join( word[0].upper()+word[1:] if i != 0 else word for i, word in enumerate(text.split()) ) - text = await clean_text_or_reply(ctx, text) - text, embed = await messages.get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = messages.convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + await self._clean_fun_cog_text(ctx, text, conversion_func) @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: @@ -131,17 +116,7 @@ def conversion_func(text: str) -> str: return "_".join( word.upper() for word in text.split() ) - text = await clean_text_or_reply(ctx, text) - text, embed = await messages.get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = messages.convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + await self._clean_fun_cog_text(ctx, text, conversion_func) @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: @@ -152,17 +127,7 @@ def conversion_func(text: str) -> str: return "".join( word[0].upper()+word[1:] for word in text.split() ) - text = await clean_text_or_reply(ctx, text) - text, embed = await messages.get_text_and_embed(ctx, text) - # Convert embed if it exists - if embed is not None: - embed = messages.convert_embed(conversion_func, embed) - converted_text = conversion_func(text) - converted_text = helpers.suppress_links(converted_text) - # Don't put >>> if only embed present - if converted_text: - converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + await self._clean_fun_cog_text(ctx, text, conversion_func) @commands.group(name="caesarcipher", aliases=("caesar", "cc",)) async def caesarcipher_group(self, ctx: Context) -> None: From 1a4bc6ab4687b8164839f6a15735906993f74d61 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 17:00:52 +0400 Subject: [PATCH 07/17] Added BadArgument error when no text provided. --- bot/exts/fun/fun.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 8bc139f2b6..dc84a7fa78 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -52,7 +52,7 @@ def _get_random_die() -> str: return getattr(Emojis, die_name) @staticmethod - async def _clean_fun_cog_text(ctx: Context, text: str, conversion_func: Callable[[str], str]) -> None: + async def _clean_text(ctx: Context, text: str, conversion_func: Callable[[str], str]) -> tuple[str, Embed]: """This groups the clean and convert functions into one so we can reuse this without duplicated code.""" text = await clean_text_or_reply(ctx, text) text, embed = await messages.get_text_and_embed(ctx, text) @@ -64,7 +64,7 @@ async def _clean_fun_cog_text(ctx: Context, text: str, conversion_func: Callable # Don't put >>> if only embed present if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" - await ctx.send(content=converted_text, embed=embed) + return converted_text, embed @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: @@ -78,56 +78,71 @@ async def roll(self, ctx: Context, num_rolls: int = 1) -> None: @commands.command(name="randomcase", aliases=("rcase", "randomcaps", "rcaps",)) async def randomcase_command(self, ctx: Context, *, text: str | None) -> None: """Randomly converts the casing of a given `text`, or the replied message.""" + if not text: + raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") def conversion_func(text: str) -> str: """Randomly converts the casing of a given string.""" return "".join( char.upper() if round(random.random()) else char.lower() for char in text ) - await self._clean_fun_cog_text(ctx, text, conversion_func) + converted_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=converted_text, embed=embed) @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" + if not text: + raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" return "_".join( text.split() ) - await self._clean_fun_cog_text(ctx, text, conversion_func) + converted_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=converted_text, embed=embed) @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to pascalCase.""" + if not text: + raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to pascalCase.""" return "".join( word[0].upper()+word[1:] if i != 0 else word for i, word in enumerate(text.split()) ) - await self._clean_fun_cog_text(ctx, text, conversion_func) + converted_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=converted_text, embed=embed) @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" + if not text: + raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" return "_".join( word.upper() for word in text.split() ) - await self._clean_fun_cog_text(ctx, text, conversion_func) + converted_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=converted_text, embed=embed) @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to CamelCase.""" + if not text: + raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to CamelCase.""" return "".join( word[0].upper()+word[1:] for word in text.split() ) - await self._clean_fun_cog_text(ctx, text, conversion_func) + converted_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=converted_text, embed=embed) @commands.group(name="caesarcipher", aliases=("caesar", "cc",)) async def caesarcipher_group(self, ctx: Context) -> None: From 731ab4d31b9a274efb13ce8270db316508faa3e0 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 17:17:07 +0400 Subject: [PATCH 08/17] Changed error message for snakecase commands. -JK --- bot/exts/fun/fun.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index dc84a7fa78..e22e5ceba0 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -79,7 +79,7 @@ async def roll(self, ctx: Context, num_rolls: int = 1) -> None: async def randomcase_command(self, ctx: Context, *, text: str | None) -> None: """Randomly converts the casing of a given `text`, or the replied message.""" if not text: - raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") def conversion_func(text: str) -> str: """Randomly converts the casing of a given string.""" return "".join( @@ -92,7 +92,7 @@ def conversion_func(text: str) -> str: async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" if not text: - raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" @@ -106,7 +106,7 @@ def conversion_func(text: str) -> str: async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to pascalCase.""" if not text: - raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to pascalCase.""" @@ -120,7 +120,7 @@ def conversion_func(text: str) -> str: async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" if not text: - raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" @@ -134,7 +134,7 @@ def conversion_func(text: str) -> str: async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to CamelCase.""" if not text: - raise BadArgument(f"`{Client.prefix}casecommands` require a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to CamelCase.""" From ddaeb14907f734e0edbb673a5d19163e69052c8a Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 17:30:14 +0400 Subject: [PATCH 09/17] Adjusted error message for casecommands. -JK --- bot/exts/fun/fun.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index e22e5ceba0..e3de5249a5 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -79,7 +79,7 @@ async def roll(self, ctx: Context, num_rolls: int = 1) -> None: async def randomcase_command(self, ctx: Context, *, text: str | None) -> None: """Randomly converts the casing of a given `text`, or the replied message.""" if not text: - raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") def conversion_func(text: str) -> str: """Randomly converts the casing of a given string.""" return "".join( @@ -92,7 +92,7 @@ def conversion_func(text: str) -> str: async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" if not text: - raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" @@ -106,7 +106,7 @@ def conversion_func(text: str) -> str: async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to pascalCase.""" if not text: - raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to pascalCase.""" @@ -120,7 +120,7 @@ def conversion_func(text: str) -> str: async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" if not text: - raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" @@ -134,7 +134,7 @@ def conversion_func(text: str) -> str: async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to CamelCase.""" if not text: - raise BadArgument(f"`{Client.prefix}{ctx.command}` requires a text to attempt to convert.") + raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to CamelCase.""" From 7ea86c912704795d371f7a5d1fbd52d244a6538b Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 19:58:22 +0400 Subject: [PATCH 10/17] Removed raise error, replaced with early return in helper function. -JK --- bot/exts/fun/fun.py | 10 ---------- bot/utils/helpers.py | 4 +++- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index e3de5249a5..fe4d6e2f49 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -78,8 +78,6 @@ async def roll(self, ctx: Context, num_rolls: int = 1) -> None: @commands.command(name="randomcase", aliases=("rcase", "randomcaps", "rcaps",)) async def randomcase_command(self, ctx: Context, *, text: str | None) -> None: """Randomly converts the casing of a given `text`, or the replied message.""" - if not text: - raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") def conversion_func(text: str) -> str: """Randomly converts the casing of a given string.""" return "".join( @@ -91,8 +89,6 @@ def conversion_func(text: str) -> str: @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" - if not text: - raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" @@ -105,8 +101,6 @@ def conversion_func(text: str) -> str: @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to pascalCase.""" - if not text: - raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to pascalCase.""" @@ -119,8 +113,6 @@ def conversion_func(text: str) -> str: @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" - if not text: - raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" @@ -133,8 +125,6 @@ def conversion_func(text: str) -> str: @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to CamelCase.""" - if not text: - raise BadArgument(f"`{Client.prefix}{ctx.invoked_with}` requires a text to attempt to convert.") text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to CamelCase.""" diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 6ca0b79f05..7d626f659a 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -7,9 +7,11 @@ def suppress_links(message: str) -> str: message = message.replace(link, f"<{link}>") return message -def neutralise_string(txt: str) -> str: +def neutralise_string(txt: str | None) -> str | None: """Attempts to neutralise all punctuation and cases and returns a string of lowercase words.""" # take out punctuation + if not txt: + return None txt = re.sub(r"([^\w\s]|_)", " ", txt) # full caps words but leaves CamelCase / pascalCase From 1891f445facbbef4561809256e72ff7ec9165990 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 20:01:39 +0400 Subject: [PATCH 11/17] Corrected comments. -JK --- bot/utils/helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 7d626f659a..12f8b71551 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -9,9 +9,11 @@ def suppress_links(message: str) -> str: def neutralise_string(txt: str | None) -> str | None: """Attempts to neutralise all punctuation and cases and returns a string of lowercase words.""" - # take out punctuation + # return early if no text provided. if not txt: return None + + # take out punctuation txt = re.sub(r"([^\w\s]|_)", " ", txt) # full caps words but leaves CamelCase / pascalCase From 143255815bd541cd152e8c7069338bf1ced602bb Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 21:41:24 +0400 Subject: [PATCH 12/17] Swap pascal/camel case to be correct. -JK --- bot/exts/fun/fun.py | 12 ++++++------ bot/utils/helpers.py | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index fe4d6e2f49..00a9b5bb4a 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -100,12 +100,12 @@ def conversion_func(text: str) -> str: @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: - """Attempts to convert the provided string to pascalCase.""" + """Attempts to convert the provided string to PascalCase.""" text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: - """Converts the provided string to pascalCase.""" + """Converts the provided string to PascalCase.""" return "".join( - word[0].upper()+word[1:] if i != 0 else word for i, word in enumerate(text.split()) + word.capitalize() for word in text.split() ) converted_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=converted_text, embed=embed) @@ -124,12 +124,12 @@ def conversion_func(text: str) -> str: @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: - """Attempts to convert the provided string to CamelCase.""" + """Attempts to convert the provided string to camelCase.""" text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: - """Converts the provided string to CamelCase.""" + """Converts the provided string to camelCase.""" return "".join( - word[0].upper()+word[1:] for word in text.split() + word.capitalize() if i != 0 else word for i, word in enumerate(text.split()) ) converted_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=converted_text, embed=embed) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 12f8b71551..6b9d82bb65 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -16,15 +16,15 @@ def neutralise_string(txt: str | None) -> str | None: # take out punctuation txt = re.sub(r"([^\w\s]|_)", " ", txt) - # full caps words but leaves CamelCase / pascalCase + # full caps words but leaves camelCase / PascalCase words = [word.lower() if word.isupper() else word for word in txt.split()] txt = " ".join(words) - # attempt to split pascalCase and CamelCase + # attempt to split PascalCase and camelCase words = [] old_i = 0 for i, char in enumerate(txt): - # to avoid CamelCase getting leading empty append + # to avoid PascalCase getting leading empty append if char.isupper() and i != 0: words.append(txt[old_i:i]) old_i = i From c599e703473e79b6e3884872d5d30f7df15601ac Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Fri, 16 Feb 2024 21:42:54 +0400 Subject: [PATCH 13/17] Adjust variable name. -JK --- bot/exts/fun/fun.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 00a9b5bb4a..aba1aeb873 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -83,8 +83,8 @@ def conversion_func(text: str) -> str: return "".join( char.upper() if round(random.random()) else char.lower() for char in text ) - converted_text, embed = await self._clean_text(ctx, text, conversion_func) - await ctx.send(content=converted_text, embed=embed) + cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=cleaned_text, embed=embed) @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: @@ -95,8 +95,8 @@ def conversion_func(text: str) -> str: return "_".join( text.split() ) - converted_text, embed = await self._clean_text(ctx, text, conversion_func) - await ctx.send(content=converted_text, embed=embed) + cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=cleaned_text, embed=embed) @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: @@ -107,8 +107,8 @@ def conversion_func(text: str) -> str: return "".join( word.capitalize() for word in text.split() ) - converted_text, embed = await self._clean_text(ctx, text, conversion_func) - await ctx.send(content=converted_text, embed=embed) + cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=cleaned_text, embed=embed) @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: @@ -119,8 +119,8 @@ def conversion_func(text: str) -> str: return "_".join( word.upper() for word in text.split() ) - converted_text, embed = await self._clean_text(ctx, text, conversion_func) - await ctx.send(content=converted_text, embed=embed) + cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=cleaned_text, embed=embed) @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: @@ -131,8 +131,8 @@ def conversion_func(text: str) -> str: return "".join( word.capitalize() if i != 0 else word for i, word in enumerate(text.split()) ) - converted_text, embed = await self._clean_text(ctx, text, conversion_func) - await ctx.send(content=converted_text, embed=embed) + cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) + await ctx.send(content=cleaned_text, embed=embed) @commands.group(name="caesarcipher", aliases=("caesar", "cc",)) async def caesarcipher_group(self, ctx: Context) -> None: From 3686fd3f94154b8ac7cd936fee65d2c9f2d6496a Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Sun, 25 Feb 2024 20:30:17 +0400 Subject: [PATCH 14/17] Redo to have less .join and .split -JK --- bot/utils/helpers.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 6b9d82bb65..ab92de4dbc 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -7,29 +7,25 @@ def suppress_links(message: str) -> str: message = message.replace(link, f"<{link}>") return message -def neutralise_string(txt: str | None) -> str | None: +def neutralise_string(txt: str | None) -> list[str] | None: """Attempts to neutralise all punctuation and cases and returns a string of lowercase words.""" - # return early if no text provided. + # Return early if no text provided. if not txt: return None - # take out punctuation + # Take out punctuation. txt = re.sub(r"([^\w\s]|_)", " ", txt) - # full caps words but leaves camelCase / PascalCase - words = [word.lower() if word.isupper() else word for word in txt.split()] - txt = " ".join(words) - - # attempt to split PascalCase and camelCase words = [] - old_i = 0 - for i, char in enumerate(txt): - # to avoid PascalCase getting leading empty append - if char.isupper() and i != 0: - words.append(txt[old_i:i]) - old_i = i - words.append(txt[old_i:]) + for word in txt.split(): + if word.isupper(): + words.append(word.lower()) + else: + old_i = 0 + for i, char in enumerate(word): + if char.isupper() and i != 0: + words.append(word[old_i:i].lower()) + old_i = i + words.append(word[old_i:].lower()) - # strip white spaces and make lowercase - words = [word.strip().lower() for word in words] - return " ".join(words) + return words From 67863eed2d99445c20837e9e5c498ea0330c37b1 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Sun, 25 Feb 2024 20:30:40 +0400 Subject: [PATCH 15/17] Removed the no longer required .split(). -JK --- bot/exts/fun/fun.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index aba1aeb873..3e99694df6 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -93,7 +93,7 @@ async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" return "_".join( - text.split() + text ) cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=cleaned_text, embed=embed) @@ -105,7 +105,7 @@ async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: def conversion_func(text: str) -> str: """Converts the provided string to PascalCase.""" return "".join( - word.capitalize() for word in text.split() + word.capitalize() for word in text ) cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=cleaned_text, embed=embed) @@ -117,7 +117,7 @@ async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" return "_".join( - word.upper() for word in text.split() + word.upper() for word in text ) cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=cleaned_text, embed=embed) @@ -129,7 +129,7 @@ async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: def conversion_func(text: str) -> str: """Converts the provided string to camelCase.""" return "".join( - word.capitalize() if i != 0 else word for i, word in enumerate(text.split()) + word.capitalize() if i != 0 else word for i, word in enumerate(text) ) cleaned_text, embed = await self._clean_text(ctx, text, conversion_func) await ctx.send(content=cleaned_text, embed=embed) From 03d73f0f298766c09aa0d62dc3950109c8f553b3 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Sun, 25 Feb 2024 20:33:39 +0400 Subject: [PATCH 16/17] Changed regex command. -JK --- bot/utils/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index ab92de4dbc..df71e813d4 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -14,7 +14,7 @@ def neutralise_string(txt: str | None) -> list[str] | None: return None # Take out punctuation. - txt = re.sub(r"([^\w\s]|_)", " ", txt) + txt = re.sub(r"[\W_]", " ", txt) words = [] for word in txt.split(): From 79a2afe4438b211a1834b418b784fd7425afdd69 Mon Sep 17 00:00:00 2001 From: JelmerKorten Date: Sun, 25 Feb 2024 21:43:22 +0400 Subject: [PATCH 17/17] Move neutralise string into conversion func. -JK --- bot/exts/fun/fun.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 3e99694df6..88e3eef0ad 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -89,9 +89,9 @@ def conversion_func(text: str) -> str: @commands.command(name="snakecase", aliases=("scase",)) async def snakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to snake_case.""" - text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to snake_case.""" + text = helpers.neutralise_string(text) return "_".join( text ) @@ -101,9 +101,9 @@ def conversion_func(text: str) -> str: @commands.command(name="pascalcase", aliases=("pcase", "pascal",)) async def pascalcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to PascalCase.""" - text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to PascalCase.""" + text = helpers.neutralise_string(text) return "".join( word.capitalize() for word in text ) @@ -113,9 +113,9 @@ def conversion_func(text: str) -> str: @commands.command(name="screamingsnakecase", aliases=("screamsnake", "ssnake", "screamingsnake",)) async def screamingsnakecase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to SCREAMING_SNAKE_CASE.""" - text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to SCREAMING_SNAKE_CASE.""" + text = helpers.neutralise_string(text) return "_".join( word.upper() for word in text ) @@ -125,9 +125,9 @@ def conversion_func(text: str) -> str: @commands.command(name="camelcase", aliases=("ccase", "camel",)) async def camelcase_command(self, ctx: Context, *, text: str | None) -> None: """Attempts to convert the provided string to camelCase.""" - text = helpers.neutralise_string(text) def conversion_func(text: str) -> str: """Converts the provided string to camelCase.""" + text = helpers.neutralise_string(text) return "".join( word.capitalize() if i != 0 else word for i, word in enumerate(text) )