Skip to content

converters

Discord.py command converters for Tux bot.

This module provides custom converters for parsing command arguments, including time durations, case types, and utility functions for channel resolution and boolean conversion.

Classes:

  • TimeConverter

    Convert string representations of time durations to seconds.

  • CaseTypeConverter

    Convert string representations to CaseType enum values.

Functions:

  • get_channel_safe

    Get a TextChannel or Thread by ID, returning None if not found.

  • convert_bool

    Convert a string to a boolean value.

Classes

TimeConverter

Bases: Converter[float]

Convert string representations of time durations to seconds.

Supports time units: s (seconds), m (minutes), h (hours), d (days). Examples: "1h30m", "2d", "45s", "1.5h".

Methods:

  • convert

    Convert a string representation of time (e.g., "1h30m", "2d") into seconds.

Functions

convert async
Python
convert(ctx: Context[Any], argument: str) -> float

Convert a string representation of time (e.g., "1h30m", "2d") into seconds.

Parameters:

  • ctx (Context[Any]) –

    The invocation context.

  • argument (str) –

    The time string to convert.

Returns:

  • float

    The total time in seconds.

Raises:

  • BadArgument

    If the time string format is invalid or uses invalid units.

CaseTypeConverter

Bases: Converter[CaseType]

Convert string representations to CaseType enum values.

Accepts case type names (case-insensitive) and converts them to the corresponding CaseType enum value for moderation commands.

Methods:

  • convert

    Convert a string to a CaseType enum.

Functions

convert async
Python
convert(ctx: Context[Any], argument: str) -> CaseType

Convert a string to a CaseType enum.

Parameters:

  • ctx (Context[Any]) –

    The context to convert the argument to a CaseType enum.

  • argument (str) –

    The argument to convert to a CaseType enum.

Returns:

Raises:

  • BadArgument

    If the argument is not a valid CaseType.

Functions

get_channel_safe async

Python
get_channel_safe(bot: Tux, channel_id: int) -> TextChannel | Thread | None

Get a TextChannel or Thread by ID, returning None if not found.

This narrows the return type so callers can safely use fetch_message and message.reactions.

Returns:

  • TextChannel | Thread | None

    The channel if found and is a text channel or thread, None otherwise.

convert_bool

Python
convert_bool(x: str | None) -> bool | None

Convert a string to a boolean value.

Parameters:

  • x (str | None) –

    The string to convert.

Returns:

  • bool | None

    The converted boolean value, or None if x is None.

Raises:

  • BadArgument

    If the string cannot be converted to a boolean.