diff options
author | 2020-06-13 23:06:37 +0800 | |
---|---|---|
committer | 2020-06-14 11:23:39 +0800 | |
commit | 4b871609190ce5ecb9eb47c5e1f7e3a42effd989 (patch) | |
tree | 5d03c98ef8504aa2ec7b874deba6b867c07c4f07 | |
parent | Merge pull request #413 from jodth07/encoding_bug_fix (diff) |
Add initial caesarcipher command
-rw-r--r-- | bot/exts/evergreen/fun.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 67a4bae5..5f4c5b3e 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -87,6 +87,40 @@ class Fun(Cog): converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) + @commands.command(name="caesarcipher", aliases=("caesar",)) + async def caesarcipher_command(self, ctx: Context, offset: int, *, text: str) -> None: + """ + Given an integer `offset`, encrypt the given `text`. + + A positive `offset` will cause the letters to shift right, + while a negative `offset` will cause the letters to shift left. + + Also accepts a valid discord Message ID or link. + """ + + def cipher_func(text: str) -> str: + """Implements a lazy Caesar cipher algorithm.""" + for char in text: + if not char.isascii() or not char.isalpha() or char.isspace(): + yield char + continue + case_start = 65 if char.isupper() else 97 + yield chr((ord(char) - case_start + offset) % 26 + case_start) + + def conversion_func(text: str) -> str: + """Encrypts the given string using the Caesar cipher.""" + return "".join(cipher_func(text)) + + text, embed = await Fun._get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = Fun._convert_embed(conversion_func, embed) + converted_text = conversion_func(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) + @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: """ |