|
30 | 30 |
|
31 | 31 | from ..interactions import Interaction
|
32 | 32 | from ..state import ConnectionState
|
| 33 | + from ..types.components import Component as ComponentPayload |
33 | 34 |
|
34 | 35 | M = TypeVar("M", bound="Modal", covariant=True)
|
35 | 36 |
|
@@ -343,6 +344,18 @@ def remove_item(self, item: InputText) -> Self:
|
343 | 344 | pass
|
344 | 345 | return self
|
345 | 346 |
|
| 347 | + def refresh(self, interaction: Interaction, data: list[ComponentPayload]): |
| 348 | + components = [ |
| 349 | + component |
| 350 | + for parent_component in data |
| 351 | + for component in parent_component["components"] |
| 352 | + ] |
| 353 | + for component in components: |
| 354 | + for child in self.children: |
| 355 | + if child.custom_id == component["custom_id"]: # type: ignore |
| 356 | + child.refresh_from_modal(interaction, component) |
| 357 | + break |
| 358 | + |
346 | 359 |
|
347 | 360 | class DesignerModal(BaseModal):
|
348 | 361 | """Represents a UI modal compatible with all modal features.
|
@@ -406,6 +419,10 @@ def add_item(self, item: ModalItem) -> Self:
|
406 | 419 | super().add_item(item)
|
407 | 420 | return self
|
408 | 421 |
|
| 422 | + def refresh(self, interaction: Interaction, data: list[ComponentPayload]): |
| 423 | + for component, child in zip(data, self.children): |
| 424 | + child.refresh_from_modal(interaction, component) |
| 425 | + |
409 | 426 |
|
410 | 427 | class _ModalWeights:
|
411 | 428 | __slots__ = ("weights",)
|
@@ -465,27 +482,15 @@ def remove_modal(self, modal: BaseModal, user_id):
|
465 | 482 |
|
466 | 483 | async def dispatch(self, user_id: int, custom_id: str, interaction: Interaction):
|
467 | 484 | key = (user_id, custom_id)
|
468 |
| - value = self._modals.get(key) |
| 485 | + modal = self._modals.get(key) |
469 | 486 | if value is None:
|
470 | 487 | return
|
471 |
| - interaction.modal = value |
| 488 | + interaction.modal = modal |
472 | 489 |
|
473 | 490 | try:
|
474 |
| - components = [ |
475 |
| - component |
476 |
| - for parent_component in interaction.data["components"] |
477 |
| - for component in ( |
478 |
| - parent_component.get("components") |
479 |
| - or ( |
480 |
| - [parent_component.get("component")] |
481 |
| - if parent_component.get("component") |
482 |
| - else [parent_component] |
483 |
| - ) |
484 |
| - ) |
485 |
| - ] |
486 |
| - for component, child in zip(components, value.children): |
487 |
| - child.refresh_from_modal(interaction, component) |
488 |
| - await value.callback(interaction) |
489 |
| - self.remove_modal(value, user_id) |
| 491 | + components = interaction.data["components"] |
| 492 | + modal.refresh(interaction, components) |
| 493 | + await modal.callback(interaction) |
| 494 | + self.remove_modal(modal, user_id) |
490 | 495 | except Exception as e:
|
491 |
| - return await value.on_error(e, interaction) |
| 496 | + return await modal.on_error(e, interaction) |
0 commit comments