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], cs: 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
cs 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",
)
async def encode(
    self,
    ctx: commands.Context[Tux],
    cs: str,
    *,
    text: str,
) -> None:
    """
    Encode text in a coding system.

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

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

    try:
        if cs == "base16":
            data = base64.b16encode(btext)
        elif cs == "base32":
            data = base64.b32encode(btext)
        elif cs == "base64":
            data = base64.b64encode(btext)
        elif cs == "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], cs: 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
cs 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",
)
async def decode(
    self,
    ctx: commands.Context[Tux],
    cs: str,
    *,
    text: str,
) -> None:
    """
    Decode text in a coding system.

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

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

    try:
        if cs == "base16":
            data = base64.b16decode(btext)
        elif cs == "base32":
            data = base64.b32decode(btext)
        elif cs == "base64":
            data = base64.b64decode(btext)
        elif cs == "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}",
        )
        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