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.

T

T = TypeVar('T', bound=Model)

ModelTransformer

Bases: Transformer, Generic[T]

Base abstract class for autocompletion from on Tortoise models

Attributes:

  • name (str) –

    Name to qualify the object being listed

  • model (T) –

    The Tortoise model associated to the class derivation

get_from_pk

get_from_pk(value: int) -> T

Return a Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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.
    """
    raise NotImplementedError()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

BallInstanceTransformer

Bases: ModelTransformer[BallInstance]

key

key(model: BallInstance) -> 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.
    """
    raise NotImplementedError()

TTLModelTransformer

TTLModelTransformer()

Bases: ModelTransformer[T]

Base class for simple Tortoise 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):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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.
    """
    raise NotImplementedError()

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(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

BallTransformer

BallTransformer()

Bases: TTLModelTransformer[Ball]

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

BallEnabledTransformer

BallEnabledTransformer()

Bases: BallTransformer

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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.
    """
    raise NotImplementedError()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

SpecialTransformer

SpecialTransformer()

Bases: TTLModelTransformer[Special]

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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()

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(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

SpecialEnabledTransformer

SpecialEnabledTransformer()

Bases: SpecialTransformer

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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.
    """
    raise NotImplementedError()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

RegimeTransformer

RegimeTransformer()

Bases: TTLModelTransformer[Regime]

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

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

EconomyTransformer

EconomyTransformer()

Bases: TTLModelTransformer[Economy]

Source code in ballsdex/core/utils/transformers.py
def __init__(self):
    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 Tortoise model instance from a primary key.

Raises:

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

    Raises
    ------
    KeyError | django.db.models.Model.DoesNotExist
        Entry does not exist
    """
    return await self.model.objects.aget(pk=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()

validate

validate(interaction: Interaction[BallsDexBot], item: T)

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

Raises:

  • ValidationError

    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, interaction: discord.Interaction["BallsDexBot"], item: T):
    """
    A function to validate the fetched item before calling back the command.

    Raises
    ------
    ValidationError
        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.

ValidationError

ValidationError(message: str)

Bases: Exception

Raised when an autocomplete result is forbidden and should raise a user message.

Source code in ballsdex/core/utils/transformers.py
def __init__(self, message: str):
    self.message = message