Module aethersprite.filters

Setting filters module

Expand source code
"""Setting filters module"""

from .boolean_filter import BooleanFilter
from .channel_filter import ChannelFilter
from .role_filter import RoleFilter
from .seconds_filter import SecondsFilter
from .setting_filter import SettingFilter

__all__ = (
    "BooleanFilter",
    "ChannelFilter",
    "RoleFilter",
    "SecondsFilter",
    "SettingFilter",
)

Sub-modules

aethersprite.filters.boolean_filter

Boolean setting filter

aethersprite.filters.channel_filter

Channel setting filter

aethersprite.filters.role_filter
aethersprite.filters.seconds_filter

Seconds setting filter

aethersprite.filters.setting_filter

Setting filter base class

Classes

class BooleanFilter (setting: str)

Filter used for converting strings to boolean values

Expand source code
class BooleanFilter(SettingFilter):
    """Filter used for converting strings to boolean values"""

    def __init__(self, setting: str):
        super().__init__(setting)

    def in_(self, ctx: Context, value: str | None) -> bool | None:
        """
        Filter setting input.

        Args:
            ctx: The current context
            value: The incoming value

        Returns:
            The raw setting value (a boolean)
        """

        if value is None:
            return

        # boolean settings are just toggles; if they're set, they are the
        # opposite of the default
        return not settings[self.setting].default

    def out(
        self,
        ctx: Context,
        value: bool | None,
    ) -> bool | None:
        """
        Filter setting output.

        Args:
            ctx: The current context
            value: The raw setting value (a boolean)

        Returns:
            The raw setting value
        """

        return value

Ancestors

Methods

def in_(self, ctx: discord.ext.commands.context.Context, value: str | None) ‑> bool | None

Filter setting input.

Args

ctx
The current context
value
The incoming value

Returns

The raw setting value (a boolean)

Expand source code
def in_(self, ctx: Context, value: str | None) -> bool | None:
    """
    Filter setting input.

    Args:
        ctx: The current context
        value: The incoming value

    Returns:
        The raw setting value (a boolean)
    """

    if value is None:
        return

    # boolean settings are just toggles; if they're set, they are the
    # opposite of the default
    return not settings[self.setting].default
def out(self, ctx: discord.ext.commands.context.Context, value: bool | None) ‑> bool | None

Filter setting output.

Args

ctx
The current context
value
The raw setting value (a boolean)

Returns

The raw setting value

Expand source code
def out(
    self,
    ctx: Context,
    value: bool | None,
) -> bool | None:
    """
    Filter setting output.

    Args:
        ctx: The current context
        value: The raw setting value (a boolean)

    Returns:
        The raw setting value
    """

    return value

Inherited members

class ChannelFilter (setting: str, multiple: bool = False)

Filter used for converting channel names to IDs and back

Expand source code
class ChannelFilter(SettingFilter):
    """Filter used for converting channel names to IDs and back"""

    multiple: bool = False
    """True to allow multiple values"""

    def __init__(self, setting: str, multiple: bool = False):
        super().__init__(setting)
        self.multiple = multiple

    def in_(self, ctx: Context, value: str | None) -> list[int] | None:
        """
        Filter setting input.

        Args:
            ctx: The current context
            value: The incoming value

        Returns:
            The raw setting value (a list of channel IDs)
        """

        if not value:
            return

        channels = get_mixed_channels(value)
        ids = []

        for c in channels:
            id = None

            if c[0] == "":
                id = get_id_for_channel(ctx.guild, c[1])
            else:
                # convert mentions, if any
                id = int(c[0])

            if id is None:
                raise ValueError()

            ids.append(id)

            if not self.multiple:
                break

        return ids

    def out(
        self,
        ctx: Context,
        value: list[int] | None,
    ) -> list[str | None] | str | None:
        """
        Filter setting output.

        Args:
            ctx: The current context
            value: The raw setting value: a list of channel IDs

        Returns:
            The filtered setting value (a list of channel names)
        """

        if value is None:
            return

        channels = (
            [get_channel_for_id(ctx.guild, value)]
            if value is int
            else [get_channel_for_id(ctx.guild, v) for v in value]
        )

        if self.multiple:
            return channels

        if len(channels) > 0:
            return channels[0]

        return None

Ancestors

Class variables

var multiple : bool

True to allow multiple values

Methods

def in_(self, ctx: discord.ext.commands.context.Context, value: str | None) ‑> list[int] | None

