Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions tux/cogs/utility/encode_decode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import binascii

from discord import AllowedMentions
from discord import AllowedMentions, app_commands

Check warning on line 4 in tux/cogs/utility/encode_decode.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/encode_decode.py#L4

Added line #L4 was not covered by tests
from discord.ext import commands

from tux.bot import Tux
Expand Down Expand Up @@ -44,16 +44,23 @@
await ctx.reply(
content=data,
allowed_mentions=allowed_mentions,
ephemeral=False,
ephemeral=True,
)

@commands.hybrid_command(
name="encode",
@commands.hybrid_command(name="encode", aliases=["ec"], description="Encode a message")
@app_commands.describe(text="Text to encode")
@app_commands.choices(

Check warning on line 52 in tux/cogs/utility/encode_decode.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/encode_decode.py#L50-L52

Added lines #L50 - L52 were not covered by tests
codesystem=[
app_commands.Choice(name="base16", value="base16"),
app_commands.Choice(name="base32", value="base32"),
app_commands.Choice(name="base64", value="base64"),
app_commands.Choice(name="base85", value="base85"),
],
)
async def encode(
self,
ctx: commands.Context[Tux],
cs: str,
codesystem: str,
*,
text: str,
) -> None:
Expand All @@ -64,23 +71,23 @@
----------
ctx : commands.Context[Tux]
The context of the command.
cs : str
codesystem : str
The coding system.
text : str
The text you want to encode.
"""

cs = cs.lower()
codesystem = codesystem.lower()

Check warning on line 80 in tux/cogs/utility/encode_decode.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/encode_decode.py#L80

Added line #L80 was not covered by tests
btext = text.encode(encoding="utf-8")

try:
if cs == "base16":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the encoding and decoding logic to use shared mappings and choices for code systems.

Suggested change
if cs == "base16":
# At module top, define mappings and shared choices
ENCODERS = {
"base16": base64.b16encode,
"base32": base64.b32encode,
"base64": base64.b64encode,
"base85": base64.b85encode,
}
DECODERS = {
"base16": base64.b16decode,
"base32": base64.b32decode,
"base64": base64.b64decode,
"base85": base64.b85decode,
}
CODE_CHOICES = [
app_commands.Choice(name=name, value=name)
for name in ENCODERS.keys()
]
# Reuse CODE_CHOICES in both commands:
@commands.hybrid_command(
name="encode", aliases=["ec"], description="Encode a message"
)
@app_commands.describe(text="Text to encode")
@app_commands.choices(codesystem=CODE_CHOICES)
async def encode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str):
btext = text.encode("utf-8")
func = ENCODERS.get(codesystem.lower())
if not func:
return await ctx.reply(
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', ENCODERS))}",
allowed_mentions=allowed_mentions, ephemeral=True
)
try:
data = func(btext)
await self.send_message(ctx, data.decode("utf-8"))
except Exception as e:
await ctx.reply(
content=f"Unknown exception: {e.__class__.__name__}: {e}",
allowed_mentions=allowed_mentions, ephemeral=True
)
@commands.hybrid_command(
name="decode", aliases=["dc"], description="Decode a message"
)
@app_commands.describe(text="Text to decode")
@app_commands.choices(codesystem=CODE_CHOICES)
async def decode(self, ctx: commands.Context[Tux], codesystem: str, *, text: str):
btext = text.encode("utf-8")
func = DECODERS.get(codesystem.lower())
if not func:
return await ctx.reply(
content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', DECODERS))}",
allowed_mentions=allowed_mentions, ephemeral=True
)
try:
data = func(btext)
await self.send_message(ctx, data.decode("utf-8"))
except Exception as e:
await ctx.reply(
content=f"Unknown exception: {e.__class__.__name__}: {e}",
allowed_mentions=allowed_mentions, ephemeral=True
)

if codesystem == "base16":
data = base64.b16encode(btext)
elif cs == "base32":
elif codesystem == "base32":
data = base64.b32encode(btext)
elif cs == "base64":
elif codesystem == "base64":
data = base64.b64encode(btext)
elif cs == "base85":
elif codesystem == "base85":
data = base64.b85encode(btext)
else:
await ctx.reply(
Expand All @@ -98,13 +105,21 @@
ephemeral=True,
)

@commands.hybrid_command(
name="decode",
@commands.hybrid_command(name="decode", aliases=["dc"], description="Decode a message")
@app_commands.describe(codesystem="Which Coding System to use")
@app_commands.describe(text="Text to decode")
@app_commands.choices(

Check warning on line 111 in tux/cogs/utility/encode_decode.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/encode_decode.py#L108-L111

Added lines #L108 - L111 were not covered by tests
codesystem=[
app_commands.Choice(name="base16", value="base16"),
app_commands.Choice(name="base32", value="base32"),
app_commands.Choice(name="base64", value="base64"),
app_commands.Choice(name="base85", value="base85"),
],
)
async def decode(
self,
ctx: commands.Context[Tux],
cs: str,
codesystem: str,
*,
text: str,
) -> None:
Expand All @@ -115,23 +130,23 @@
----------
ctx : commands.Context[Tux]
The context of the command.
cs : str
codesystem : str
The coding system.
text : str
The text you want to decode.
"""

cs = cs.lower()
codesystem = codesystem.lower()

Check warning on line 139 in tux/cogs/utility/encode_decode.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/encode_decode.py#L139

Added line #L139 was not covered by tests
btext = text.encode(encoding="utf-8")

try:
if cs == "base16":
if codesystem == "base16":
data = base64.b16decode(btext)
elif cs == "base32":
elif codesystem == "base32":
data = base64.b32decode(btext)
elif cs == "base64":
elif codesystem == "base64":
data = base64.b64decode(btext)
elif cs == "base85":
elif codesystem == "base85":
data = base64.b85decode(btext)
else:
await ctx.reply(
Expand All @@ -145,6 +160,7 @@
except binascii.Error as e:
await ctx.reply(
content=f"Decoding error: {e}",
ephemeral=True,
)
return
except UnicodeDecodeError:
Expand Down