From e4157431361dc761b1c6e23521d4a062b9cda7cf Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sat, 9 Jul 2022 12:43:01 -0400 Subject: [PATCH 1/4] refactor: Refactor context edit/send/defer to not use legacy v3 black magic. --- interactions/client/context.py | 188 ++++++++++++++++----------------- 1 file changed, 91 insertions(+), 97 deletions(-) diff --git a/interactions/client/context.py b/interactions/client/context.py index 0cb4cfd51..6bc455145 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -352,36 +352,41 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: msg = None if self.deferred: - if hasattr(self.message, "id") and self.message.id is not None: + if ( + hasattr(self.message, "id") + and self.message.id is not None + and self.message.flags != 64 + ): res = await self._client.edit_message( int(self.channel_id), int(self.message.id), payload=payload ) - self.message = msg = Message(**res, _client=self._client) else: res = await self._client.edit_interaction_response( token=self.token, application_id=str(self.id), - data={"type": self.callback.value, "data": payload}, - message_id=self.message.id if self.message else "@original", + data=payload, + message_id=self.message.id + if self.message and self.message.flags != 64 + else "@original", + ) + if not res.get("code"): + self.message = msg = Message(**res, _client=self._client) + elif res["code"] in {10008, 10015}: + log.warning( + f"You can't edit hidden messages " + f"(Unknown {'interactions' if res['code'] == 10015 else 'message'} reference)." ) - if res["flags"] == 64: - log.warning("You can't edit hidden messages.") - else: - await self._client.edit_message( - int(self.channel_id), res["id"], payload=payload - ) - self.message = msg = Message(**res, _client=self._client) else: res = await self._client.edit_interaction_response( - token=self.token, - application_id=str(self.application_id), - data={"type": self.callback.value, "data": payload}, + token=self.token, application_id=str(self.application_id), data=payload ) - if res["flags"] == 64: - log.warning("You can't edit hidden messages.") - else: - await self._client.edit_message(int(self.channel_id), res["id"], payload=payload) + if not res.get("code"): self.message = msg = Message(**res, _client=self._client) + elif res["code"] in {10008, 10015}: + log.warning( + f"You can't edit hidden messages " + f"(Unknown {'interactions' if res['code'] == 10015 else 'message'} reference)." + ) if msg is not None: return msg @@ -395,15 +400,18 @@ async def defer(self, ephemeral: Optional[bool] = False) -> None: :param ephemeral?: Whether the deferred state is hidden or not. :type ephemeral: Optional[bool] """ - self.deferred = True - _ephemeral: int = (1 << 6) if ephemeral else 0 - self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE + if not self.responded: + self.deferred = True + _ephemeral: int = (1 << 6) if ephemeral else 0 + self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE - await self._client.create_interaction_response( - token=self.token, - application_id=int(self.id), - data={"type": self.callback.value, "data": {"flags": _ephemeral}}, - ) + await self._client.create_interaction_response( + token=self.token, + application_id=int(self.id), + data={"type": self.callback.value, "data": {"flags": _ephemeral}}, + ) + + self.responded = True async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: payload = await super().send(content, **kwargs) @@ -414,20 +422,12 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: _payload: dict = {"type": self.callback.value, "data": payload} msg = None - if self.responded or self.deferred: - if self.deferred: - res = await self._client.edit_interaction_response( - data=payload, - token=self.token, - application_id=str(self.application_id), - ) - self.responded = True - else: - res = await self._client._post_followup( - data=payload, - token=self.token, - application_id=str(self.application_id), - ) + if self.responded: + res = await self._client._post_followup( + data=payload, + token=self.token, + application_id=str(self.application_id), + ) self.message = msg = Message(**res, _client=self._client) else: await self._client.create_interaction_response( @@ -435,19 +435,19 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: application_id=int(self.id), data=_payload, ) - __newdata = await self._client.edit_interaction_response( - data={}, - token=self.token, - application_id=str(self.application_id), + + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) ) - if not __newdata.get("code"): + + if not _msg.get("code"): # if sending message fails somehow - msg = Message(**__newdata, _client=self._client) + msg = Message(**_msg, _client=self._client) self.message = msg self.responded = True + if msg is not None: return msg - return Message( **payload, _client=self._client, @@ -531,6 +531,7 @@ class ComponentContext(_Context): async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: payload = await super().edit(content, **kwargs) + msg = None if not self.deferred: self.callback = InteractionCallbackType.UPDATE_MESSAGE @@ -539,20 +540,22 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: token=self.token, application_id=int(self.id), ) - payload = Message(**payload, _client=self._client) - for attr in payload.__slots__: - if getattr(self.message, attr, None) and not getattr(payload, attr, None): - setattr(payload, attr, getattr(self.message, attr)) - payload._json[attr] = self.message._json[attr] - self.message = payload + + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) + ) + + if not _msg.get("code"): + # if sending message fails somehow + msg = Message(**_msg, _client=self._client) + self.message = msg self.responded = True elif self.callback != InteractionCallbackType.DEFERRED_UPDATE_MESSAGE: - res = await self._client._post_followup( + await self._client._post_followup( data=payload, token=self.token, application_id=str(self.application_id), ) - self.message = Message(**res, _client=self._client) else: res = await self._client.edit_interaction_response( data=payload, @@ -560,12 +563,12 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: application_id=str(self.application_id), ) self.responded = True - self.message = Message(**res, _client=self._client) + self.message = msg = Message(**res, _client=self._client) - if self.message is None: - self.message = Message(**payload, _client=self._client) + if msg is not None: + return msg - return self.message + return Message(**payload, _client=self._client) async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: payload = await super().send(content, **kwargs) @@ -576,46 +579,33 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: _payload: dict = {"type": self.callback.value, "data": payload} msg = None - if ( - self.responded - or self.deferred - or self.callback == InteractionCallbackType.DEFERRED_UPDATE_MESSAGE - ): - if self.deferred: - res = await self._client.edit_interaction_response( - data=payload, - token=self.token, - application_id=str(self.application_id), - ) - self.responded = True - else: - res = await self._client._post_followup( - data=payload, - token=self.token, - application_id=str(self.application_id), - ) + if self.responded: + res = await self._client._post_followup( + data=payload, + token=self.token, + application_id=str(self.application_id), + ) self.message = msg = Message(**res, _client=self._client) - else: await self._client.create_interaction_response( token=self.token, application_id=int(self.id), data=_payload, ) - __newdata = await self._client.edit_interaction_response( - data={}, - token=self.token, - application_id=str(self.application_id), + + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) ) - if not __newdata.get("code"): + + if not _msg.get("code"): # if sending message fails somehow - msg = Message(**__newdata, _client=self._client) + msg = Message(**_msg, _client=self._client) self.message = msg self.responded = True if msg is not None: return msg - return Message(**payload) + return Message(**payload, _client=self._client) async def defer( self, ephemeral: Optional[bool] = False, edit_origin: Optional[bool] = False @@ -629,20 +619,24 @@ async def defer( :param edit_origin?: Whether you want to edit the original message or send a followup message :type edit_origin: Optional[bool] """ - self.deferred = True - _ephemeral: int = (1 << 6) if bool(ephemeral) else 0 + if not self.responded: - # ephemeral doesn't change callback typings. just data json - if edit_origin: - self.callback = InteractionCallbackType.DEFERRED_UPDATE_MESSAGE - else: - self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE + self.deferred = True + _ephemeral: int = (1 << 6) if bool(ephemeral) else 0 - await self._client.create_interaction_response( - token=self.token, - application_id=int(self.id), - data={"type": self.callback.value, "data": {"flags": _ephemeral}}, - ) + # ephemeral doesn't change callback typings. just data json + if edit_origin: + self.callback = InteractionCallbackType.DEFERRED_UPDATE_MESSAGE + else: + self.callback = InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE + + await self._client.create_interaction_response( + token=self.token, + application_id=int(self.id), + data={"type": self.callback.value, "data": {"flags": _ephemeral}}, + ) + + self.responded = True @property def custom_id(self) -> Optional[str]: From 76c428a22af822c8eeb9e17624520d510aa05813 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sat, 9 Jul 2022 13:11:17 -0400 Subject: [PATCH 2/4] refactor: Utilise LibraryException --- interactions/client/context.py | 98 ++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/interactions/client/context.py b/interactions/client/context.py index 6bc455145..031019c99 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -357,36 +357,37 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: and self.message.id is not None and self.message.flags != 64 ): - res = await self._client.edit_message( - int(self.channel_id), int(self.message.id), payload=payload - ) + try: + res = await self._client.edit_message( + int(self.channel_id), int(self.message.id), payload=payload + ) + except LibraryException as e: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: + self.message = msg = Message(**res, _client=self._client) else: + try: + res = await self._client.edit_interaction_response( + token=self.token, + application_id=str(self.id), + data=payload, + message_id=self.message.id + if self.message and self.message.flags != 64 + else "@original", + ) + except LibraryException as e: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: + self.message = msg = Message(**res, _client=self._client) + else: + try: res = await self._client.edit_interaction_response( - token=self.token, - application_id=str(self.id), - data=payload, - message_id=self.message.id - if self.message and self.message.flags != 64 - else "@original", + token=self.token, application_id=str(self.application_id), data=payload ) - if not res.get("code"): - self.message = msg = Message(**res, _client=self._client) - elif res["code"] in {10008, 10015}: - log.warning( - f"You can't edit hidden messages " - f"(Unknown {'interactions' if res['code'] == 10015 else 'message'} reference)." - ) - else: - res = await self._client.edit_interaction_response( - token=self.token, application_id=str(self.application_id), data=payload - ) - if not res.get("code"): + except LibraryException as e: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: self.message = msg = Message(**res, _client=self._client) - elif res["code"] in {10008, 10015}: - log.warning( - f"You can't edit hidden messages " - f"(Unknown {'interactions' if res['code'] == 10015 else 'message'} reference)." - ) if msg is not None: return msg @@ -436,14 +437,15 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: data=_payload, ) - _msg = await self._client.get_original_interaction_response( - self.token, str(self.application_id) - ) + try: + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) + ) + except LibraryException: + pass + else: + self.message = msg = Message(**_msg, _client=self._client) - if not _msg.get("code"): - # if sending message fails somehow - msg = Message(**_msg, _client=self._client) - self.message = msg self.responded = True if msg is not None: @@ -541,14 +543,15 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: application_id=int(self.id), ) - _msg = await self._client.get_original_interaction_response( - self.token, str(self.application_id) - ) + try: + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) + ) + except LibraryException: + pass + else: + self.message = msg = Message(**_msg, _client=self._client) - if not _msg.get("code"): - # if sending message fails somehow - msg = Message(**_msg, _client=self._client) - self.message = msg self.responded = True elif self.callback != InteractionCallbackType.DEFERRED_UPDATE_MESSAGE: await self._client._post_followup( @@ -593,14 +596,15 @@ async def send(self, content: Optional[str] = MISSING, **kwargs) -> Message: data=_payload, ) - _msg = await self._client.get_original_interaction_response( - self.token, str(self.application_id) - ) + try: + _msg = await self._client.get_original_interaction_response( + self.token, str(self.application_id) + ) + except LibraryException: + pass + else: + self.message = msg = Message(**_msg, _client=self._client) - if not _msg.get("code"): - # if sending message fails somehow - msg = Message(**_msg, _client=self._client) - self.message = msg self.responded = True if msg is not None: From 125e25a71dcc999236b5c39fe67e975a345432af Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sun, 10 Jul 2022 12:05:26 -0400 Subject: [PATCH 3/4] refactor: Check for non-ephemeral based errors. --- interactions/client/context.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/interactions/client/context.py b/interactions/client/context.py index 031019c99..d5a1b9d22 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -363,6 +363,9 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: ) except LibraryException as e: log.warning(f"You can't edit hidden messages." f"({e.message}).") + if e.code not in {10015, 10018}: + # if its not ephemeral or some other thing. + raise e else: self.message = msg = Message(**res, _client=self._client) else: @@ -377,6 +380,9 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: ) except LibraryException as e: log.warning(f"You can't edit hidden messages." f"({e.message}).") + if e.code not in {10015, 10018}: + # if its not ephemeral or some other thing. + raise e else: self.message = msg = Message(**res, _client=self._client) else: @@ -386,6 +392,9 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: ) except LibraryException as e: log.warning(f"You can't edit hidden messages." f"({e.message}).") + if e.code not in {10015, 10018}: + # if its not ephemeral or some other thing. + raise e else: self.message = msg = Message(**res, _client=self._client) From ceccc3b0dded198f8a7d4f9fe9cdbc5c9146f6f4 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sun, 10 Jul 2022 12:19:28 -0400 Subject: [PATCH 4/4] refactor: Invert e.code logic check. --- interactions/client/context.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/interactions/client/context.py b/interactions/client/context.py index d5a1b9d22..740354adc 100644 --- a/interactions/client/context.py +++ b/interactions/client/context.py @@ -362,10 +362,11 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: int(self.channel_id), int(self.message.id), payload=payload ) except LibraryException as e: - log.warning(f"You can't edit hidden messages." f"({e.message}).") - if e.code not in {10015, 10018}: + if e.code in {10015, 10018}: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: # if its not ephemeral or some other thing. - raise e + raise e from e else: self.message = msg = Message(**res, _client=self._client) else: @@ -379,10 +380,11 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: else "@original", ) except LibraryException as e: - log.warning(f"You can't edit hidden messages." f"({e.message}).") - if e.code not in {10015, 10018}: + if e.code in {10015, 10018}: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: # if its not ephemeral or some other thing. - raise e + raise e from e else: self.message = msg = Message(**res, _client=self._client) else: @@ -391,10 +393,11 @@ async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message: token=self.token, application_id=str(self.application_id), data=payload ) except LibraryException as e: - log.warning(f"You can't edit hidden messages." f"({e.message}).") - if e.code not in {10015, 10018}: + if e.code in {10015, 10018}: + log.warning(f"You can't edit hidden messages." f"({e.message}).") + else: # if its not ephemeral or some other thing. - raise e + raise e from e else: self.message = msg = Message(**res, _client=self._client)