Skip to content

Commit c3c9457

Browse files
committed
feat: support editing messages with modal without resolving
1 parent ef4199b commit c3c9457

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

interactions/models/internal/context.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,8 @@ class ModalContext(InteractionContext):
788788
"""The responses of the modal. The key is the `custom_id` of the component."""
789789
custom_id: str
790790
"""The developer defined custom ID of this modal"""
791+
edit_origin: bool
792+
"""Whether to edit the original message instead of sending a new one."""
791793

792794
@classmethod
793795
def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
@@ -797,8 +799,42 @@ def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
797799
}
798800
instance.kwargs = instance.responses
799801
instance.custom_id = payload["data"]["custom_id"]
802+
instance.edit_origin = False
800803
return instance
801804

805+
async def edit(self, message: "Snowflake_Type", **kwargs) -> "interactions.Message":
806+
if not self.deferred and not self.responded:
807+
await self.defer(edit_origin=True)
808+
return await super().edit(message, **kwargs)
809+
810+
async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None:
811+
"""
812+
Defer the interaction.
813+
814+
Args:
815+
ephemeral: Whether the interaction response should be ephemeral.
816+
edit_origin: Whether to edit the original message instead of sending a followup.
817+
"""
818+
if self.deferred:
819+
raise AlreadyDeferred("Interaction has already been responded to.")
820+
if self.responded:
821+
raise AlreadyResponded("Interaction has already been responded to.")
822+
823+
payload = {
824+
"type": CallbackType.DEFERRED_UPDATE_MESSAGE
825+
if edit_origin
826+
else CallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
827+
}
828+
if ephemeral:
829+
payload["data"] = {"flags": MessageFlags.EPHEMERAL}
830+
831+
if edit_origin:
832+
self.edit_origin = True
833+
834+
await self.client.http.post_initial_response(payload, self.id, self.token)
835+
self.deferred = True
836+
self.ephemeral = ephemeral
837+
802838

803839
class AutocompleteContext(BaseInteractionContext):
804840
focussed_option: SlashCommandOption # todo: option parsing

0 commit comments

Comments
 (0)