Skip to content

Commit a1068fb

Browse files
authored
update refresh logic
1 parent 5255da1 commit a1068fb

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

discord/ui/input_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def to_component_dict(self) -> InputTextComponentPayload:
238238
def refresh_state(self, data) -> None:
239239
self._input_value = data["value"]
240240

241-
def refresh_from_modal(self, interaction: Interaction, data: dict) -> None:
241+
def refresh_from_modal(self, interaction: Interaction, data: InputTextComponentPayload) -> None:
242242
return self.refresh_state(data)
243243

244244

discord/ui/label.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def to_component_dict(self) -> LabelPayload:
270270
self._set_component_from_item(self.item)
271271
return self._underlying.to_dict()
272272

273+
def refresh_from_modal(self, interaction: Interaction, data: LabelPayload) -> None:
274+
return self.item.refresh_from_modal(interaction, data.get("component", {}))
275+
273276
@classmethod
274277
def from_component(cls: type[L], component: LabelComponent) -> L:
275278
from .view import _component_to_item

discord/ui/modal.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from ..interactions import Interaction
3232
from ..state import ConnectionState
33+
from ..types.components import Component as ComponentPayload
3334

3435
M = TypeVar("M", bound="Modal", covariant=True)
3536

@@ -343,6 +344,18 @@ def remove_item(self, item: InputText) -> Self:
343344
pass
344345
return self
345346

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+
346359

347360
class DesignerModal(BaseModal):
348361
"""Represents a UI modal compatible with all modal features.
@@ -406,6 +419,10 @@ def add_item(self, item: ModalItem) -> Self:
406419
super().add_item(item)
407420
return self
408421

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+
409426

410427
class _ModalWeights:
411428
__slots__ = ("weights",)
@@ -465,27 +482,15 @@ def remove_modal(self, modal: BaseModal, user_id):
465482

466483
async def dispatch(self, user_id: int, custom_id: str, interaction: Interaction):
467484
key = (user_id, custom_id)
468-
value = self._modals.get(key)
485+
modal = self._modals.get(key)
469486
if value is None:
470487
return
471-
interaction.modal = value
488+
interaction.modal = modal
472489

473490
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)
490495
except Exception as e:
491-
return await value.on_error(e, interaction)
496+
return await modal.on_error(e, interaction)

discord/ui/select.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def refresh_state(self, interaction: Interaction | dict) -> None:
724724
self._selected_values = data.get("values", [])
725725
self._interaction = interaction
726726

727-
def refresh_from_modal(self, interaction: Interaction | dict, data: dict) -> None:
727+
def refresh_from_modal(self, interaction: Interaction | dict, data: SelectMenuPayload) -> None:
728728
self._selected_values = data.get("values", [])
729729
self._interaction = interaction
730730

discord/ui/view.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ def refresh(self, components: list[Component]):
737737
self.children = children
738738

739739
def is_components_v2(self) -> bool:
740+
"""Whether the view contains V2 components.
741+
742+
A view containing V2 components cannot be sent alongside message content or embeds.
743+
744+
This is always ``False`` for :class:`View`.
745+
"""
740746
return False
741747

742748

@@ -883,13 +889,7 @@ def refresh(self, components: list[Component]):
883889
i += 1
884890

885891
def is_components_v2(self) -> bool:
886-
"""Whether the view contains V2 components or requires the V2 flag.
887-
888-
A view containing V2 components cannot be sent alongside message content or embeds.
889-
"""
890-
return len(self.children) > 5 or any(
891-
i.is_components_v2() for i in self.children
892-
)
892+
return len(self.children) > 5 or super().is_components_v2()
893893

894894

895895
class ViewStore:

0 commit comments

Comments
 (0)