aboutsummaryrefslogtreecommitdiffstats
path: root/bot
diff options
context:
space:
mode:
authorGravatar PureFunctor <[email protected]>2020-07-15 15:03:13 +0800
committerGravatar PureFunctor <[email protected]>2020-07-15 15:03:13 +0800
commit411db10969387fbb8e89f87c78a39dc580cc6498 (patch)
treed87abd19d2aa2404ad031104eb6cc38980b3e64d /bot
parentAdd 'enc'/'dec' aliases for encrypt/decrypt (diff)
Refactor translation methods to avoid repetition
Diffstat (limited to 'bot')
-rw-r--r--bot/exts/evergreen/fun.py24
1 files changed, 12 insertions, 12 deletions
diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py
index 31dba936..759a9e2a 100644
--- a/bot/exts/evergreen/fun.py
+++ b/bot/exts/evergreen/fun.py
@@ -111,15 +111,21 @@ class Fun(Cog):
await ctx.send(embed=embed)
@staticmethod
- async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str]) -> None:
+ async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str], left_shift: bool = False) -> None:
"""
- Given an integer `offset`, translates and sends the given `msg`.
+ Given a positive integer `offset`, translates and sends the given `msg`.
- A positive `offset` will cause the letters to shift right, while
- a negative `offset` will cause the letters to shift left.
+ Performs a right shift by default unless `left_shift` is specified as `True`.
Also accepts a valid Discord Message ID or link.
"""
+ if offset < 0:
+ await ctx.send(":no_entry: Cannot use a negative offset.")
+ return
+
+ if left_shift:
+ offset = -offset
+
def caesar_func(text: str) -> Iterable[str]:
"""Implements a lazy Caesar Cipher algorithm."""
for char in text:
@@ -160,10 +166,7 @@ class Fun(Cog):
Also accepts a valid Discord Message ID or link.
"""
- if offset < 0:
- await ctx.send(":no_entry: Cannot use a negative offset.")
- else:
- await self._caesar_cipher(ctx, offset, msg)
+ await self._caesar_cipher(ctx, offset, msg, False)
@caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift", "dec",))
async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None:
@@ -174,10 +177,7 @@ class Fun(Cog):
Also accepts a valid Discord Message ID or link.
"""
- if offset < 0:
- await ctx.send(":no_entry: Cannot use a negative offset.")
- else:
- await self._caesar_cipher(ctx, -offset, msg)
+ await self._caesar_cipher(ctx, offset, msg, True)
@staticmethod
async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: