Skip to content

Commit 09e8699

Browse files
authored
refactor: Refactor context edit/send/defer. (#908)
* refactor: Refactor context edit/send/defer to not use legacy v3 black magic. * refactor: Utilise LibraryException * refactor: Check for non-ephemeral based errors.
1 parent 1a55229 commit 09e8699

File tree

1 file changed

+119
-109
lines changed

1 file changed

+119
-109
lines changed

interactions/client/context.py

Lines changed: 119 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -352,35 +352,53 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:
352352
msg = None
353353

354354
if self.deferred:
355-
if hasattr(self.message, "id") and self.message.id is not None:
356-
res = await self._client.edit_message(
357-
int(self.channel_id), int(self.message.id), payload=payload
358-
)
359-
self.message = msg = Message(**res, _client=self._client)
360-
else:
361-
res = await self._client.edit_interaction_response(
362-
token=self.token,
363-
application_id=str(self.id),
364-
data={"type": self.callback.value, "data": payload},
365-
message_id=self.message.id if self.message else "@original",
366-
)
367-
if res["flags"] == 64:
368-
log.warning("You can't edit hidden messages.")
355+
if (
356+
hasattr(self.message, "id")
357+
and self.message.id is not None
358+
and self.message.flags != 64
359+
):
360+
try:
361+
res = await self._client.edit_message(
362+
int(self.channel_id), int(self.message.id), payload=payload
363+
)
364+
except LibraryException as e:
365+
if e.code in {10015, 10018}:
366+
log.warning(f"You can't edit hidden messages." f"({e.message}).")
367+
else:
368+
# if its not ephemeral or some other thing.
369+
raise e from e
369370
else:
370-
await self._client.edit_message(
371-
int(self.channel_id), res["id"], payload=payload
371+
self.message = msg = Message(**res, _client=self._client)
372+
else:
373+
try:
374+
res = await self._client.edit_interaction_response(
375+
token=self.token,
376+
application_id=str(self.id),
377+
data=payload,
378+
message_id=self.message.id
379+
if self.message and self.message.flags != 64
380+
else "@original",
372381
)
382+
except LibraryException as e:
383+
if e.code in {10015, 10018}:
384+
log.warning(f"You can't edit hidden messages." f"({e.message}).")
385+
else:
386+
# if its not ephemeral or some other thing.
387+
raise e from e
388+
else:
373389
self.message = msg = Message(**res, _client=self._client)
374390
else:
375-
res = await self._client.edit_interaction_response(
376-
token=self.token,
377-
application_id=str(self.application_id),
378-
data={"type": self.callback.value, "data": payload},
379-
)
380-
if res["flags"] == 64:
381-
log.warning("You can't edit hidden messages.")
391+
try:
392+
res = await self._client.edit_interaction_response(
393+
token=self.token, application_id=str(self.application_id), data=payload
394+
)
395+
except LibraryException as e:
396+
if e.code in {10015, 10018}:
397+
log.warning(f"You can't edit hidden messages." f"({e.message}).")
398+
else:
399+
# if its not ephemeral or some other thing.
400+
raise e from e
382401
else:
383-
await self._client.edit_message(int(self.channel_id), res["id"], payload=payload)
384402
self.message = msg = Message(**res, _client=self._client)
385403

386404
if msg is not None:
@@ -395,15 +413,18 @@ async def defer(self, ephemeral: Optional[bool] = False) -> None:
395413
:param ephemeral?: Whether the deferred state is hidden or not.
396414
:type ephemeral: Optional[bool]
397415
"""
398-
self.deferred = True
399-
_ephemeral: int = (1 << 6) if ephemeral else 0
400-
self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
416+
if not self.responded:
417+
self.deferred = True
418+
_ephemeral: int = (1 << 6) if ephemeral else 0
419+
self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
401420

402-
await self._client.create_interaction_response(
403-
token=self.token,
404-
application_id=int(self.id),
405-
data={"type": self.callback.value, "data": {"flags": _ephemeral}},
406-
)
421+
await self._client.create_interaction_response(
422+
token=self.token,
423+
application_id=int(self.id),
424+
data={"type": self.callback.value, "data": {"flags": _ephemeral}},
425+
)
426+
427+
self.responded = True
407428

408429
async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
409430
payload = await super().send(content, **kwargs)
@@ -414,40 +435,33 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
414435
_payload: dict = {"type": self.callback.value, "data": payload}
415436

416437
msg = None
417-
if self.responded or self.deferred:
418-
if self.deferred:
419-
res = await self._client.edit_interaction_response(
420-
data=payload,
421-
token=self.token,
422-
application_id=str(self.application_id),
423-
)
424-
self.responded = True
425-
else:
426-
res = await self._client._post_followup(
427-
data=payload,
428-
token=self.token,
429-
application_id=str(self.application_id),
430-
)
438+
if self.responded:
439+
res = await self._client._post_followup(
440+
data=payload,
441+
token=self.token,
442+
application_id=str(self.application_id),
443+
)
431444
self.message = msg = Message(**res, _client=self._client)
432445
else:
433446
await self._client.create_interaction_response(
434447
token=self.token,
435448
application_id=int(self.id),
436449
data=_payload,
437450
)
438-
__newdata = await self._client.edit_interaction_response(
439-
data={},
440-
token=self.token,
441-
application_id=str(self.application_id),
442-
)
443-
if not __newdata.get("code"):
444-
# if sending message fails somehow
445-
msg = Message(**__newdata, _client=self._client)
446-
self.message = msg
451+
452+
try:
453+
_msg = await self._client.get_original_interaction_response(
454+
self.token, str(self.application_id)
455+
)
456+
except LibraryException:
457+
pass
458+
else:
459+
self.message = msg = Message(**_msg, _client=self._client)
460+
447461
self.responded = True
462+
448463
if msg is not None:
449464
return msg
450-
451465
return Message(
452466
**payload,
453467
_client=self._client,
@@ -531,6 +545,7 @@ class ComponentContext(_Context):
531545
async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:
532546

533547
payload = await super().edit(content, **kwargs)
548+
msg = None
534549

535550
if not self.deferred:
536551
self.callback = InteractionCallbackType.UPDATE_MESSAGE
@@ -539,33 +554,36 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:
539554
token=self.token,
540555
application_id=int(self.id),
541556
)
542-
payload = Message(**payload, _client=self._client)
543-
for attr in payload.__slots__:
544-
if getattr(self.message, attr, None) and not getattr(payload, attr, None):
545-
setattr(payload, attr, getattr(self.message, attr))
546-
payload._json[attr] = self.message._json[attr]
547-
self.message = payload
557+
558+
try:
559+
_msg = await self._client.get_original_interaction_response(
560+
self.token, str(self.application_id)
561+
)
562+
except LibraryException:
563+
pass
564+
else:
565+
self.message = msg = Message(**_msg, _client=self._client)
566+
548567
self.responded = True
549568
elif self.callback != InteractionCallbackType.DEFERRED_UPDATE_MESSAGE:
550-
res = await self._client._post_followup(
569+
await self._client._post_followup(
551570
data=payload,
552571
token=self.token,
553572
application_id=str(self.application_id),
554573
)
555-
self.message = Message(**res, _client=self._client)
556574
else:
557575
res = await self._client.edit_interaction_response(
558576
data=payload,
559577
token=self.token,
560578
application_id=str(self.application_id),
561579
)
562580
self.responded = True
563-
self.message = Message(**res, _client=self._client)
581+
self.message = msg = Message(**res, _client=self._client)
564582

565-
if self.message is None:
566-
self.message = Message(**payload, _client=self._client)
583+
if msg is not None:
584+
return msg
567585

568-
return self.message
586+
return Message(**payload, _client=self._client)
569587

570588
async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
571589
payload = await super().send(content, **kwargs)
@@ -576,46 +594,34 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message:
576594
_payload: dict = {"type": self.callback.value, "data": payload}
577595

578596
msg = None
579-
if (
580-
self.responded
581-
or self.deferred
582-
or self.callback == InteractionCallbackType.DEFERRED_UPDATE_MESSAGE
583-
):
584-
if self.deferred:
585-
res = await self._client.edit_interaction_response(
586-
data=payload,
587-
token=self.token,
588-
application_id=str(self.application_id),
589-
)
590-
self.responded = True
591-
else:
592-
res = await self._client._post_followup(
593-
data=payload,
594-
token=self.token,
595-
application_id=str(self.application_id),
596-
)
597+
if self.responded:
598+
res = await self._client._post_followup(
599+
data=payload,
600+
token=self.token,
601+
application_id=str(self.application_id),
602+
)
597603
self.message = msg = Message(**res, _client=self._client)
598-
599604
else:
600605
await self._client.create_interaction_response(
601606
token=self.token,
602607
application_id=int(self.id),
603608
data=_payload,
604609
)
605-
__newdata = await self._client.edit_interaction_response(
606-
data={},
607-
token=self.token,
608-
application_id=str(self.application_id),
609-
)
610-
if not __newdata.get("code"):
611-
# if sending message fails somehow
612-
msg = Message(**__newdata, _client=self._client)
613-
self.message = msg
610+
611+
try:
612+
_msg = await self._client.get_original_interaction_response(
613+
self.token, str(self.application_id)
614+
)
615+
except LibraryException:
616+
pass
617+
else:
618+
self.message = msg = Message(**_msg, _client=self._client)
619+
614620
self.responded = True
615621

616622
if msg is not None:
617623
return msg
618-
return Message(**payload)
624+
return Message(**payload, _client=self._client)
619625

620626
async def defer(
621627
self, ephemeral: Optional[bool] = False, edit_origin: Optional[bool] = False
@@ -629,20 +635,24 @@ async def defer(
629635
:param edit_origin?: Whether you want to edit the original message or send a followup message
630636
:type edit_origin: Optional[bool]
631637
"""
632-
self.deferred = True
633-
_ephemeral: int = (1 << 6) if bool(ephemeral) else 0
638+
if not self.responded:
634639

635-
# ephemeral doesn't change callback typings. just data json
636-
if edit_origin:
637-
self.callback = InteractionCallbackType.DEFERRED_UPDATE_MESSAGE
638-
else:
639-
self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
640+
self.deferred = True
641+
_ephemeral: int = (1 << 6) if bool(ephemeral) else 0
640642

641-
await self._client.create_interaction_response(
642-
token=self.token,
643-
application_id=int(self.id),
644-
data={"type": self.callback.value, "data": {"flags": _ephemeral}},
645-
)
643+
# ephemeral doesn't change callback typings. just data json
644+
if edit_origin:
645+
self.callback = InteractionCallbackType.DEFERRED_UPDATE_MESSAGE
646+
else:
647+
self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
648+
649+
await self._client.create_interaction_response(
650+
token=self.token,
651+
application_id=int(self.id),
652+
data={"type": self.callback.value, "data": {"flags": _ephemeral}},
653+
)
654+
655+
self.responded = True
646656

647657
@property
648658
def custom_id(self) -> Optional[str]:

0 commit comments

Comments
 (0)