Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new selects #1702

Merged
merged 44 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
983ad54
feat: initial implementation of selects v2
plun1331 Oct 13, 2022
d33a371
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
48cf00e
fix: bugs
plun1331 Oct 14, 2022
b6e5542
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2022
f2d0f5a
Merge branch 'master' into selects-v2
plun1331 Oct 14, 2022
86fc02b
feat: add new classes and methods to docs
plun1331 Oct 14, 2022
d5bb0e4
Merge remote-tracking branch 'origin/selects-v2' into selects-v2
plun1331 Oct 14, 2022
8a34f1a
fix: add new classes to __all__
plun1331 Oct 14, 2022
3a1229a
fix: title underlines
plun1331 Oct 14, 2022
e65433f
fix: ComponentType refs
plun1331 Oct 14, 2022
64ca0ff
refactor: make value parsing somewhat consistent
plun1331 Oct 14, 2022
23b4e24
Update discord/ui/select.py
plun1331 Oct 14, 2022
f8ef420
Update select.py
plun1331 Oct 14, 2022
7dbf6e0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2022
2a6bb8d
Merge branch 'master' into selects-v2
BobDotCom Oct 16, 2022
9320727
Merge branch 'master' into selects-v2
BobDotCom Oct 17, 2022
854c2f8
Merge branch 'master' into selects-v2
Lulalaby Oct 18, 2022
84a4a3e
Merge remote-tracking branch 'origin/master' into selects-v2
plun1331 Oct 27, 2022
77b4bf3
feat: update docs
plun1331 Oct 27, 2022
aa20fc9
Merge branch 'master' into selects-v2
BobDotCom Oct 27, 2022
903ff5b
Merge branch 'Pycord-Development:master' into selects-v2
plun1331 Oct 28, 2022
053a559
Merge branch 'Pycord-Development:master' into selects-v2
plun1331 Oct 30, 2022
a16426f
feat: redo impl for one select class
plun1331 Oct 30, 2022
7e5f92c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2022
42e9d2b
Delete api.rst
Lulalaby Oct 30, 2022
c2c57ed
Update ui_kit.rst
plun1331 Oct 30, 2022
2131960
Update models.rst
plun1331 Oct 30, 2022
773321e
Update discord/ui/select.py
Lulalaby Oct 30, 2022
b856289
Update discord/ui/select.py
Lulalaby Oct 30, 2022
1e6eaa7
Update components.py
plun1331 Nov 1, 2022
9b156aa
Update components.py
plun1331 Nov 1, 2022
4460fc9
Update components.py
plun1331 Nov 1, 2022
4a0db1d
Update select.py
plun1331 Nov 1, 2022
ab1d8de
Update enums.py
plun1331 Nov 1, 2022
2525d06
Merge branch 'Pycord-Development:master' into selects-v2
plun1331 Nov 1, 2022
5d37430
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2022
09b7ddc
alias
plun1331 Nov 1, 2022
030dbbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2022
abdf4ca
add string_select to __all__
plun1331 Nov 1, 2022
3652803
Merge branch 'selects-v2' of https://github.com/plun1331/pycord into …
plun1331 Nov 1, 2022
d6012fd
update docs
plun1331 Nov 1, 2022
e9134c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2022
408148c
Merge branch 'master' into selects-v2
Lulalaby Nov 2, 2022
1421c5a
Merge branch 'master' into selects-v2
BobDotCom Nov 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 28 additions & 8 deletions discord/components.py
Expand Up @@ -27,7 +27,7 @@

from typing import TYPE_CHECKING, Any, ClassVar, TypeVar

from .enums import ButtonStyle, ComponentType, InputTextStyle, try_enum
from .enums import ButtonStyle, ChannelType, ComponentType, InputTextStyle, try_enum
from .partial_emoji import PartialEmoji, _EmojiTag
from .utils import MISSING, get_slots

Expand All @@ -40,7 +40,6 @@
from .types.components import SelectMenu as SelectMenuPayload
from .types.components import SelectOption as SelectOptionPayload


