|
1 | 1 | import abc
|
2 | 2 | import datetime
|
3 | 3 | import re
|
| 4 | +import contextlib |
4 | 5 | import typing
|
5 | 6 | from typing_extensions import Self
|
6 | 7 |
|
@@ -405,14 +406,28 @@ def gather_options(_options: list[dict[str, typing.Any]]) -> dict[str, typing.An
|
405 | 406 |
|
406 | 407 |
|
407 | 408 | class InteractionContext(BaseInteractionContext, SendMixin):
|
408 |
| - async def defer(self, *, ephemeral: bool = False) -> None: |
| 409 | + async def defer(self, *, ephemeral: bool = False, suppress_error: bool = False) -> None: |
409 | 410 | """
|
410 | 411 | Defer the interaction.
|
411 | 412 |
|
| 413 | + Note: |
| 414 | + This method's ephemeral settings override the ephemeral settings of `send()`. |
| 415 | +
|
| 416 | + For example, deferring with `ephemeral=True` will make the response ephemeral even with |
| 417 | + `send(ephemeral=False)`. |
| 418 | +
|
412 | 419 | Args:
|
413 | 420 | ephemeral: Whether the interaction response should be ephemeral.
|
| 421 | + suppress_error: Should errors on deferring be suppressed than raised. |
414 | 422 |
|
415 | 423 | """
|
| 424 | + if suppress_error: |
| 425 | + with contextlib.suppress(AlreadyDeferred, AlreadyResponded, HTTPException): |
| 426 | + await self._defer(ephemeral=ephemeral) |
| 427 | + else: |
| 428 | + await self._defer(ephemeral=ephemeral) |
| 429 | + |
| 430 | + async def _defer(self, *, ephemeral: bool = False) -> None: |
416 | 431 | if self.deferred:
|
417 | 432 | raise AlreadyDeferred("Interaction has already been responded to.")
|
418 | 433 | if self.responded:
|
@@ -651,15 +666,29 @@ def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
|
651 | 666 | instance.target_type = CommandType(payload["data"]["type"])
|
652 | 667 | return instance
|
653 | 668 |
|
654 |
| - async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
| 669 | + async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False, suppress_error: bool = False) -> None: |
655 | 670 | """
|
656 | 671 | Defer the interaction.
|
657 | 672 |
|
| 673 | + Note: |
| 674 | + This method's ephemeral settings override the ephemeral settings of `send()`. |
| 675 | +
|
| 676 | + For example, deferring with `ephemeral=True` will make the response ephemeral even with |
| 677 | + `send(ephemeral=False)`. |
| 678 | +
|
658 | 679 | Args:
|
659 | 680 | ephemeral: Whether the interaction response should be ephemeral.
|
660 | 681 | edit_origin: Whether to edit the original message instead of sending a new one.
|
| 682 | + suppress_error: Should errors on deferring be suppressed than raised. |
661 | 683 |
|
662 | 684 | """
|
| 685 | + if suppress_error: |
| 686 | + with contextlib.suppress(AlreadyDeferred, AlreadyResponded, HTTPException): |
| 687 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 688 | + else: |
| 689 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 690 | + |
| 691 | + async def _defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
663 | 692 | if self.deferred:
|
664 | 693 | raise AlreadyDeferred("Interaction has already been responded to.")
|
665 | 694 | if self.responded:
|
@@ -745,15 +774,29 @@ def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
|
745 | 774 | instance.values[i] = channel
|
746 | 775 | return instance
|
747 | 776 |
|
748 |
| - async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
| 777 | + async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False, suppress_error: bool = False) -> None: |
749 | 778 | """
|
750 | 779 | Defer the interaction.
|
751 | 780 |
|
| 781 | + Note: |
| 782 | + This method's ephemeral settings override the ephemeral settings of `send()`. |
| 783 | +
|
| 784 | + For example, deferring with `ephemeral=True` will make the response ephemeral even with |
| 785 | + `send(ephemeral=False)`. |
| 786 | +
|
752 | 787 | Args:
|
753 | 788 | ephemeral: Whether the interaction response should be ephemeral.
|
754 | 789 | edit_origin: Whether to edit the original message instead of sending a new one.
|
| 790 | + suppress_error: Should errors on deferring be suppressed than raised. |
755 | 791 |
|
756 | 792 | """
|
| 793 | + if suppress_error: |
| 794 | + with contextlib.suppress(AlreadyDeferred, AlreadyResponded, HTTPException): |
| 795 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 796 | + else: |
| 797 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 798 | + |
| 799 | + async def _defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
757 | 800 | if self.deferred:
|
758 | 801 | raise AlreadyDeferred("Interaction has already been responded to.")
|
759 | 802 | if self.responded:
|
@@ -883,15 +926,29 @@ async def edit(self, message: "Snowflake_Type", **kwargs) -> "interactions.Messa
|
883 | 926 | await self.defer(edit_origin=True)
|
884 | 927 | return await super().edit(message, **kwargs)
|
885 | 928 |
|
886 |
| - async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
| 929 | + async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False, suppress_error: bool = False) -> None: |
887 | 930 | """
|
888 | 931 | Defer the interaction.
|
889 | 932 |
|
| 933 | + Note: |
| 934 | + This method's ephemeral settings override the ephemeral settings of `send()`. |
| 935 | +
|
| 936 | + For example, deferring with `ephemeral=True` will make the response ephemeral even with |
| 937 | + `send(ephemeral=False)`. |
| 938 | +
|
890 | 939 | Args:
|
891 | 940 | ephemeral: Whether the interaction response should be ephemeral.
|
892 | 941 | edit_origin: Whether to edit the original message instead of sending a followup.
|
| 942 | + suppress_error: Should errors on deferring be suppressed than raised. |
893 | 943 |
|
894 | 944 | """
|
| 945 | + if suppress_error: |
| 946 | + with contextlib.suppress(AlreadyDeferred, AlreadyResponded, HTTPException): |
| 947 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 948 | + else: |
| 949 | + await self._defer(ephemeral=ephemeral, edit_origin=edit_origin) |
| 950 | + |
| 951 | + async def _defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None: |
895 | 952 | if self.deferred:
|
896 | 953 | raise AlreadyDeferred("Interaction has already been responded to.")
|
897 | 954 | if self.responded:
|
|
0 commit comments