Skip to content

Commit 3c968e6

Browse files
committed
feat: update classes to support new select features
1 parent b2e4bdd commit 3c968e6

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

interactions/models/discord/components.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ class BaseSelectMenu(InteractiveComponent):
366366
max_values Optional[int]: The maximum number of items that can be chosen. (default 1, max 25)
367367
disabled bool: Disable the select and make it not intractable, default false.
368368
type Union[ComponentType, int]: The action role type number defined by discord. This cannot be modified.
369+
required bool: Whether this select menu is required to be filled out or not in modals.
369370
370371
"""
371372

@@ -377,12 +378,14 @@ def __init__(
377378
max_values: int = 1,
378379
custom_id: str | None = None,
379380
disabled: bool = False,
381+
required: bool = True,
380382
) -> None:
381383
self.custom_id: str = custom_id or str(uuid.uuid4())
382384
self.placeholder: str | None = placeholder
383385
self.min_values: int = min_values
384386
self.max_values: int = max_values
385387
self.disabled: bool = disabled
388+
self.required: bool = required
386389

387390
self.type: ComponentType = MISSING
388391

@@ -394,10 +397,11 @@ def from_dict(cls, data: discord_typings.SelectMenuComponentData) -> "BaseSelect
394397
max_values=data["max_values"],
395398
custom_id=data["custom_id"],
396399
disabled=data.get("disabled", False),
400+
required=data.get("required", True),
397401
)
398402

399403
def __repr__(self) -> str:
400-
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled}>"
404+
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled} required={self.required}>"
401405

402406
def to_dict(self) -> discord_typings.SelectMenuComponentData:
403407
return {
@@ -407,6 +411,7 @@ def to_dict(self) -> discord_typings.SelectMenuComponentData:
407411
"min_values": self.min_values,
408412
"max_values": self.max_values,
409413
"disabled": self.disabled,
414+
"required": self.required,
410415
}
411416

412417

@@ -585,7 +590,8 @@ class StringSelectMenu(BaseSelectMenu):
585590
min_values Optional[int]: The minimum number of items that must be chosen. (default 1, min 0, max 25)
586591
max_values Optional[int]: The maximum number of items that can be chosen. (default 1, max 25)
587592
disabled bool: Disable the select and make it not intractable, default false.
588-
type Union[ComponentType, int]: The type of component, as defined by discord. This cannot be modified.
593+
type Union[ComponentType, int]: The action role type number defined by discord. This cannot be modified.
594+
required bool: Whether this select menu is required to be filled out or not in modals.
589595
590596
"""
591597

@@ -600,13 +606,15 @@ def __init__(
600606
max_values: int = 1,
601607
custom_id: str | None = None,
602608
disabled: bool = False,
609+
required: bool = True,
603610
) -> None:
604611
super().__init__(
605612
placeholder=placeholder,
606613
min_values=min_values,
607614
max_values=max_values,
608615
custom_id=custom_id,
609616
disabled=disabled,
617+
required=required,
610618
)
611619
if isinstance(options, (list, tuple)) and len(options) == 1 and isinstance(options[0], (list, tuple)):
612620
# user passed in a list of options, expand it out
@@ -624,10 +632,11 @@ def from_dict(cls, data: discord_typings.SelectMenuComponentData) -> "StringSele
624632
max_values=data["max_values"],
625633
custom_id=data["custom_id"],
626634
disabled=data.get("disabled", False),
635+
required=data.get("required", True),
627636
)
628637

629638
def __repr__(self) -> str:
630-
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled} options={self.options}>"
639+
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled} required={self.required} options={self.options}>"
631640