__all__ = (
"Component",
"ActionRow",
Expand Down Expand Up @@ -157,7 +156,6 @@ class InputText(Component):
"""

__slots__: tuple[str, ...] = (
"type",
plun1331 marked this conversation as resolved.
Show resolved Hide resolved
"style",
"custom_id",
"label",
Expand Down Expand Up @@ -293,8 +291,15 @@ class SelectMenu(Component):

.. versionadded:: 2.0

.. versionchanged:: 2.3

Added support for :attr:`ComponentType.user_select`, :attr:`ComponentType.role_select`,
:attr:`ComponentType.mentionable_select`, and :attr:`ComponentType.channel_select`.

Attributes
----------
type: :class:`ComponentType`
The select menu's type.
custom_id: Optional[:class:`str`]
The ID of the select menu that gets received during an interaction.
placeholder: Optional[:class:`str`]
Expand All @@ -307,6 +312,12 @@ class SelectMenu(Component):
Defaults to 1 and must be between 1 and 25.
options: List[:class:`SelectOption`]
A list of options that can be selected in this menu.
Will be an empty list for all component types
except for :attr:`ComponentType.string_select`.
channel_types: List[:class:`ChannelType`]
A list of channel types that can be selected.
Will be an empty list for all component types
except for :attr:`ComponentType.channel_select`.
disabled: :class:`bool`
Whether the select is disabled or not.
"""
Expand All @@ -317,40 +328,47 @@ class SelectMenu(Component):
"min_values",
"max_values",
"options",
"channel_types",
"disabled",
)

__repr_info__: ClassVar[tuple[str, ...]] = __slots__

def __init__(self, data: SelectMenuPayload):
self.type = ComponentType.select
self.type = try_enum(ComponentType, data["type"])
self.custom_id: str = data["custom_id"]
self.placeholder: str | None = data.get("placeholder")
self.min_values: int = data.get("min_values", 1)
self.max_values: int = data.get("max_values", 1)
self.disabled: bool = data.get("disabled", False)
self.options: list[SelectOption] = [
SelectOption.from_dict(option) for option in data.get("options", [])
]
self.disabled: bool = data.get("disabled", False)
self.channel_types: list[ChannelType] = [
try_enum(ChannelType, ct) for ct in data.get("channel_types", [])
]

def to_dict(self) -> SelectMenuPayload:
payload: SelectMenuPayload = {
"type": self.type.value,
"custom_id": self.custom_id,
"min_values": self.min_values,
"max_values": self.max_values,
"options": [op.to_dict() for op in self.options],
"disabled": self.disabled,
}

if self.type is ComponentType.string_select:
payload["options"] = [op.to_dict() for op in self.options]
if self.type is ComponentType.channel_select and self.channel_types:
payload["channel_types"] = [ct.value for ct in self.channel_types]
if self.placeholder:
payload["placeholder"] = self.placeholder

return payload


class SelectOption:
"""Represents a select menu's option.
"""Represents a :class:`discord.SelectMenu`'s option.

These can be created by users.

Expand Down Expand Up @@ -474,7 +492,9 @@ def _component_factory(data: ComponentPayload) -> Component:
return ActionRow(data)
elif component_type == 2:
return Button(data) # type: ignore
elif component_type == 3:
elif component_type == 4:
return InputText(data) # type: ignore
elif component_type in (3, 5, 6, 7, 8):
return SelectMenu(data) # type: ignore
else:
as_enum = try_enum(ComponentType, component_type)
Expand Down
7 changes: 6 additions & 1 deletion discord/enums.py
Expand Up @@ -654,8 +654,13 @@ class ComponentType(Enum):

action_row = 1
button = 2
select = 3
select = 3 # alias for string_select
plun1331 marked this conversation as resolved.
Show resolved Hide resolved
string_select = 3
input_text = 4
user_select = 5
role_select = 6
mentionable_select = 7
channel_select = 8

def __int__(self):
return self.value
Expand Down
5 changes: 3 additions & 2 deletions discord/types/components.py
Expand Up @@ -28,6 +28,7 @@
from typing import Literal, Union

from .._typed_dict import NotRequired, TypedDict
from .channel import ChannelType
from .emoji import PartialEmoji

ComponentType = Literal[1, 2, 3, 4]
Expand Down Expand Up @@ -75,9 +76,9 @@ class SelectMenu(TypedDict):
min_values: NotRequired[int]
max_values: NotRequired[int]
disabled: NotRequired[bool]
type: Literal[3]
channel_types: NotRequired[list[ChannelType]]
type: Literal[3, 5, 6, 7, 8]
custom_id: str
options: list[SelectOption]
BobDotCom marked this conversation as resolved.
Show resolved Hide resolved


Component = Union[ActionRow, ButtonComponent, SelectMenu, InputText]