Skip to content

tux.utils.converters

Classes:

Name Description
TimeConverter
CaseTypeConverter

Functions:

Name Description
convert_bool

Convert a string to a boolean value.

Classes

TimeConverter

Bases: Converter[float]

Methods:

Name Description
convert

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

Functions

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

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

Parameters:

Name Type Description Default
ctx Context[Any]

The invocation context.

required
argument str

The time string to convert.

required

Returns:

Type Description
float

The total time in seconds.

Raises:

Type Description
BadArgument

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

Source code in tux/utils/converters.py
Python
async def convert(self, ctx: commands.Context[Any], argument: str) -> float:
    """
    Convert a string representation of time (e.g., "1h30m", "2d") into seconds.

    Parameters
    ----------
    ctx : commands.Context[Any]
        The invocation context.
    argument : str
        The time string to convert.

    Returns
    -------
    float
        The total time in seconds.

    Raises
    ------
    commands.BadArgument
        If the time string format is invalid or uses invalid units.
    """
    matches = time_regex.findall(argument.lower())
    time = 0.0
    if not matches:
        msg = "Invalid time format. Use digits followed by s, m, h, or d (e.g., '1h30m')."
        raise commands.BadArgument(msg)

    for v, k in matches:
        try:
            # Replace comma with dot for float conversion if necessary
            processed_v = v.replace(",", ".")
            time += time_dict[k] * float(processed_v)
        except KeyError as e:
            msg = f"'{k}' is an invalid time unit. Use s, m, h, or d."
            raise commands.BadArgument(msg) from e
        except ValueError as e:
            msg = f"Could not convert '{v}' to a number."
            raise commands.BadArgument(msg) from e
    return time

CaseTypeConverter

Bases: Converter[CaseType]

Methods:

Name Description
convert

Convert a string to a CaseType enum.

Functions

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

Convert a string to a CaseType enum.

Parameters:

Name Type Description Default
ctx Context[Any]

The context to convert the argument to a CaseType enum.

required
argument str

The argument to convert to a CaseType enum.

required

Returns:

Type Description
CaseType

The CaseType enum.

Source code in tux/utils/converters.py
Python
async def convert(self, ctx: commands.Context[Any], argument: str) -> CaseType:
    """
    Convert a string to a CaseType enum.

    Parameters
    ----------
    ctx : commands.Context[Any]
        The context to convert the argument to a CaseType enum.
    argument : str
        The argument to convert to a CaseType enum.

    Returns
    -------
    CaseType
        The CaseType enum.
    """

    try:
        return CaseType[argument.upper()]
    except KeyError as e:
        msg = f"Invalid CaseType: {argument}"
        raise commands.BadArgument(msg) from e

Functions

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

Convert a string to a boolean value.

Parameters:

Name Type Description Default
x str | None

The string to convert.

required

Returns:

Type Description
bool | None

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

Raises:

Type Description
BadArgument

If the string cannot be converted to a boolean.

Source code in tux/utils/converters.py
Python
def 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
    ------
    commands.BadArgument
        If the string cannot be converted to a boolean.
    """
    if x is None:
        return None

    x = str(x).lower()

    if x in {"true", "t", "yes", "y", "1", "on", "active", "enable", "enabled"}:
        return True
    if x in {"false", "f", "no", "n", "0", "off", "inactive", "disable", "disabled"}:
        return False

    msg = f"{x} must be a boolean value (e.g. true/false, yes/no)"
    raise commands.BadArgument(msg)