Skip to content

Commit d2e059d

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

File tree

5 files changed

+59
-23
lines changed

5 files changed

+59
-23
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/modal.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
from typing_extensions import Self
4242

4343
from ..interactions import Interaction
44-
from ..types.interactions import ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload
44+
from ..types.interactions import (
45+
ModalSubmitComponentInteractionData as ModalSubmitComponentInteractionDataPayload,
46+
ResolvedData as ResolvedDataPayload,
47+
)
4548

4649

4750
# fmt: off
@@ -168,27 +171,42 @@ async def on_error(self, interaction: Interaction[ClientT], error: Exception, /)
168171
"""
169172
_log.error('Ignoring exception in modal %r:', self, exc_info=error)
170173

171-
def _refresh(self, interaction: Interaction, components: Sequence[ModalSubmitComponentInteractionDataPayload]) -> None:
174+
def _refresh(
175+
self,
176+
interaction: Interaction,
177+
components: Sequence[ModalSubmitComponentInteractionDataPayload],
178+
resolved: Optional[ResolvedDataPayload],
179+
) -> None:
172180
for component in components:
173181
if component['type'] == 1:
174-
self._refresh(interaction, component['components'])
182+
self._refresh(interaction, component['components'], resolved) # type: ignore
175183
elif component['type'] == 18:
176-
self._refresh(interaction, [component['component']])
184+
self._refresh(interaction, [component['component']], resolved) # type: ignore
177185
else:
178186
custom_id = component.get('custom_id')
179187
if custom_id is None:
180188
continue
181189

182-
item = find(lambda i: getattr(i, 'custom_id', None) == custom_id, self.walk_children())
183-
if item is None:
184-
_log.debug('Modal interaction referencing unknown item custom_id %s. Discarding', custom_id)
185-
continue
190+
item = find(
191+
lambda i: getattr(i, 'custom_id', None) == custom_id,
192+
self.walk_children(),
193+
)
194+
195+
# resolved items are sent at once in the modal submit payload
196+
if component['type'] in (5, 6, 7, 8, 19):
197+
component['resolved'] = resolved or {} # type: ignore
198+
186199
item._refresh_state(interaction, component) # type: ignore
187200

188-
async def _scheduled_task(self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]):
201+
async def _scheduled_task(
202+
self,
203+
interaction: Interaction,
204+
components: List[ModalSubmitComponentInteractionDataPayload],
205+
resolved: Optional[ResolvedDataPayload],
206+
):
189207
try:
190208
self._refresh_timeout()
191-
self._refresh(interaction, components)
209+
self._refresh(interaction, components, resolved)
192210

193211
allow = await self.interaction_check(interaction)
194212
if not allow:
@@ -225,10 +243,13 @@ def key(item: Item) -> int:
225243
return components
226244

227245
def _dispatch_submit(
228-
self, interaction: Interaction, components: List[ModalSubmitComponentInteractionDataPayload]
246+
self,
247+
interaction: Interaction,
248+
components: List[ModalSubmitComponentInteractionDataPayload],
249+
resolved: Optional[ResolvedDataPayload],
229250
) -> asyncio.Task[None]:
230251
return asyncio.create_task(
231-
self._scheduled_task(interaction, components), name=f'discord-ui-modal-dispatch-{self.id}'
252+
self._scheduled_task(interaction, components, resolved), name=f'discord-ui-modal-dispatch-{self.id}'
232253
)
233254

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

discord/ui/select.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,15 @@ def _refresh_component(self, component: SelectMenu) -> None:
359359
def _refresh_state(self, interaction: Interaction, data: SelectMessageComponentInteractionData) -> None:
360360
values = selected_values.get({})
361361
payload: List[PossibleValue]
362+
string_values = data.get('values', [])
362363
try:
363364
resolved = Namespace._get_resolved_items(
364365
interaction,
365366
data['resolved'], # pyright: ignore[reportTypedDictNotRequiredAccess]
366367
)
367-
payload = list(resolved.values())
368+
payload = [v for k, v in resolved.items() if k.id in string_values]
368369
except KeyError:
369-
payload = data.get('values', []) # type: ignore
370+
payload = list(string_values)
370371

371372
self._values = values[self.custom_id] = payload
372373
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: Optional[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)