Skip to content

Commit

Permalink
Select menu V2 (#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa authored Feb 6, 2023
1 parent 102bd15 commit 17a1407
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 151 deletions.
Empty file added changes/1455.breaking.md
Empty file.
4 changes: 4 additions & 0 deletions changes/1455.deprecation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deprecate selects v1 functionality:
- `ComponentType.SELECT_MENU` -> `ComponentType.TEXT_SELECT_MENU`
- Not passing `MessageActionRowBuilder.add_select_menu`'s `type` argument explicitly.
- `InteractionChannel` and `ResolvedOptionData` moved from `hikari.interactions.command_interactions` to `hikari.interactions.base_interactions`.
1 change: 1 addition & 0 deletions changes/1455.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add selects v2 components.
144 changes: 116 additions & 28 deletions hikari/api/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ def add_to_menu(self) -> _SelectMenuBuilderT:


class SelectMenuBuilder(ComponentBuilder, abc.ABC, typing.Generic[_ContainerT]):
"""Builder class for select menu options."""
"""Builder class for a select menu."""

__slots__: typing.Sequence[str] = ()

Expand All @@ -1474,11 +1474,6 @@ def custom_id(self) -> str:
def is_disabled(self) -> bool:
"""Whether the select menu should be marked as disabled."""

@property
@abc.abstractmethod
def options(self: _SelectMenuBuilderT) -> typing.Sequence[SelectOptionBuilder[_SelectMenuBuilderT]]:
"""Sequence of the options set for this select menu."""

@property
@abc.abstractmethod
def placeholder(self) -> undefined.UndefinedOr[str]:
Expand All @@ -1504,27 +1499,6 @@ def max_values(self) -> int:
less than or equal to 25.
"""

@abc.abstractmethod
def add_option(self: _SelectMenuBuilderT, label: str, value: str, /) -> SelectOptionBuilder[_SelectMenuBuilderT]:
"""Add an option to this menu.
.. note::
Setup should be finalised by calling `add_to_menu` in the builder
returned.
Parameters
----------
label : str
The user-facing name of this option, max 100 characters.
value : str
The developer defined value of this option, max 100 characters.
Returns
-------
SelectOptionBuilder[SelectMenuBuilder]
Option builder object.
"""

@abc.abstractmethod
def set_is_disabled(self: _T, state: bool, /) -> _T:
"""Set whether this option is disabled.
Expand Down Expand Up @@ -1607,6 +1581,64 @@ def add_to_container(self) -> _ContainerT:
"""


class TextSelectMenuBuilder(SelectMenuBuilder[_ContainerT], abc.ABC, typing.Generic[_ContainerT]):
"""Builder class for a text select menu."""

__slots__: typing.Sequence[str] = ()

@property
@abc.abstractmethod
def options(self: _SelectMenuBuilderT) -> typing.Sequence[SelectOptionBuilder[_SelectMenuBuilderT]]:
"""Sequence of the options set for this select menu."""

@abc.abstractmethod
def add_option(self: _SelectMenuBuilderT, label: str, value: str, /) -> SelectOptionBuilder[_SelectMenuBuilderT]:
"""Add an option to this menu.
.. note::
Setup should be finalised by calling `add_to_menu` in the builder
returned.
Parameters
----------
label : str
The user-facing name of this option, max 100 characters.
value : str
The developer defined value of this option, max 100 characters.
Returns
-------
SelectOptionBuilder[SelectMenuBuilder]
Option builder object.
"""


class ChannelSelectMenuBuilder(SelectMenuBuilder[_ContainerT], abc.ABC, typing.Generic[_ContainerT]):
"""Builder class for a channel select menu."""

__slots__: typing.Sequence[str] = ()

@property
@abc.abstractmethod
def channel_types(self) -> typing.Sequence[channels.ChannelType]:
"""The channel types that can be selected in this menu."""

@abc.abstractmethod
def set_channel_types(self: _T, value: typing.Sequence[channels.ChannelType], /) -> _T:
"""Set the valid channel types for this menu.
Parameters
----------
value : typing.Sequence[hikari.channels.ChannelType]
The valid channel types for this menu.
Returns
-------
SelectMenuBuilder
The builder object to enable chained calls.
"""


class TextInputBuilder(ComponentBuilder, abc.ABC, typing.Generic[_ContainerT]):
"""Builder class for text inputs components."""

Expand Down Expand Up @@ -1868,12 +1900,63 @@ def add_button(
component.
"""

@typing.overload # Deprecated overload
@abc.abstractmethod
def add_select_menu(
self: _T,
custom_id: str,
/,
) -> TextSelectMenuBuilder[_T]:
...

@typing.overload
@abc.abstractmethod
def add_select_menu(
self: _T,
type_: typing.Literal[components_.ComponentType.TEXT_SELECT_MENU, 3],
custom_id: str,
/,
) -> TextSelectMenuBuilder[_T]:
...

@typing.overload
@abc.abstractmethod
def add_select_menu(
self: _T,
type_: typing.Literal[components_.ComponentType.CHANNEL_SELECT_MENU, 8],
custom_id: str,
/,
) -> ChannelSelectMenuBuilder[_T]:
...

@typing.overload
@abc.abstractmethod
def add_select_menu(self: _T, custom_id: str, /) -> SelectMenuBuilder[_T]:
def add_select_menu(
self: _T,
type_: typing.Union[components_.ComponentType, int],
custom_id: str,
/,
) -> SelectMenuBuilder[_T]:
...

@abc.abstractmethod
def add_select_menu(
self: _T,
type_: typing.Union[components_.ComponentType, int, str],
# These have default during the deprecation period for backwards compatibility, as custom_id
# used to come first
custom_id: str = "",
/,
) -> SelectMenuBuilder[_T]:
"""Add a select menu component to this action row builder.
.. deprecated:: 2.0.0.dev116
`type_` now comes as a positional-only argument before `custom_id`.
Parameters
----------
type_ : typing.Union[hikari.components.ComponentType, int]
The type for the select menu.
custom_id : str
A developer-defined custom identifier used to identify which menu
triggered component interactions.
Expand All @@ -1884,6 +1967,11 @@ def add_select_menu(self: _T, custom_id: str, /) -> SelectMenuBuilder[_T]:
Select menu builder object.
`SelectMenuBuilder.add_to_container` should be called to finalise the
component.
Raises
------
ValueError
If an invalid select menu type is passed.
"""


Expand Down
102 changes: 97 additions & 5 deletions hikari/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import attr

from hikari import channels
from hikari import emojis
from hikari.internal import enums

Expand Down Expand Up @@ -73,14 +74,17 @@ class ComponentType(int, enums.Enum):
as `ComponentType.ACTION_ROW`.
"""

SELECT_MENU = 3
"""A select menu component.
TEXT_SELECT_MENU = 3
"""A text select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""

SELECT_MENU = enums.deprecated(TEXT_SELECT_MENU, removal_version="2.0.0.dev118")
"""Deprecated alias for `TEXT_MENU_SELECT`."""

TEXT_INPUT = 4
"""A text input component.
Expand All @@ -92,6 +96,38 @@ class ComponentType(int, enums.Enum):
as `ComponentType.ACTION_ROW`.
"""

USER_SELECT_MENU = 5
"""A user select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""

ROLE_SELECT_MENU = 6
"""A role select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""

MENTIONABLE_SELECT_MENU = 7
"""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`.
"""

CHANNEL_SELECT_MENU = 8
"""A channel select component.
.. note::
This cannot be top-level and must be within a container component such
as `ComponentType.ACTION_ROW`.
"""


@typing.final
class ButtonStyle(int, enums.Enum):
Expand Down Expand Up @@ -230,9 +266,6 @@ class SelectMenuComponent(PartialComponent):
custom_id: str = attr.field(hash=True)
"""Developer defined identifier for this menu (will be <= 100 characters)."""

options: typing.Sequence[SelectMenuOption] = attr.field(eq=False)
"""Sequence of up to 25 of the options set for this menu."""

placeholder: typing.Optional[str] = attr.field(eq=False)
"""Custom placeholder text shown if nothing is selected, max 100 characters."""

Expand All @@ -254,6 +287,22 @@ class SelectMenuComponent(PartialComponent):
"""Whether the select menu is disabled."""


@attr.define(kw_only=True, weakref_slot=False)
class TextSelectMenuComponent(SelectMenuComponent):
"""Represents a text select menu component."""

options: typing.Sequence[SelectMenuOption] = attr.field(eq=False)
"""Sequence of up to 25 of the options set for this menu."""


@attr.define(kw_only=True, weakref_slot=False)
class ChannelSelectMenuComponent(SelectMenuComponent):
"""Represents a channel select menu component."""

channel_types: typing.Sequence[typing.Union[int, channels.ChannelType]] = attr.field(eq=False)
"""The valid channel types for this menu."""


@attr.define(kw_only=True, weakref_slot=False)
class TextInputComponent(PartialComponent):
"""Represents a text input component."""
Expand All @@ -265,6 +314,49 @@ class TextInputComponent(PartialComponent):
"""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`
"""

InteractiveButtonTypesT = typing.Union[
typing.Literal[ButtonStyle.PRIMARY],
typing.Literal[1],
Expand Down
Loading

0 comments on commit 17a1407

Please sign in to comment.