Filter setting input.

Args

ctx
The current context
value
The incoming value

Returns

The raw setting value (a list of channel IDs)

Expand source code
def in_(self, ctx: Context, value: str | None) -> list[int] | None:
    """
    Filter setting input.

    Args:
        ctx: The current context
        value: The incoming value

    Returns:
        The raw setting value (a list of channel IDs)
    """

    if not value:
        return

    channels = get_mixed_channels(value)
    ids = []

    for c in channels:
        id = None

        if c[0] == "":
            id = get_id_for_channel(ctx.guild, c[1])
        else:
            # convert mentions, if any
            id = int(c[0])

        if id is None:
            raise ValueError()

        ids.append(id)

        if not self.multiple:
            break

    return ids
def out(self, ctx: discord.ext.commands.context.Context, value: list[int] | None) ‑> list[str | None] | str | None

Filter setting output.

Args

ctx
The current context
value
The raw setting value: a list of channel IDs

Returns

The filtered setting value (a list of channel names)

Expand source code
def out(
    self,
    ctx: Context,
    value: list[int] | None,
) -> list[str | None] | str | None:
    """
    Filter setting output.

    Args:
        ctx: The current context
        value: The raw setting value: a list of channel IDs

    Returns:
        The filtered setting value (a list of channel names)
    """

    if value is None:
        return

    channels = (
        [get_channel_for_id(ctx.guild, value)]
        if value is int
        else [get_channel_for_id(ctx.guild, v) for v in value]
    )

    if self.multiple:
        return channels

    if len(channels) > 0:
        return channels[0]

    return None

Inherited members

class RoleFilter (setting: str, multiple: bool = True)

Filter used for converting role names to IDs and back

Expand source code
class RoleFilter(SettingFilter):
    """Filter used for converting role names to IDs and back"""

    multiple: bool = True
    """True to allow multiple values"""

    def __init__(self, setting: str, multiple: bool = True):
        super().__init__(setting)
        self.multiple = multiple

    def in_(self, ctx: Context, value: str | None) -> list[int] | None:
        """
        Filter setting input.

        Args:
            ctx: The current context
            value: The incoming value

        Returns:
            The raw setting value (a list of role IDs)
        """

        if not value:
            return

        roles = get_mixed_roles(value)
        ids = []

        for r in roles:
            id = None

            if r[0] == "":
                id = get_id_for_role(ctx.guild, r[1])
            else:
                # convert mentions, if any
                id = int(r[0])

            if id is None:
                raise ValueError(r)

            ids.append(id)

            if not self.multiple:
                break

        return ids

    def out(
        self,
        ctx: Context,
        value: list[int] | None,
    ) -> list[str | None] | str | None:
        """
        Filter setting output.

        Args:
            ctx: The current context
            value: The raw setting value: a list of role IDs

        Returns:
            The filtered setting value (a list of role names)
        """

        if value is None:
            return

        roles = [get_role_for_id(ctx.guild, v) for v in value]

        if self.multiple:
            return roles

        if len(roles) > 0:
            return roles[0]

        return None

Ancestors

Subclasses

Class variables

var multiple : bool

True to allow multiple values

Methods

def in_(self, ctx: discord.ext.commands.context.Context, value: str | None) ‑> list[int] | None

Filter setting input.

Args

ctx
The current context
value
The incoming value

Returns

The raw setting value (a list of role IDs)

Expand source code
def in_(self, ctx: Context, value: str | None) -> list[int] | None:
    """
    Filter setting input.

    Args:
        ctx: The current context
        value: The incoming value

    Returns:
        The raw setting value (a list of role IDs)
    """

    if not value:
        return

    roles = get_mixed_roles(value)
    ids = []

    for r in roles:
        id = None

        if r[0] == "":
            id = get_id_for_role(ctx.guild, r[1])
        else:
            # convert mentions, if any
            id = int(r[0])

        if id is None:
            raise ValueError(r)

        ids.append(id)

        if not self.multiple:
            break

    return ids
def out(self, ctx: discord.ext.commands.context.Context, value: list[int] | None) ‑> list[str | None] | str | None

Filter setting output.

Args

ctx
The current context
value
The raw setting value: a list of role IDs

Returns

The filtered setting value (a list of role names)

