Skip to content

Commit 50b0f15

Browse files
EepyElvyrai0bs
andauthored
fix!: allow ctx.edit to edit component responses when not deferred (#436)
* fix!: - prevent removal of message contents when editing messages - fix editing components without deferring * Update context.py * style: correct tab usage. * style: correct tab usage. * pre-commit: allow black formatting. Co-authored-by: fl0w <41456914+goverfl0w@users.noreply.github.com>
1 parent ce62812 commit 50b0f15

File tree

1 file changed

+128
-87
lines changed

1 file changed

+128
-87
lines changed

interactions/context.py

Lines changed: 128 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,29 @@ async def send(
183183
:return: The sent message as an object.
184184
:rtype: Message
185185
"""
186-
_content: str = "" if content is None else content
186+
if (
187+
content is None
188+
and self.message
189+
and self.callback == InteractionCallbackType.DEFERRED_UPDATE_MESSAGE
190+
):
191+
_content = self.message.content
192+
else:
193+
_content: str = "" if content is None else content
187194
_tts: bool = False if tts is None else tts
188195
# _file = None if file is None else file
189196
# _attachments = [] if attachments else None
190-
_embeds: list = (
191-
[]
192-
if embeds is None
193-
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
194-
)
197+
if embeds is None and self.message:
198+
_embeds = self.message.embeds
199+
else:
200+
_embeds: list = (
201+
[]
202+
if embeds is None
203+
else (
204+
[embed._json for embed in embeds]
205+
if isinstance(embeds, list)
206+
else [embeds._json]
207+
)
208+
)
195209
_allowed_mentions: dict = {} if allowed_mentions is None else allowed_mentions
196210
_components: List[dict] = [{"type": 1, "components": []}]
197211

@@ -291,6 +305,8 @@ async def send(
291305
if components._json.get("custom_id") or components._json.get("url")
292306
else []
293307
)
308+
elif components is None and self.message:
309+
_components = self.message.components
294310
else:
295311
_components = []
296312

@@ -380,70 +396,37 @@ async def edit(
380396
:return: The edited message as an object.
381397
:rtype: Message
382398
"""
383-
_content: str = "" if content is None else content
399+
_content: str = self.message.content if content is None else content
384400
_tts: bool = False if tts is None else tts
385401
# _file = None if file is None else file
386-
_embeds: list = (
387-
[]
388-
if embeds is None
389-
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
390-
)
402+
403+
if embeds is None:
404+
_embeds = self.message.embeds
405+
else:
406+
_embeds: list = (
407+
[]
408+
if embeds is None
409+
else (
410+
[embed._json for embed in embeds]
411+
if isinstance(embeds, list)
412+
else [embeds._json]
413+
)
414+
)
391415
_allowed_mentions: dict = {} if allowed_mentions is None else allowed_mentions
392416
_message_reference: dict = {} if message_reference is None else message_reference._json
393-
_components: list = [{"type": 1, "components": []}]
394417

395-
if (
396-
isinstance(components, list)
397-
and components
398-
and all(isinstance(action_row, ActionRow) for action_row in components)
399-
):
400-
_components = [
401-
{
402-
"type": 1,
403-
"components": [
404-
(
405-
component._json
406-
if component._json.get("custom_id") or component._json.get("url")
407-
else []
408-
)
409-
for component in action_row.components
410-
],
411-
}
412-
for action_row in components
413-
]
414-
elif (
415-
isinstance(components, list)
416-
and components
417-
and all(isinstance(component, (Button, SelectMenu)) for component in components)
418-
):
419-
if isinstance(components[0], SelectMenu):
420-
components[0]._json["options"] = [option._json for option in components[0].options]
421-
_components = [
422-
{
423-
"type": 1,
424-
"components": [
425-
(
426-
component._json
427-
if component._json.get("custom_id") or component._json.get("url")
428-
else []
429-
)
430-
for component in components
431-
],
432-
}
433-
]
434-
elif (
435-
isinstance(components, list)
436-
and components
437-
and all(isinstance(action_row, (list, ActionRow)) for action_row in components)
438-
):
418+
if components is None:
419+
_components = self.message.components
420+
elif components == []:
439421
_components = []
440-
for action_row in components:
441-
for component in (
442-
action_row if isinstance(action_row, list) else action_row.components
443-
):
444-
if isinstance(component, SelectMenu):
445-
component._json["options"] = [option._json for option in component.options]
446-
_components.append(
422+
else:
423+
_components: list = [{"type": 1, "components": []}]
424+
if (
425+
isinstance(components, list)
426+
and components
427+
and all(isinstance(action_row, ActionRow) for action_row in components)
428+
):
429+
_components = [
447430
{
448431
"type": 1,
449432
"components": [
@@ -452,33 +435,82 @@ async def edit(
452435
if component._json.get("custom_id") or component._json.get("url")
453436
else []
454437
)
455-
for component in (
456-
action_row
457-
if isinstance(action_row, list)
458-
else action_row.components
438+
for component in action_row.components
439+
],
440+
}
441+
for action_row in components
442+
]
443+
elif (
444+
isinstance(components, list)
445+
and components
446+
and all(isinstance(component, (Button, SelectMenu)) for component in components)
447+
):
448+
if isinstance(components[0], SelectMenu):
449+
components[0]._json["options"] = [
450+
option._json for option in components[0].options
451+
]
452+
_components = [
453+
{
454+
"type": 1,
455+
"components": [
456+
(
457+
component._json
458+
if component._json.get("custom_id") or component._json.get("url")
459+
else []
459460
)
461+
for component in components
460462
],
461463
}
462-
)
463-
elif isinstance(components, ActionRow):
464-
_components[0]["components"] = [
465-
(
466-
component._json
467-
if component._json.get("custom_id") or component._json.get("url")
464+
]
465+
elif (
466+
isinstance(components, list)
467+
and components
468+
and all(isinstance(action_row, (list, ActionRow)) for action_row in components)
469+
):
470+
_components = []
471+
for action_row in components:
472+
for component in (
473+
action_row if isinstance(action_row, list) else action_row.components
474+
):
475+
if isinstance(component, SelectMenu):
476+
component._json["options"] = [
477+
option._json for option in component.options
478+
]
479+
_components.append(
480+
{
481+
"type": 1,
482+
"components": [
483+
(
484+
component._json
485+
if component._json.get("custom_id")
486+
or component._json.get("url")
487+
else []
488+
)
489+
for component in (
490+
action_row
491+
if isinstance(action_row, list)
492+
else action_row.components
493+
)
494+
],
495+
}
496+
)
497+
elif isinstance(components, ActionRow):
498+
_components[0]["components"] = [
499+
(
500+
component._json
501+
if component._json.get("custom_id") or component._json.get("url")
502+
else []
503+
)
504+
for component in components.components
505+
]
506+
elif isinstance(components, (Button, SelectMenu)):
507+
_components[0]["components"] = (
508+
[components._json]
509+
if components._json.get("custom_id") or components._json.get("url")
468510
else []
469511
)
470-
for component in components.components
471-
]
472-
elif isinstance(components, (Button, SelectMenu)):
473-
_components[0]["components"] = (
474-
[components._json]
475-
if components._json.get("custom_id") or components._json.get("url")
476-
else []
477-
)
478-
elif components is None:
479-
_components = None
480-
else:
481-
_components = []
512+
else:
513+
_components = []
482514

483515
payload: Message = Message(
484516
content=_content,
@@ -491,7 +523,16 @@ async def edit(
491523
)
492524

493525
async def func():
494-
if self.deferred:
526+
if not self.deferred and self.type == InteractionType.MESSAGE_COMPONENT:
527+
self.callback = InteractionCallbackType.UPDATE_MESSAGE
528+
await self.client.create_interaction_response(
529+
data={"type": self.callback.value, "data": payload._json},
530+
token=self.token,
531+
application_id=int(self.id),
532+
)
533+
self.message = payload
534+
self.responded = True
535+
elif self.deferred:
495536
if (
496537
self.type == InteractionType.MESSAGE_COMPONENT
497538
and self.callback != InteractionCallbackType.DEFERRED_UPDATE_MESSAGE

0 commit comments

Comments
 (0)