Skip to content

Transformers

ballsdex.core.utils.transformers

This file contains discord.py transformers used to provide autocompletion, parsing and validation for various Ballsdex models.

ModelTransformer

ModelTransformer(**filters: Any)

Bases: Transformer, Converter

Base abstract class for autocompletion from on Django models

This also works with hybrid commands.

Attributes:

  • name (str) –

    Name to qualify the object being listed

  • column (str) –

    Column of the model to use for matching text-based conversions. Defaults to "name".

  • model (T) –

    The Django model associated to the class derivation

  • base (int) –

    The base in which database IDs are converted. Defaults to decimal (10).

Parameters:

  • **filters (Any, default: {} ) –

    Filters to apply on the model before providing options.

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

get_options

get_options(interaction: Interaction[BallsDexBot], value: str) -> list[Choice[int]]

Generate the list of options for autocompletion

Source code in ballsdex/core/utils/transformers.py
async def get_options(
    self, interaction: discord.Interaction["BallsDexBot"], value: str
) -> list[app_commands.Choice[int]]:
    """
    Generate the list of options for autocompletion
    """
    raise NotImplementedError()

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

BallInstanceTransformer

BallInstanceTransformer(**filters: Any)

Bases: ModelTransformer[BallInstance]

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

TTLModelTransformer

TTLModelTransformer(**filters: Any)

Bases: ModelTransformer[T]

Base class for simple Django model autocompletion with TTL cache.

This is used in most cases except for BallInstance which requires special handling depending on the interaction passed.

Attributes:

  • ttl (float) –

    Delay in seconds for items to live until refreshed with load_items, defaults to 300

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    super().__init__(**filters)
    self.items: dict[int, T] = {}
    self.search_map: dict[T, str] = {}
    self.last_refresh: float = 0
    log.debug(f"Inited transformer for {self.name}")

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

load_items

load_items() -> Iterable[T]

Query values to fill items with.

Source code in ballsdex/core/utils/transformers.py
async def load_items(self) -> Iterable[T]:
    """
    Query values to fill `items` with.
    """
    return [x async for x in self.model.objects.all()]

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

BallTransformer

BallTransformer(**filters: Any)

Bases: TTLModelTransformer[Ball]

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

get_options

get_options(interaction: Interaction[BallsDexBot], value: str) -> list[Choice[int]]

Generate the list of options for autocompletion

Source code in ballsdex/core/utils/transformers.py
async def get_options(
    self, interaction: discord.Interaction["BallsDexBot"], value: str
) -> list[app_commands.Choice[int]]:
    """
    Generate the list of options for autocompletion
    """
    raise NotImplementedError()

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

load_items

load_items() -> Iterable[T]

Query values to fill items with.

Source code in ballsdex/core/utils/transformers.py
async def load_items(self) -> Iterable[T]:
    """
    Query values to fill `items` with.
    """
    return [x async for x in self.model.objects.all()]

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

SpecialTransformer

SpecialTransformer(**filters: Any)

Bases: TTLModelTransformer[Special]

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

get_options

get_options(interaction: Interaction[BallsDexBot], value: str) -> list[Choice[int]]

Generate the list of options for autocompletion

Source code in ballsdex/core/utils/transformers.py
async def get_options(
    self, interaction: discord.Interaction["BallsDexBot"], value: str
) -> list[app_commands.Choice[int]]:
    """
    Generate the list of options for autocompletion
    """
    raise NotImplementedError()

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

load_items

load_items() -> Iterable[T]

Query values to fill items with.

Source code in ballsdex/core/utils/transformers.py
async def load_items(self) -> Iterable[T]:
    """
    Query values to fill `items` with.
    """
    return [x async for x in self.model.objects.all()]

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

RegimeTransformer

RegimeTransformer(**filters: Any)

Bases: TTLModelTransformer[Regime]

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

get_options

get_options(interaction: Interaction[BallsDexBot], value: str) -> list[Choice[int]]

Generate the list of options for autocompletion

Source code in ballsdex/core/utils/transformers.py
async def get_options(
    self, interaction: discord.Interaction["BallsDexBot"], value: str
) -> list[app_commands.Choice[int]]:
    """
    Generate the list of options for autocompletion
    """
    raise NotImplementedError()

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

load_items

load_items() -> Iterable[T]

Query values to fill items with.

Source code in ballsdex/core/utils/transformers.py
async def load_items(self) -> Iterable[T]:
    """
    Query values to fill `items` with.
    """
    return [x async for x in self.model.objects.all()]

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

EconomyTransformer

EconomyTransformer(**filters: Any)

Bases: TTLModelTransformer[Economy]

Source code in ballsdex/core/utils/transformers.py
def __init__(self, **filters: Any):
    self.filters = filters

get_from_pk

get_from_pk(value: int) -> T

Return a Django model instance from a primary key.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_pk(self, value: int) -> T:
    """
    Return a Django model instance from a primary key.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(pk=value)

get_from_text

get_from_text(value: str) -> T

Return a Django model instance from the raw value entered.

Raises:

  • KeyError | DoesNotExist

    Entry does not exist

Source code in ballsdex/core/utils/transformers.py
async def get_from_text(self, value: str) -> T:
    """
    Return a Django model instance from the raw value entered.

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.get_queryset().aget(**{f"{self.column}__iexact": value})

get_options

get_options(interaction: Interaction[BallsDexBot], value: str) -> list[Choice[int]]

Generate the list of options for autocompletion

Source code in ballsdex/core/utils/transformers.py
async def get_options(
    self, interaction: discord.Interaction["BallsDexBot"], value: str
) -> list[app_commands.Choice[int]]:
    """
    Generate the list of options for autocompletion
    """
    raise NotImplementedError()

key

key(model: T) -> str

Return a string used for searching while sending autocompletion suggestions.

Source code in ballsdex/core/utils/transformers.py
def key(self, model: T) -> str:
    """
    Return a string used for searching while sending autocompletion suggestions.
    """
    return getattr(model, self.column)

load_items

load_items() -> Iterable[T]

Query values to fill items with.

Source code in ballsdex/core/utils/transformers.py
async def load_items(self) -> Iterable[T]:
    """
    Query values to fill `items` with.
    """
    return [x async for x in self.model.objects.all()]

validate

validate(ctx: Context[BallsDexBot], item: T)

A function to validate the fetched item before calling back the command.

Raises:

  • BadArgument

    Raised if the item does not pass validation with the message to be displayed

Source code in ballsdex/core/utils/transformers.py
async def validate(self, ctx: commands.Context["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    commands.BadArgument
        Raised if the item does not pass validation with the message to be displayed
    """
    pass

TradeCommandType

Bases: Enum

If a command is using BallInstanceTransformer for trading purposes, it should define this enum to filter out values.