-
Notifications
You must be signed in to change notification settings - Fork 1
/
depypher.py
77 lines (62 loc) · 2.3 KB
/
depypher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import discord, functools
from typing import Callable, Final, List, Literal, Optional
from pycipher import pycipher
from discord.ext import commands
_caesar: Final[pycipher.Caesar] = pycipher.Caesar(key=4)
_atbash: Final[pycipher.Atbash] = pycipher.Atbash()
_vigenere = pycipher.Vigenere
_porta = pycipher.Porta
def convert_case(original: str, new: str) -> str:
ret = ""
for (
index,
letter,
) in enumerate(original):
if not letter.isalpha():
ret += letter
new = f"{new[:index]}{letter}{new[index + 1:]}"
continue
if letter.isupper():
ret += new[index].upper()
else:
ret += new[index].lower()
return ret
class Depypher(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def caesar(self, ctx, *, message: str):
await self._process_message(
ctx, _caesar.encipher(string=message, keep_punct=True), message
)
@commands.command()
async def decaesar(self, ctx, *, cipher: str):
await self._process_message(
ctx, _caesar.decipher(string=cipher, keep_punct=True), cipher
)
@commands.command()
async def vigenere(self, ctx, keyword: str, *, message: str):
await self._process_message(
ctx, _vigenere(key=keyword).encipher(message), message
)
@commands.command()
async def devigenere(self, ctx, keyword: str, *, cipher: str):
await self._process_message(
ctx, _vigenere(key=keyword).decipher(cipher), cipher
)
@commands.command()
async def porta(self, ctx, keyword: str, *, message: str):
await self._process_message(ctx, _porta(key=keyword).encipher(message), message)
@commands.command()
async def deporta(self, ctx, keyword: str, *, cipher: str):
await self._process_message(ctx, _porta(key=keyword).decipher(cipher), cipher)
@staticmethod
async def _process_message(
ctx: commands.Context, msg: str, original: str
) -> discord.Message:
async with ctx.typing():
case = functools.partial(convert_case, original, msg)
processed = await ctx.bot.loop.run_in_executor(None, case)
return await ctx.send(processed)
def setup(bot):
bot.add_cog(Depypher(bot))