Skip to content

Commit

Permalink
Add support for default_values field on selects
Browse files Browse the repository at this point in the history
  • Loading branch information
Soheab committed Sep 29, 2023
1 parent 9f8f9bf commit c5ecc42
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 7 deletions.
84 changes: 83 additions & 1 deletion discord/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from __future__ import annotations

from typing import ClassVar, List, Literal, Optional, TYPE_CHECKING, Tuple, Union, overload
from .enums import try_enum, ComponentType, ButtonStyle, TextStyle, ChannelType
from .enums import try_enum, ComponentType, ButtonStyle, TextStyle, ChannelType, SelectDefaultValueType
from .utils import get_slots, MISSING
from .partial_emoji import PartialEmoji, _EmojiTag

Expand All @@ -40,8 +40,10 @@
ActionRow as ActionRowPayload,
TextInput as TextInputPayload,
ActionRowChildComponent as ActionRowChildComponentPayload,
SelectDefaultValues as SelectDefaultValuesPayload,
)
from .emoji import Emoji
from .abc import Snowflake

ActionRowChildComponentType = Union['Button', 'SelectMenu', 'TextInput']

Expand All @@ -53,6 +55,7 @@
'SelectMenu',
'SelectOption',
'TextInput',
'SelectDefaultValue',
)


Expand Down Expand Up @@ -263,6 +266,7 @@ class SelectMenu(Component):
'options',
'disabled',
'channel_types',
'default_values',
)

__repr_info__: ClassVar[Tuple[str, ...]] = __slots__
Expand All @@ -276,6 +280,9 @@ def __init__(self, data: SelectMenuPayload, /) -> None:
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, t) for t in data.get('channel_types', [])]
self.default_values: List[SelectDefaultValue] = [
SelectDefaultValue.from_dict(d) for d in data.get('default_values', [])
]

def to_dict(self) -> SelectMenuPayload:
payload: SelectMenuPayload = {
Expand All @@ -291,6 +298,8 @@ def to_dict(self) -> SelectMenuPayload:
payload['options'] = [op.to_dict() for op in self.options]
if self.channel_types:
payload['channel_types'] = [t.value for t in self.channel_types]
if self.default_values:
payload["default_values"] = [v.to_dict() for v in self.default_values]

return payload

Expand Down Expand Up @@ -512,6 +521,79 @@ def default(self) -> Optional[str]:
return self.value


class SelectDefaultValue:
"""Represents a select menu's default value.
These can be created by users.
.. versionadded:: 2.4
Parameters
-----------
id: :class:`int`
The id of a role, user, or channel.
type: :class:`SelectDefaultValueType`
The type of value that ``id`` represents.
"""

def __init__(
self,
*,
id: int,
type: SelectDefaultValueType,
) -> None:
self.id: int = id
self._type: SelectDefaultValueType = type

@property
def type(self) -> SelectDefaultValueType:
return self._type

@type.setter
def type(self, value: SelectDefaultValueType) -> None:
if not isinstance(value, SelectDefaultValueType):
raise TypeError(f'expected SelectDefaultValueType, received {value.__class__.__name__} instead')

self._type = value

def __repr__(self) -> str:
return f'<SelectDefaultValue id={self.id!r} type={self.type!r}>'

@classmethod
def from_dict(cls, data: SelectDefaultValuesPayload) -> SelectDefaultValue:
return cls(
id=data['id'],
type=try_enum(SelectDefaultValueType, data['type']),
)

def to_dict(self) -> SelectDefaultValuesPayload:
return {
'id': self.id,
'type': self._type.value,
}

@classmethod
def from_channel(cls, channel: Snowflake, /) -> Self:
return cls(
id=channel.id,
type=SelectDefaultValueType.channel,
)

@classmethod
def from_role(cls, role: Snowflake, /) -> Self:
return cls(
id=role.id,
type=SelectDefaultValueType.role,
)

@classmethod
def from_user(cls, user: Snowflake, /) -> Self:
return cls(
id=user.id,
type=SelectDefaultValueType.user,
)


@overload
def _component_factory(data: ActionRowChildComponentPayload) -> Optional[ActionRowChildComponentType]:
...
Expand Down
7 changes: 7 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
'AutoModRuleActionType',
'ForumLayoutType',
'ForumOrderType',
'SelectDefaultValueType',
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -772,6 +773,12 @@ class ForumOrderType(Enum):
creation_date = 1


class SelectDefaultValueType(Enum):
user = 'user'
role = 'role'
channel = 'channel'


def create_unknown_value(cls: Type[E], val: Any) -> E:
value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below
name = f'unknown_{val}'
Expand Down
11 changes: 11 additions & 0 deletions discord/types/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ComponentType = Literal[1, 2, 3, 4]
ButtonStyle = Literal[1, 2, 3, 4, 5]
TextStyle = Literal[1, 2]
DefaultValueType = Literal['user', 'role', 'channel']


class ActionRow(TypedDict):
Expand Down Expand Up @@ -66,26 +67,35 @@ class SelectComponent(TypedDict):
disabled: NotRequired[bool]


class SelectDefaultValues(TypedDict):
id: int
type: DefaultValueType


class StringSelectComponent(SelectComponent):
type: Literal[3]
options: NotRequired[List[SelectOption]]


class UserSelectComponent(SelectComponent):
type: Literal[5]
default_values: NotRequired[List[SelectDefaultValues]]


class RoleSelectComponent(SelectComponent):
type: Literal[6]
default_values: NotRequired[List[SelectDefaultValues]]


class MentionableSelectComponent(SelectComponent):
type: Literal[7]
default_values: NotRequired[List[SelectDefaultValues]]


class ChannelSelectComponent(SelectComponent):
type: Literal[8]
channel_types: NotRequired[List[ChannelType]]
default_values: NotRequired[List[SelectDefaultValues]]


class TextInput(TypedDict):
Expand All @@ -104,6 +114,7 @@ class SelectMenu(SelectComponent):
type: Literal[3, 5, 6, 7, 8]
options: NotRequired[List[SelectOption]]
channel_types: NotRequired[List[ChannelType]]
default_values: NotRequired[List[SelectDefaultValues]]


ActionRowChildComponent = Union[ButtonComponent, SelectMenu, TextInput]
Expand Down

0 comments on commit c5ecc42

Please sign in to comment.