Skip to content

Commit 3e4f69e

Browse files
Artur Manuelanemoijereja-eden
authored andcommitted
feat(encode_decode): add encoder and decoder for multiple coding systems
1 parent d12b15e commit 3e4f69e

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

tux/cogs/utility/encode_decode.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import base64
2+
import binascii
3+
4+
from discord import AllowedMentions
5+
from discord.ext import commands
6+
7+
from tux.bot import Tux
8+
from tux.utils.functions import generate_usage
9+
10+
11+
def wrap_strings(wrapper: str, contents: list[str]) -> list[str]:
12+
return [f"{wrapper}{content}{wrapper}" for content in contents]
13+
14+
15+
allowed_mentions: AllowedMentions = AllowedMentions(
16+
everyone=False,
17+
users=False,
18+
roles=False,
19+
)
20+
21+
CODING_SYSTEMS = [
22+
"base16",
23+
"base32",
24+
"base64",
25+
"base85",
26+
]
27+
28+
29+
class EncodeDecode(commands.Cog):
30+
def __init__(self, bot: Tux) -> None:
31+
self.bot = bot
32+
self.encode.usage = generate_usage(self.encode)
33+
self.decode.usage = generate_usage(self.decode)
34+
35+
async def send_message(self, ctx: commands.Context[Tux], data: str):
36+
if len(data) > 2000:
37+
await ctx.reply(
38+
content="The string ended up being too long. Please use this [site](https://www.base64encode.org/) instead.",
39+
allowed_mentions=allowed_mentions,
40+
ephemeral=True,
41+
)
42+
return
43+
44+
await ctx.reply(
45+
content=data,
46+
allowed_mentions=allowed_mentions,
47+
ephemeral=False,
48+
)
49+
50+
@commands.hybrid_command(
51+
name="encode",
52+
)
53+
async def encode(
54+
self,
55+
ctx: commands.Context[Tux],
56+
cs: str,
57+
*,
58+
text: str,
59+
) -> None:
60+
"""
61+
Encode text in a coding system.
62+
63+
Parameters
64+
----------
65+
ctx : commands.Context[Tux]
66+
The context of the command.
67+
cs : str
68+
The coding system.
69+
text : str
70+
The text you want to encode.
71+
"""
72+
73+
cs = cs.lower()
74+
btext = text.encode(encoding="utf-8")
75+
76+
try:
77+
if cs == "base16":
78+
data = base64.b16encode(btext)
79+
elif cs == "base32":
80+
data = base64.b32encode(btext)
81+
elif cs == "base64":
82+
data = base64.b64encode(btext)
83+
elif cs == "base85":
84+
data = base64.b85encode(btext)
85+
else:
86+
await ctx.reply(
87+
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', CODING_SYSTEMS))}",
88+
allowed_mentions=allowed_mentions,
89+
ephemeral=True,
90+
)
91+
return
92+
93+
await self.send_message(ctx, data.decode(encoding="utf-8"))
94+
except Exception as e:
95+
await ctx.reply(
96+
content=f"Unknown excpetion: {type(e)}: {e}",
97+
allowed_mentions=allowed_mentions,
98+
ephemeral=True,
99+
)
100+
101+
@commands.hybrid_command(
102+
name="decode",
103+
)
104+
async def decode(
105+
self,
106+
ctx: commands.Context[Tux],
107+
cs: str,
108+
*,
109+
text: str,
110+
) -> None:
111+
"""
112+
Decode text in a coding system.
113+
114+
Parameters
115+
----------
116+
ctx : commands.Context[Tux]
117+
The context of the command.
118+
cs : str
119+
The coding system.
120+
text : str
121+
The text you want to decode.
122+
"""
123+
124+
cs = cs.lower()
125+
btext = text.encode(encoding="utf-8")
126+
127+
try:
128+
if cs == "base16":
129+
data = base64.b16decode(btext)
130+
elif cs == "base32":
131+
data = base64.b32decode(btext)
132+
elif cs == "base64":
133+
data = base64.b64decode(btext)
134+
elif cs == "base85":
135+
data = base64.b85decode(btext)
136+
else:
137+
await ctx.reply(
138+
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', CODING_SYSTEMS))}",
139+
allowed_mentions=allowed_mentions,
140+
ephemeral=True,
141+
)
142+
return
143+
144+
await self.send_message(ctx, data.decode(encoding="utf-8"))
145+
except binascii.Error as e:
146+
await ctx.reply(
147+
content=f"Decoding error: {e}",
148+
)
149+
return
150+
except UnicodeDecodeError:
151+
await ctx.reply(
152+
content="The message was decoded, but the output is not valid UTF-8.",
153+
allowed_mentions=allowed_mentions,
154+
ephemeral=True,
155+
)
156+
except Exception as e:
157+
await ctx.reply(
158+
content=f"Unknown excpetion: {type(e)}: {e}",
159+
allowed_mentions=allowed_mentions,
160+
ephemeral=True,
161+
)
162+
163+
164+
async def setup(bot: Tux):
165+
await bot.add_cog(EncodeDecode(bot))

0 commit comments

Comments
 (0)