mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-06 18:11:59 +00:00
Fix ui.Select.values in modals
This commit is contained in:
parent
584c22f642
commit
9c38cf3aef
@ -174,9 +174,11 @@ class Modal(View):
|
|||||||
continue
|
continue
|
||||||
item._refresh_state(component) # type: ignore
|
item._refresh_state(component) # type: ignore
|
||||||
|
|
||||||
async def _scheduled_task(self, interaction: Interaction):
|
async def _scheduled_task(self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]):
|
||||||
try:
|
try:
|
||||||
self._refresh_timeout()
|
self._refresh_timeout()
|
||||||
|
self._refresh(components)
|
||||||
|
|
||||||
allow = await self.interaction_check(interaction)
|
allow = await self.interaction_check(interaction)
|
||||||
if not allow:
|
if not allow:
|
||||||
return
|
return
|
||||||
@ -189,8 +191,10 @@ class Modal(View):
|
|||||||
# In the future, maybe this will require checking if we set an error response.
|
# In the future, maybe this will require checking if we set an error response.
|
||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
def _dispatch_submit(self, interaction: Interaction) -> None:
|
def _dispatch_submit(
|
||||||
asyncio.create_task(self._scheduled_task(interaction), name=f'discord-ui-modal-dispatch-{self.id}')
|
self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]
|
||||||
|
) -> None:
|
||||||
|
asyncio.create_task(self._scheduled_task(interaction, components), name=f'discord-ui-modal-dispatch-{self.id}')
|
||||||
|
|
||||||
def to_dict(self) -> Dict[str, Any]:
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
payload = {
|
payload = {
|
||||||
|
@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import List, Literal, Optional, TYPE_CHECKING, Tuple, TypeVar, Callable, Union
|
from typing import List, Literal, Optional, TYPE_CHECKING, Tuple, TypeVar, Callable, Union, Dict
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
@ -54,7 +54,7 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
V = TypeVar('V', bound='View', covariant=True)
|
V = TypeVar('V', bound='View', covariant=True)
|
||||||
|
|
||||||
selected_values: ContextVar[Optional[List[str]]] = ContextVar('selected_values', default=None)
|
selected_values: ContextVar[Dict[str, List[str]]] = ContextVar('selected_values')
|
||||||
|
|
||||||
|
|
||||||
class Select(Item[V]):
|
class Select(Item[V]):
|
||||||
@ -126,6 +126,7 @@ class Select(Item[V]):
|
|||||||
disabled=disabled,
|
disabled=disabled,
|
||||||
)
|
)
|
||||||
self.row = row
|
self.row = row
|
||||||
|
self._values: List[str] = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def custom_id(self) -> str:
|
def custom_id(self) -> str:
|
||||||
@ -262,8 +263,8 @@ class Select(Item[V]):
|
|||||||
@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."""
|
||||||
values = selected_values.get()
|
values = selected_values.get(None)
|
||||||
return values if values is not None else []
|
return self._values if values is None else values.get(self.custom_id, [])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def width(self) -> int:
|
def width(self) -> int:
|
||||||
@ -276,7 +277,9 @@ class Select(Item[V]):
|
|||||||
self._underlying = component
|
self._underlying = component
|
||||||
|
|
||||||
def _refresh_state(self, data: MessageComponentInteractionData) -> None:
|
def _refresh_state(self, data: MessageComponentInteractionData) -> None:
|
||||||
selected_values.set(data.get('values', []))
|
values = selected_values.get({})
|
||||||
|
self._values = values[self.custom_id] = data.get('values', [])
|
||||||
|
selected_values.set(values)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_component(cls, component: SelectMenu) -> Self:
|
def from_component(cls, component: SelectMenu) -> Self:
|
||||||
|
@ -416,6 +416,8 @@ class View:
|
|||||||
if self.timeout:
|
if self.timeout:
|
||||||
self.__timeout_expiry = time.monotonic() + self.timeout
|
self.__timeout_expiry = time.monotonic() + self.timeout
|
||||||
|
|
||||||
|
item._refresh_state(interaction.data) # type: ignore
|
||||||
|
|
||||||
allow = await self.interaction_check(interaction)
|
allow = await self.interaction_check(interaction)
|
||||||
if not allow:
|
if not allow:
|
||||||
return
|
return
|
||||||
@ -615,7 +617,6 @@ class ViewStore:
|
|||||||
if item is None:
|
if item is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
item._refresh_state(interaction.data) # type: ignore
|
|
||||||
# Note, at this point the View is *not* None
|
# Note, at this point the View is *not* None
|
||||||
item.view._dispatch_item(item, interaction) # type: ignore
|
item.view._dispatch_item(item, interaction) # type: ignore
|
||||||
|
|
||||||
@ -630,8 +631,7 @@ class ViewStore:
|
|||||||
_log.debug("Modal interaction referencing unknown custom_id %s. Discarding", custom_id)
|
_log.debug("Modal interaction referencing unknown custom_id %s. Discarding", custom_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
modal._refresh(components)
|
modal._dispatch_submit(interaction, components)
|
||||||
modal._dispatch_submit(interaction)
|
|
||||||
|
|
||||||
def remove_interaction_mapping(self, interaction_id: int) -> None:
|
def remove_interaction_mapping(self, interaction_id: int) -> None:
|
||||||
# This is called before re-adding the view
|
# This is called before re-adding the view
|
||||||
|
Loading…
x
Reference in New Issue
Block a user