Skip to content

Commit 7d5f073

Browse files
committed
Add support for default values in non string selects
1 parent a140c3a commit 7d5f073

File tree

4 files changed

+231
-1
lines changed

4 files changed

+231
-1
lines changed

discord/components.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
from .types.components import InputText as InputTextComponentPayload
4040
from .types.components import SelectMenu as SelectMenuPayload
4141
from .types.components import SelectOption as SelectOptionPayload
42+
from .types.components import SelectDefaultValue as SelectDefaultValuePayload
43+
from .types.components import SelectMenuDefaultValueType
4244

4345
__all__ = (
4446
"Component",
4547
"ActionRow",
4648
"Button",
4749
"SelectMenu",
4850
"SelectOption",
51+
"SelectDefaultValue",
4952
"InputText",
5053
)
5154

@@ -328,6 +331,9 @@ class SelectMenu(Component):
328331
except for :attr:`ComponentType.channel_select`.
329332
disabled: :class:`bool`
330333
Whether the select is disabled or not.
334+
default_values: List[:class:`SelectDefaultValue`]
335+
A list of options that are selected by default for select menus of type user, role, channel and mentionable.
336+
Will be an empty list for component type :attr:`ComponentType.string_select`.
331337
"""
332338

333339
__slots__: tuple[str, ...] = (
@@ -338,6 +344,7 @@ class SelectMenu(Component):
338344
"options",
339345
"channel_types",
340346
"disabled",
347+
"default_values",
341348
)
342349

343350
__repr_info__: ClassVar[tuple[str, ...]] = __slots__
@@ -355,6 +362,7 @@ def __init__(self, data: SelectMenuPayload):
355362
self.channel_types: list[ChannelType] = [
356363
try_enum(ChannelType, ct) for ct in data.get("channel_types", [])
357364
]
365+
self.default_values: list[SelectDefaultValue] = []
358366

359367
def to_dict(self) -> SelectMenuPayload:
360368
payload: SelectMenuPayload = {
@@ -371,6 +379,8 @@ def to_dict(self) -> SelectMenuPayload:
371379
payload["channel_types"] = [ct.value for ct in self.channel_types]
372380
if self.placeholder:
373381
payload["placeholder"] = self.placeholder
382+
if self.type is ComponentType.role_select and self.default_values:
383+
payload["default_values"] = []
374384

375385
return payload
376386

@@ -494,6 +504,63 @@ def to_dict(self) -> SelectOptionPayload:
494504
return payload
495505

496506

507+
class SelectDefaultValue:
508+
"""Represents a :class:`discord.SelectMenu`'s default option for user, role, channel and mentionable select menu types.
509+
510+
These can be created by users.
511+
512+
.. versionadded:: 2.7
513+
514+
Attributes
515+
----------
516+
id: :class:`int`
517+
The snowflake id of the default option.
518+
type: :class:`SelectMenuDefaultValueType`
519+
The type of the default value. This is not displayed to users.
520+
"""
521+
522+
__slots__: tuple[str, ...] = (
523+
"id",
524+
"type",
525+
)
526+
527+
def __init__(
528+
self,
529+
*,
530+
id: int,
531+
type: SelectMenuDefaultValueType
532+
) -> None:
533+
self.id = id
534+
self.type = type
535+
536+
537+
538+
def __repr__(self) -> str:
539+
return (
540+
"<SelectDefaultValue"
541+
f" id={self.id!r} type={self.type!r}>"
542+
)
543+
544+
def __str__(self) -> str:
545+
return f"{self.id} {self.type}"
546+
547+
@classmethod
548+
def from_dict(cls, data: SelectDefaultValuePayload) -> SelectDefaultValue:
549+
550+
return cls(
551+
id=data["id"],
552+
type=data["type"],
553+
)
554+
555+
def to_dict(self) -> SelectDefaultValuePayload:
556+
payload: SelectDefaultValuePayload = {
557+
"id": self.id,
558+
"type": self.type,
559+
}
560+
561+
return payload
562+
563+
497564
def _component_factory(data: ComponentPayload) -> Component:
498565
component_type = data["type"]
499566
if component_type == 1:

discord/enums.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"InteractionContextType",
7979
"PollLayoutType",
8080
"MessageReferenceType",
81+
"SelectMenuDefaultValueType",
8182
)
8283

8384

@@ -1078,6 +1079,17 @@ class SubscriptionStatus(Enum):
10781079
inactive = 2
10791080

10801081

1082+
class SelectMenuDefaultValueType(Enum):
1083+
"""The type of a select menu's default value."""
1084+
1085+
Channel = 'channel'
1086+
Role = 'role'
1087+
User = 'user'
1088+
1089+
def __str__(self):
1090+
return self.name
1091+
1092+
10811093
T = TypeVar("T")
10821094

10831095

discord/types/components.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
ComponentType = Literal[1, 2, 3, 4]
3737
ButtonStyle = Literal[1, 2, 3, 4, 5, 6]
3838
InputTextStyle = Literal[1, 2]
39+
SelectMenuDefaultValueType = Literal["channel", "role", "user"]
3940

4041

4142
class ActionRow(TypedDict):
@@ -74,6 +75,11 @@ class SelectOption(TypedDict):
7475
default: bool
7576

7677

78+
class SelectDefaultValue(TypedDict):
79+
id: Snowflake
80+
type: SelectMenuDefaultValueType
81+
82+
7783
class SelectMenu(TypedDict):
7884
placeholder: NotRequired[str]
7985
min_values: NotRequired[int]
@@ -83,6 +89,7 @@ class SelectMenu(TypedDict):
8389
options: NotRequired[list[SelectOption]]
8490
type: Literal[3, 5, 6, 7, 8]
8591
custom_id: str
92+
default_values: NotRequired[list[SelectDefaultValue]]
8693

8794

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

0 commit comments

Comments
 (0)