Expand source code
def out(
    self,
    ctx: Context,
    value: list[int] | None,
) -> list[str | None] | str | None:
    """
    Filter setting output.

    Args:
        ctx: The current context
        value: The raw setting value: a list of role IDs

    Returns:
        The filtered setting value (a list of role names)
    """

    if value is None:
        return

    roles = [get_role_for_id(ctx.guild, v) for v in value]

    if self.multiple:
        return roles

    if len(roles) > 0:
        return roles[0]

    return None

Inherited members

class SecondsFilter (setting: str)

Filter used for converting strings to number of seconds values

Expand source code
class SecondsFilter(SettingFilter):
    """Filter used for converting strings to number of seconds values"""

    def __init__(self, setting: str):
        super().__init__(setting)

    def in_(self, ctx: Context, value: str | None) -> int | None:
        """
        Filter setting input.

        Args:
            ctx: The current context
            value: The incoming value

        Returns:
            The raw setting value (an integer)
        """

        if not value:
            return

        try:
            return int(value)
        except ValueError:
            days, hours, minutes = get_timespan_chunks(value)
            return minutes * 60 + hours * 3600 + days * 86400

    def out(
        self,
        ctx: Context,
        value: int | None,
    ) -> str | None:
        """
        Filter setting output.

        Args:
            ctx: The current context
            value: The raw setting value (an integer)

        Returns:
            The filtered setting value (days, hours, minutes)
        """

        if not value:
            return

        return seconds_to_str(value)

Ancestors

Methods

def in_(self, ctx: discord.ext.commands.context.Context, value: str | None) ‑> int | None

Filter setting input.

Args

ctx
The current context
value
The incoming value

Returns

The raw setting value (an integer)

Expand source code
def in_(self, ctx: Context, value: str | None) -> int | None:
    """
    Filter setting input.

    Args:
        ctx: The current context
        value: The incoming value

    Returns:
        The raw setting value (an integer)
    """

    if not value:
        return

    try:
        return int(value)
    except ValueError:
        days, hours, minutes = get_timespan_chunks(value)
        return minutes * 60 + hours * 3600 + days * 86400
def out(self, ctx: discord.ext.commands.context.Context, value: int | None) ‑> str | None

Filter setting output.

Args

ctx
The current context
value
The raw setting value (an integer)

Returns

The filtered setting value (days, hours, minutes)

Expand source code
def out(
    self,
    ctx: Context,
    value: int | None,
) -> str | None:
    """
    Filter setting output.

    Args:
        ctx: The current context
        value: The raw setting value (an integer)

    Returns:
        The filtered setting value (days, hours, minutes)
    """

    if not value:
        return

    return seconds_to_str(value)

Inherited members

class SettingFilter (setting: str)

A class with methods for filtering a setting's input and output

Expand source code
class SettingFilter(object):
    """A class with methods for filtering a setting's input and output"""

    setting: str
    """The name of the setting to filter"""

    def __init__(self, setting: str):
        self.setting = setting

    def in_(self, ctx: Context, value: str | None) -> Any | None:
        """
        Must override; input filter method.

        Args:
            ctx: The current context
            value: The incoming value

        Returns:
            The raw setting value(s), for reference
        """

        raise NotImplementedError()

    def out(self, ctx: Context, value: Any | None) -> Any | None:
        """
        Must override; output filter method.

        Args:
            ctx: The current context
            value: The raw setting value

        Returns:
            The filtered setting value(s)
        """

        raise NotImplementedError()

Subclasses

Class variables

var setting : str

The name of the setting to filter

Methods

def in_(self, ctx: discord.ext.commands.context.Context, value: str | None) ‑> typing.Any | None

Must override; input filter method.

Args

ctx
The current context
value
The incoming value

Returns

The raw setting value(s), for reference

Expand source code
def in_(self, ctx: Context, value: str | None) -> Any | None:
    """
    Must override; input filter method.

    Args:
        ctx: The current context
        value: The incoming value

    Returns:
        The raw setting value(s), for reference
    """

    raise NotImplementedError()
def out(self, ctx: discord.ext.commands.context.Context, value: typing.Any | None) ‑> typing.Any | None

Must override; output filter method.

Args

ctx
The current context
value
The raw setting value

Returns

The filtered setting value(s)

Expand source code
def out(self, ctx: Context, value: Any | None) -> Any | None:
    """
    Must override; output filter method.

    Args:
        ctx: The current context
        value: The raw setting value

    Returns:
        The filtered setting value(s)
    """

    raise NotImplementedError()