# -*- coding: utf-8 -*-
# cython: language_level=3
# Copyright (c) 2020 Nekokatt
# Copyright (c) 2021-present davfsa
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Application and entities that are used to describe components on Discord."""
from __future__ import annotations
__all__: typing.Sequence[str] = (
"ComponentType",
"PartialComponent",
"ActionRowComponent",
"ButtonStyle",
"ButtonComponent",
"SelectMenuOption",
"SelectMenuComponent",
"TextSelectMenuComponent",
"ChannelSelectMenuComponent",
"TextInputStyle",
"TextInputComponent",
"InteractiveButtonTypes",
"InteractiveButtonTypesT",
"MessageComponentTypesT",
"ModalComponentTypesT",
"MessageActionRowComponent",
"ModalActionRowComponent",
)
import typing
import attr
from hikari import channels
from hikari import emojis
from hikari.internal import enums
@typing.final
[docs]class ComponentType(int, enums.Enum):
"""Types of components found within Discord."""
"""A non-interactive container component for other types of components.
.. note::
As this is a container component it can never be contained within another
component and therefore will always be top-level.
.. note::
As of writing this can only contain one component type.
"""
"""A button component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""A text select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""Deprecated alias for `TEXT_MENU_SELECT`."""
"""A text input component.
.. note::
This component may only be used inside a modal container.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""A user select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""A role select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""A mentionable (users and roles) select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
"""A channel select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""
@typing.final
@typing.final
[docs]class TextInputStyle(int, enums.Enum):
"""A text input style."""
"""Intended for short single-line text."""
"""Intended for much longer inputs."""
@attr.define(kw_only=True, weakref_slot=False)
[docs]class PartialComponent:
"""Base class for all component entities."""
[docs] type: typing.Union[ComponentType, int] = attr.field()
"""The type of component this is."""
AllowedComponentsT = typing.TypeVar("AllowedComponentsT", bound="PartialComponent")
@attr.define(weakref_slot=False)
[docs]class ActionRowComponent(typing.Generic[AllowedComponentsT], PartialComponent):
"""Represents a row of components."""
[docs] components: typing.Sequence[AllowedComponentsT] = attr.field()
"""Sequence of the components contained within this row."""
@typing.overload
def __getitem__(self, index: int, /) -> PartialComponent:
...
@typing.overload
def __getitem__(self, slice_: slice, /) -> typing.Sequence[AllowedComponentsT]:
...
def __getitem__(
self, index_or_slice: typing.Union[int, slice], /
) -> typing.Union[PartialComponent, typing.Sequence[AllowedComponentsT]]:
return self.components[index_or_slice]
def __iter__(self) -> typing.Iterator[AllowedComponentsT]:
return iter(self.components)
def __len__(self) -> int:
return len(self.components)
@attr.define(hash=True, kw_only=True, weakref_slot=False)
@attr.define(kw_only=True, weakref_slot=False)
@attr.define(hash=True, kw_only=True, weakref_slot=False)
@attr.define(kw_only=True, weakref_slot=False)
@attr.define(kw_only=True, weakref_slot=False)
@attr.define(kw_only=True, weakref_slot=False)
[docs]class TextInputComponent(PartialComponent):
"""Represents a text input component."""
[docs] custom_id: str = attr.field(repr=True)
"""Developer set custom ID used for identifying interactions with this modal."""
[docs] value: str = attr.field(repr=True)
"""Value provided for this text input."""
SelectMenuTypesT = typing.Union[
typing.Literal[ComponentType.TEXT_SELECT_MENU],
typing.Literal[3],
typing.Literal[ComponentType.USER_SELECT_MENU],
typing.Literal[5],
typing.Literal[ComponentType.ROLE_SELECT_MENU],
typing.Literal[6],
typing.Literal[ComponentType.MENTIONABLE_SELECT_MENU],
typing.Literal[7],
typing.Literal[ComponentType.CHANNEL_SELECT_MENU],
typing.Literal[8],
]
"""Type hints of the `ComponentType` values which are valid for select menus.
The following values are valid for this:
* `ComponentType.TEXT_SELECT_MENU`/`3`
* `ComponentType.USER_SELECT_MENU`/`5`
* `ComponentType.ROLE_SELECT_MENU`/`6`
* `ComponentType.MENTIONABLE_SELECT_MENU`/`7`
* `ComponentType.CHANNEL_SELECT_MENU`/`8`
"""
SelectMenuTypes: typing.AbstractSet[SelectMenuTypesT] = frozenset(
(
ComponentType.TEXT_SELECT_MENU,
ComponentType.USER_SELECT_MENU,
ComponentType.ROLE_SELECT_MENU,
ComponentType.MENTIONABLE_SELECT_MENU,
ComponentType.CHANNEL_SELECT_MENU,
)
)
"""Set of the `ComponentType` values which are valid for select menus.
The following values are included in this:
* `ComponentType.TEXT_SELECT_MENU`
* `ComponentType.USER_SELECT_MENU`
* `ComponentType.ROLE_SELECT_MENU`
* `ComponentType.MENTIONABLE_SELECT_MENU`
* `ComponentType.CHANNEL_SELECT_MENU`
"""
"""Type hints of the `ButtonStyle` values which are valid for interactive buttons.
The following values are valid for this:
* `ButtonStyle.PRIMARY`/`1`
* `ButtonStyle.SECONDARY`/`2`
* `ButtonStyle.SUCCESS`/`3`
* `ButtonStyle.DANGER`/`4`
"""
"""Set of the `ButtonType` which are valid for interactive buttons.
The following values are included in this:
* `ButtonStyle.PRIMARY`
* `ButtonStyle.SECONDARY`
* `ButtonStyle.SUCCESS`
* `ButtonStyle.DANGER`
"""
[docs]MessageComponentTypesT = typing.Union[ButtonComponent, SelectMenuComponent]
"""Type hint of the `PartialComponent` that be contained in a `MessageActionRowComponent`.
The following values are valid for this:
* `ButtonComponent`
* `SelectMenuComponent`
"""
[docs]ModalComponentTypesT = TextInputComponent
"""Type hint of the `PartialComponent` that be contained in a `ModalActionRowComponent`.
The following values are valid for this:
* `TextInputComponent`
"""
[docs]MessageActionRowComponent = ActionRowComponent[MessageComponentTypesT]
"""A message action row component."""
[docs]ModalActionRowComponent = ActionRowComponent[ModalComponentTypesT]
"""A modal action row component."""