Skip to content

tux.cogs.utility.encode_decode

Classes:

Name Description
EncodeDecode

Classes

EncodeDecode(bot: Tux)

Bases: Cog

Methods:

Name Description
encode

Encode text in a coding system.

decode

Decode text in a coding system.

Source code in tux/cogs/utility/encode_decode.py
Python
def __init__(self, bot: Tux) -> None:
    self.bot = bot
    self.encode.usage = generate_usage(self.encode)
    self.decode.usage = generate_usage(self.decode)

Functions

encode(ctx: commands.Context[Tux], codesystem: str, *, text: str) -> None async

Encode text in a coding system.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context of the command.

required
codesystem str

The coding system.

required
text str

The text you want to encode.

required
Source code in tux/cogs/utility/encode_decode.py
Python
@commands.hybrid_command(name="encode", aliases=["ec"], description="Encode a message")
@app_commands.describe(text="Text to encode")
@app_commands.choices(
    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],
    codesystem: str,
    *,
    text: str,
) -> None:
    """
    Encode text in a coding system.

    Parameters
    ----------
    ctx : commands.Context[Tux]
        The context of the command.
    codesystem : str
        The coding system.
    text : str
        The text you want to encode.
    """

    codesystem = codesystem.lower()
    btext = text.encode(encoding="utf-8")

    try:
        if codesystem == "base16":
            data = base64.b16encode(btext)
        elif codesystem == "base32":
            data = base64.b32encode(btext)
        elif codesystem == "base64":
            data = base64.b64encode(btext)
        elif codesystem == "base85":
            data = base64.b85encode(btext)
        else:
            await ctx.reply(
                content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', CODING_SYSTEMS))}",
                allowed_mentions=allowed_mentions,
                ephemeral=True,
            )
            return

        await self.send_message(ctx, data.decode(encoding="utf-8"))
    except Exception as e:
        await ctx.reply(
            content=f"Unknown excpetion: {type(e)}: {e}",
            allowed_mentions=allowed_mentions,
            ephemeral=True,
        )
decode(ctx: commands.Context[Tux], codesystem: str, *, text: str) -> None async

Decode text in a coding system.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context of the command.

required
codesystem str

The coding system.

required
text str

The text you want to decode.

required
Source code in tux/cogs/utility/encode_decode.py
Python
@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(
    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],
    codesystem: str,
    *,
    text: str,
) -> None:
    """
    Decode text in a coding system.

    Parameters
    ----------
    ctx : commands.Context[Tux]
        The context of the command.
    codesystem : str
        The coding system.
    text : str
        The text you want to decode.
    """

    codesystem = codesystem.lower()
    btext = text.encode(encoding="utf-8")

    try:
        if codesystem == "base16":
            data = base64.b16decode(btext)
        elif codesystem == "base32":
            data = base64.b32decode(btext)
        elif codesystem == "base64":
            data = base64.b64decode(btext)
        elif codesystem == "base85":
            data = base64.b85decode(btext)
        else:
            await ctx.reply(
                content=f"Invalid coding system. Please use: {', '.join(wrap_strings('`', CODING_SYSTEMS))}",
                allowed_mentions=allowed_mentions,
                ephemeral=True,
            )
            return

        await self.send_message(ctx, data.decode(encoding="utf-8"))
    except binascii.Error as e:
        await ctx.reply(
            content=f"Decoding error: {e}",
            ephemeral=True,
        )
        return
    except UnicodeDecodeError:
        await ctx.reply(
            content="The message was decoded, but the output is not valid UTF-8.",
            allowed_mentions=allowed_mentions,
            ephemeral=True,
        )
    except Exception as e:
        await ctx.reply(
            content=f"Unknown excpetion: {type(e)}: {e}",
            allowed_mentions=allowed_mentions,
            ephemeral=True,
        )

Functions