Skip to content

Commit f6658a5

Browse files
committed
Handle resolved data for modal components and types
1 parent 9931d7c commit f6658a5

File tree

6 files changed

+84
-21
lines changed

6 files changed

+84
-21
lines changed

discord/state.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ def parse_interaction_create(self, data: gw.InteractionCreateEvent) -> None:
828828
inner_data = data['data']
829829
custom_id = inner_data['custom_id']
830830
components = inner_data['components']
831-
self._view_store.dispatch_modal(custom_id, interaction, components)
831+
resolved = inner_data.get('resolved', {})
832+
self._view_store.dispatch_modal(custom_id, interaction, components, resolved)
832833
self.dispatch('interaction', interaction)
833834

834835
def parse_presence_update(self, data: gw.PresenceUpdateEvent) -> None:

discord/types/interactions.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .snowflake import Snowflake
3737
from .user import User
3838
from .guild import GuildFeature
39+
from .components import ComponentBase
3940

4041
if TYPE_CHECKING:
4142
from .message import Message
@@ -204,39 +205,47 @@ class SelectMessageComponentInteractionData(_BaseMessageComponentInteractionData
204205
MessageComponentInteractionData = Union[ButtonMessageComponentInteractionData, SelectMessageComponentInteractionData]
205206

206207

207-
class ModalSubmitTextInputInteractionData(TypedDict):
208+
class ModalSubmitTextInputInteractionData(ComponentBase):
208209
type: Literal[4]
209210
custom_id: str
210211
value: str
211212

212213

213-
class ModalSubmitStringSelectInteractionData(TypedDict):
214-
type: Literal[3]
214+
class ModalSubmitSelectInteractionData(ComponentBase):
215+
type: Literal[3, 5, 6, 7, 8]
215216
custom_id: str
216217
values: List[str]
217218

218219

219-
ModalSubmitComponentItemInteractionData = Union[ModalSubmitTextInputInteractionData, ModalSubmitStringSelectInteractionData]
220+
ModalSubmitComponentItemInteractionData = Union[ModalSubmitSelectInteractionData, ModalSubmitTextInputInteractionData]
220221

221222

222223
class ModalSubmitActionRowInteractionData(TypedDict):
223224
type: Literal[1]
224225
components: List[ModalSubmitComponentItemInteractionData]
225226

226227

227-
class ModalSubmitLabelInteractionData(TypedDict):
228+
class ModalSubmitTextDisplayInteractionData(ComponentBase):
229+
type: Literal[10]
230+
content: str
231+
232+
233+
class ModalSubmitLabelInteractionData(ComponentBase):
228234
type: Literal[18]
229235
component: ModalSubmitComponentItemInteractionData
230236

231237

232238
ModalSubmitComponentInteractionData = Union[
233-
ModalSubmitLabelInteractionData, ModalSubmitActionRowInteractionData, ModalSubmitComponentItemInteractionData
239+
ModalSubmitActionRowInteractionData,
240+
ModalSubmitTextDisplayInteractionData,
241+
ModalSubmitLabelInteractionData,
234242
]
235243

236244

237245
class ModalSubmitInteractionData(TypedDict):
238246
custom_id: str
239247
components: List[ModalSubmitComponentInteractionData]
248+
resolved: NotRequired[ResolvedData]
240249

241250

242251
InteractionData = Union[

discord/ui/item.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .action_row import ActionRow
4646
from .container import Container
4747
from .dynamic import DynamicItem
48+
from ..app_commands.namespace import ResolveKey
4849

4950
I = TypeVar('I', bound='Item[Any]')
5051
V = TypeVar('V', bound='BaseView', covariant=True)
@@ -97,6 +98,9 @@ def to_component_dict(self) -> Dict[str, Any]:
9798
def _refresh_component(self, component: Component) -> None:
9899
return None
99100

101+
def _handle_submit(self, interaction: Interaction, data: Dict[str, Any], resolved: Dict[ResolveKey, Any]) -> None:
102+
return self._refresh_state(interaction, data)
103+
100104
def _refresh_state(self, interaction: Interaction, data: Dict[str, Any]) -> None:
101105
return None
102106

discord/ui/modal.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@
3636
from .view import BaseView
3737
from .select import BaseSelect
3838
from .text_input import TextInput
39+
from ..interactions import Namespace
3940

4041
if TYPE_CHECKING:
4142
from typing_extensions import Self
4243

4344
from ..interactions import Interaction
44-
from ..types.interactions import ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload
45+
from ..types.interactions import (
46+
ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload,
47+
ResolvedData as ResolvedDataPayload,
48+
)
49+
from ..app_commands.namespace import ResolveKey
4550

4651

4752
# fmt: off
@@ -168,27 +173,41 @@ async def on_error(self, interaction: Interaction[ClientT], error: Exception, /)
168173
"""
169174
_log.error('Ignoring exception in modal %r:', self, exc_info=error)
170175

171-
def _refresh(self, interaction: Interaction, components: Sequence[ModalSubmitComponentInteractionDataPayload]) -> None:
176+
def _refresh(
177+
self,
178+
interaction: Interaction,
179+
components: Sequence[ModalSubmitComponentInteractionDataPayload],
180+
resolved: Dict[ResolveKey, Any],
181+
) -> None:
172182
for component in components:
173183
if component['type'] == 1:
174-
self._refresh(interaction, component['components'])
184+
self._refresh(interaction, component['components'], resolved) # type: ignore
175185
elif component['type'] == 18:
176-
self._refresh(interaction, [component['component']])
186+
self._refresh(interaction, [component['component']], resolved) # type: ignore
177187
else:
178188
custom_id = component.get('custom_id')
179189
if custom_id is None:
180190
continue
181191

182-
item = find(lambda i: getattr(i, 'custom_id', None) == custom_id, self.walk_children())
192+
item = find(
193+
lambda i: getattr(i, 'custom_id', None) == custom_id,
194+
self.walk_children(),
195+
)
183196
if item is None:
184197
_log.debug('Modal interaction referencing unknown item custom_id %s. Discarding', custom_id)
185198
continue
186-
item._refresh_state(interaction, component) # type: ignore
187199

188-
async def _scheduled_task(self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]):
200+
item._handle_submit(interaction, component, resolved) # type: ignore
201+
202+
async def _scheduled_task(
203+
self,
204+
interaction: Interaction,
205+
components: List[ModalSubmitComponentInteractionDataPayload],
206+
resolved: Dict[ResolveKey, Any],
207+
):
189208
try:
190209
self._refresh_timeout()
191-
self._refresh(interaction, components)
210+
self._refresh(interaction, components, resolved)
192211

193212
allow = await self.interaction_check(interaction)
194213
if not allow:
@@ -225,10 +244,18 @@ def key(item: Item) -> int:
225244
return components
226245

227246
def _dispatch_submit(
228-
self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]
247+
self,
248+
interaction: Interaction,
249+
components: List[ModalSubmitComponentInteractionDataPayload],
250+
resolved: ResolvedDataPayload,
229251
) -> asyncio.Task[None]:
252+
try:
253+
namespace = Namespace._get_resolved_items(interaction, resolved)
254+
except KeyError:
255+
namespace = {}
256+
230257
return asyncio.create_task(
231-
self._scheduled_task(interaction, components), name=f'discord-ui-modal-dispatch-{self.id}'
258+
self._scheduled_task(interaction, components, namespace), name=f'discord-ui-modal-dispatch-{self.id}'
232259
)
233260

234261
def to_dict(self) -> Dict[str, Any]:

discord/ui/select.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
from ..types.interactions import SelectMessageComponentInteractionData
7979
from ..app_commands import AppCommandChannel, AppCommandThread
8080
from ..interactions import Interaction
81+
from ..app_commands.namespace import ResolveKey
8182

8283
ValidSelectType: TypeAlias = Literal[
8384
ComponentType.string_select,
@@ -356,7 +357,24 @@ def to_component_dict(self) -> SelectMenuPayload:
356357
def _refresh_component(self, component: SelectMenu) -> None:
357358
self._underlying = component
358359

359-
def _refresh_state(self, interaction: Interaction, data: SelectMessageComponentInteractionData) -> None:
360+
def _handle_submit(
361+
self, interaction: Interaction, data: SelectMessageComponentInteractionData, resolved: Dict[ResolveKey, Any]
362+
) -> None:
363+
payload: List[PossibleValue]
364+
values = selected_values.get({})
365+
string_values = data.get('values', [])
366+
payload = [v for k, v in resolved.items() if k.id in string_values]
367+
if not payload:
368+
payload = list(string_values)
369+
370+
self._values = values[self.custom_id] = payload
371+
selected_values.set(values)
372+
373+
def _refresh_state(
374+
self,
375+
interaction: Interaction,
376+
data: SelectMessageComponentInteractionData,
377+
) -> None:
360378
values = selected_values.get({})
361379
payload: List[PossibleValue]
362380
try:
@@ -366,7 +384,7 @@ def _refresh_state(self, interaction: Interaction, data: SelectMessageComponentI
366384
)
367385
payload = list(resolved.values())
368386
except KeyError:
369-
payload = data.get('values', []) # type: ignore
387+
payload = list(data.get('values', []))
370388

371389
self._values = values[self.custom_id] = payload
372390
selected_values.set(values)

discord/ui/view.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@
8585
from ..interactions import Interaction
8686
from ..message import Message
8787
from ..types.components import ComponentBase as ComponentBasePayload
88-
from ..types.interactions import ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload
88+
from ..types.interactions import (
89+
ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload,
90+
ResolvedData as ResolvedDataPayload,
91+
)
8992
from ..state import ConnectionState
9093
from .modal import Modal
9194

@@ -1041,13 +1044,14 @@ def dispatch_modal(
10411044
custom_id: str,
10421045
interaction: Interaction,
10431046
components: List[ModalSubmitComponentInteractionDataPayload],
1047+
resolved: ResolvedDataPayload,
10441048
) -> None:
10451049
modal = self._modals.get(custom_id)
10461050
if modal is None:
10471051
_log.debug('Modal interaction referencing unknown custom_id %s. Discarding', custom_id)
10481052
return
10491053

1050-
self.add_task(modal._dispatch_submit(interaction, components))
1054+
self.add_task(modal._dispatch_submit(interaction, components, resolved))
10511055

10521056
def remove_interaction_mapping(self, interaction_id: int) -> None:
10531057
# This is called before re-adding the view

0 commit comments

Comments
 (0)