Browse Source

Add support for discord.ui.Select.disabled

pull/7297/head
Lucas Hardt 4 years ago
committed by GitHub
parent
commit
ca9b371982
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      discord/components.py
  2. 1
      discord/types/components.py
  3. 22
      discord/ui/select.py

5
discord/components.py

@ -226,6 +226,8 @@ class SelectMenu(Component):
Defaults to 1 and must be between 1 and 25. Defaults to 1 and must be between 1 and 25.
options: List[:class:`SelectOption`] options: List[:class:`SelectOption`]
A list of options that can be selected in this menu. A list of options that can be selected in this menu.
disabled: :class:`bool`
Whether the select is disabled or not.
""" """
__slots__: Tuple[str, ...] = ( __slots__: Tuple[str, ...] = (
@ -234,6 +236,7 @@ class SelectMenu(Component):
'min_values', 'min_values',
'max_values', 'max_values',
'options', 'options',
'disabled',
) )
__repr_info__: ClassVar[Tuple[str, ...]] = __slots__ __repr_info__: ClassVar[Tuple[str, ...]] = __slots__
@ -245,6 +248,7 @@ class SelectMenu(Component):
self.min_values: int = data.get('min_values', 1) self.min_values: int = data.get('min_values', 1)
self.max_values: int = data.get('max_values', 1) self.max_values: int = data.get('max_values', 1)
self.options: List[SelectOption] = [SelectOption.from_dict(option) for option in data.get('options', [])] self.options: List[SelectOption] = [SelectOption.from_dict(option) for option in data.get('options', [])]
self.disabled: bool = data.get('disabled', False)
def to_dict(self) -> SelectMenuPayload: def to_dict(self) -> SelectMenuPayload:
payload: SelectMenuPayload = { payload: SelectMenuPayload = {
@ -253,6 +257,7 @@ class SelectMenu(Component):
'min_values': self.min_values, 'min_values': self.min_values,
'max_values': self.max_values, 'max_values': self.max_values,
'options': [op.to_dict() for op in self.options], 'options': [op.to_dict() for op in self.options],
'disabled': self.disabled,
} }
if self.placeholder: if self.placeholder:

1
discord/types/components.py

@ -53,6 +53,7 @@ class _SelectMenuOptional(TypedDict, total=False):
placeholder: str placeholder: str
min_values: int min_values: int
max_values: int max_values: int
disabled: bool
class _SelectOptionsOptional(TypedDict, total=False): class _SelectOptionsOptional(TypedDict, total=False):

22
discord/ui/select.py

@ -78,6 +78,8 @@ class Select(Item[V]):
Defaults to 1 and must be between 1 and 25. Defaults to 1 and must be between 1 and 25.
options: List[:class:`discord.SelectOption`] options: List[:class:`discord.SelectOption`]
A list of options that can be selected in this menu. A list of options that can be selected in this menu.
disabled: :class:`bool`
Whether the select is disabled or not.
row: Optional[:class:`int`] row: Optional[:class:`int`]
The relative row this select menu belongs to. A Discord component can only have 5 The relative row this select menu belongs to. A Discord component can only have 5
rows. By default, items are arranged automatically into those 5 rows. If you'd rows. By default, items are arranged automatically into those 5 rows. If you'd
@ -91,6 +93,7 @@ class Select(Item[V]):
'min_values', 'min_values',
'max_values', 'max_values',
'options', 'options',
'disabled',
) )
def __init__( def __init__(
@ -101,8 +104,10 @@ class Select(Item[V]):
min_values: int = 1, min_values: int = 1,
max_values: int = 1, max_values: int = 1,
options: List[SelectOption] = MISSING, options: List[SelectOption] = MISSING,
disabled: bool = False,
row: Optional[int] = None, row: Optional[int] = None,
) -> None: ) -> None:
super().__init__()
self._selected_values: List[str] = [] self._selected_values: List[str] = []
self._provided_custom_id = custom_id is not MISSING self._provided_custom_id = custom_id is not MISSING
custom_id = os.urandom(16).hex() if custom_id is MISSING else custom_id custom_id = os.urandom(16).hex() if custom_id is MISSING else custom_id
@ -114,6 +119,7 @@ class Select(Item[V]):
min_values=min_values, min_values=min_values,
max_values=max_values, max_values=max_values,
options=options, options=options,
disabled=disabled,
) )
self.row = row self.row = row
@ -240,6 +246,15 @@ class Select(Item[V]):
self._underlying.options.append(option) self._underlying.options.append(option)
@property
def disabled(self) -> bool:
""":class:`bool`: Whether the select is disabled or not."""
return self._underlying.disabled
@disabled.setter
def disabled(self, value: bool):
self._underlying.disabled = bool(value)
@property @property
def values(self) -> List[str]: def values(self) -> List[str]:
"""List[:class:`str`]: A list of values that have been selected by the user.""" """List[:class:`str`]: A list of values that have been selected by the user."""
@ -267,6 +282,7 @@ class Select(Item[V]):
min_values=component.min_values, min_values=component.min_values,
max_values=component.max_values, max_values=component.max_values,
options=component.options, options=component.options,
disabled=component.disabled,
row=None, row=None,
) )
@ -285,6 +301,7 @@ def select(
min_values: int = 1, min_values: int = 1,
max_values: int = 1, max_values: int = 1,
options: List[SelectOption] = MISSING, options: List[SelectOption] = MISSING,
disabled: bool = False,
row: Optional[int] = None, row: Optional[int] = None,
) -> Callable[[ItemCallbackType], ItemCallbackType]: ) -> Callable[[ItemCallbackType], ItemCallbackType]:
"""A decorator that attaches a select menu to a component. """A decorator that attaches a select menu to a component.
@ -317,11 +334,13 @@ def select(
Defaults to 1 and must be between 1 and 25. Defaults to 1 and must be between 1 and 25.
options: List[:class:`discord.SelectOption`] options: List[:class:`discord.SelectOption`]
A list of options that can be selected in this menu. A list of options that can be selected in this menu.
disabled: :class:`bool`
Whether the select is disabled or not. Defaults to ``False``.
""" """
def decorator(func: ItemCallbackType) -> ItemCallbackType: def decorator(func: ItemCallbackType) -> ItemCallbackType:
if not inspect.iscoroutinefunction(func): if not inspect.iscoroutinefunction(func):
raise TypeError('button function must be a coroutine function') raise TypeError('select function must be a coroutine function')
func.__discord_ui_model_type__ = Select func.__discord_ui_model_type__ = Select
func.__discord_ui_model_kwargs__ = { func.__discord_ui_model_kwargs__ = {
@ -331,6 +350,7 @@ def select(
'min_values': min_values, 'min_values': min_values,
'max_values': max_values, 'max_values': max_values,
'options': options, 'options': options,
'disabled': disabled,
} }
return func return func

Loading…
Cancel
Save