632641
def to_dict(self) -> discord_typings.SelectMenuComponentData:
633642
return {
@@ -647,7 +656,8 @@ class UserSelectMenu(DefaultableSelectMenu):
647656
custom_id str: A developer-defined identifier for the select, max 100 characters.
648657
default_values list[BaseUser, Member, SelectDefaultValues]: A list of default values to pre-select in the select.
649658
disabled bool: Disable the select and make it not intractable, default false.
650-
type Union[ComponentType, int]: The type of component, as defined by discord. This cannot be modified.
659+
type Union[ComponentType, int]: The action role type number defined by discord. This cannot be modified.
660+
required bool: Whether this select menu is required to be filled out or not in modals.
651661
652662
"""
653663

@@ -669,13 +679,15 @@ def __init__(
669679
| None
670680
) = None,
671681
disabled: bool = False,
682+
required: bool = True,
672683
) -> None:
673684
super().__init__(
674685
placeholder=placeholder,
675686
min_values=min_values,
676687
max_values=max_values,
677688
custom_id=custom_id,
678689
disabled=disabled,
690+
required=required,
679691
defaults=default_values,
680692
)
681693

@@ -693,7 +705,8 @@ class RoleSelectMenu(DefaultableSelectMenu):
693705
custom_id str: A developer-defined identifier for the select, max 100 characters.
694706
default_values list[Role, SelectDefaultValues]: A list of default values to pre-select in the select.
695707
disabled bool: Disable the select and make it not intractable, default false.
696-
type Union[ComponentType, int]: The type of component, as defined by discord. This cannot be modified.
708+
type Union[ComponentType, int]: The action role type number defined by discord. This cannot be modified.
709+
required bool: Whether this select menu is required to be filled out or not in modals.
697710
698711
"""
699712

@@ -705,6 +718,7 @@ def __init__(
705718
max_values: int = 1,
706719
custom_id: str | None = None,
707720
disabled: bool = False,
721+
required: bool = True,
708722
default_values: (
709723
list[
710724
Union[
@@ -721,6 +735,7 @@ def __init__(
721735
max_values=max_values,
722736
custom_id=custom_id,
723737
disabled=disabled,
738+
required=required,
724739
defaults=default_values,
725740
)
726741

@@ -750,6 +765,7 @@ def __init__(
750765
max_values: int = 1,
751766
custom_id: str | None = None,
752767
disabled: bool = False,
768+
required: bool = True,
753769
default_values: (
754770
list[
755771
Union[
@@ -769,6 +785,7 @@ def __init__(
769785
max_values=max_values,
770786
custom_id=custom_id,
771787
disabled=disabled,
788+
required=required,
772789
defaults=default_values,
773790
)
774791

@@ -799,6 +816,7 @@ def __init__(
799816
max_values: int = 1,
800817
custom_id: str | None = None,
801818
disabled: bool = False,
819+
required: bool = True,
802820
default_values: (
803821
list[
804822
Union[
@@ -815,6 +833,7 @@ def __init__(
815833
max_values=max_values,
816834
custom_id=custom_id,
817835
disabled=disabled,
836+
required=required,
818837
defaults=default_values,
819838
)
820839

@@ -831,11 +850,12 @@ def from_dict(cls, data: discord_typings.SelectMenuComponentData) -> "ChannelSel
831850
max_values=data["max_values"],
832851
custom_id=data["custom_id"],
833852
disabled=data.get("disabled", False),
853+
required=data.get("required", True),
834854
channel_types=data.get("channel_types", []),
835855
)
836856

837857
def __repr__(self) -> str:
838-
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled} channel_types={self.channel_types}>"
858+
return f"<{self.__class__.__name__} type={self.type} custom_id={self.custom_id} placeholder={self.placeholder} min_values={self.min_values} max_values={self.max_values} disabled={self.disabled} required={self.required} channel_types={self.channel_types}>"
839859

840860
def to_dict(self) -> discord_typings.SelectMenuComponentData:
841861
return {

interactions/models/discord/modal.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@
88
from interactions.client.const import MISSING
99
from interactions.client.mixins.serialization import DictSerializationMixin
1010
from interactions.client.utils import dict_filter, dict_filter_none
11-
from interactions.models.discord.components import ComponentType, BaseComponent, StringSelectMenu
11+
from interactions.models.discord.components import (
12+
ChannelSelectMenu,
13+
ComponentType,
14+
BaseComponent,
15+
BaseSelectMenu,
16+
MentionableSelectMenu,
17+
RoleSelectMenu,
18+
StringSelectMenu,
19+
UserSelectMenu,
20+
)
1221
from interactions.models.internal.application_commands import CallbackType
1322

1423
__all__ = ("InputText", "Modal", "ParagraphText", "ShortText", "TextStyles", "LabelComponent")
@@ -129,12 +138,23 @@ def __init__(
129138

130139

131140
class LabelComponent(BaseComponent):
141+
"""
142+
A top-level layout component that wraps modal components with text as a label and optional description.
143+
144+
Attributes:
145+
label: The text label for the component.
146+
description: An optional description for the component.
147+
component: The component to be wrapped, either an InputText or a select menu.
148+
type: The type of the component, always ComponentType.LABEL.
149+
150+
"""
151+
132152
def __init__(
133153
self,
134154
*,
135155
label: str,
136156
description: Optional[str] = None,
137-
component: StringSelectMenu | InputText,
157+
component: BaseSelectMenu | InputText,
138158
):
139159
self.label = label
140160
self.component = component
@@ -160,7 +180,11 @@ def from_dict(cls, data: dict) -> Self:
160180
data["component"],
161181
alternate_mapping={
162182
ComponentType.INPUT_TEXT: InputText,
183+
ComponentType.CHANNEL_SELECT: ChannelSelectMenu,
163184
ComponentType.STRING_SELECT: StringSelectMenu,
185+
ComponentType.USER_SELECT: UserSelectMenu,
186+
ComponentType.ROLE_SELECT: RoleSelectMenu,
187+
ComponentType.MENTIONABLE_SELECT: MentionableSelectMenu,
164188
},
165189
),
166190
)

0 commit comments

Comments
 (0)