From f6a47eab2b2f21bae2cf1ebfefe395e887bb4066 Mon Sep 17 00:00:00 2001 From: baronkobama Date: Wed, 11 May 2022 21:36:07 -0500 Subject: [PATCH 1/3] Update delete methods --- discord/commands/context.py | 24 ++++++++++++++++++++---- discord/ext/bridge/context.py | 6 +----- discord/ext/commands/context.py | 21 ++++++++++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index dbc40d1043..b745b883e9 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -256,13 +256,29 @@ def defer(self) -> Callable[..., Awaitable[None]]: def followup(self) -> Webhook: return self.interaction.followup - async def delete(self): - """Calls :attr:`~discord.commands.ApplicationContext.respond`. - If the response is done, then calls :attr:`~discord.commands.ApplicationContext.respond` first.""" + async def delete(self, *, delay: Optional[float] = None) -> None: + """|coro| + + Deletes the original interaction response message. + + This is a higher level interface to :meth:`Interaction.delete_original_message`. + + Parameters + ----------- + delay: Optional[:class:`float`] + If provided, the number of seconds to wait before deleting the message. + + Raises + ------- + HTTPException + Deleting the message failed. + Forbidden + Deleted a message that is not yours. + """ if not self.interaction.response.is_done(): await self.defer() - return await self.interaction.delete_original_message() + return await self.interaction.delete_original_message(delay=delay) @property def edit(self) -> Callable[..., Awaitable[InteractionMessage]]: diff --git a/discord/ext/bridge/context.py b/discord/ext/bridge/context.py index f65f2309f4..69f221e8d9 100644 --- a/discord/ext/bridge/context.py +++ b/discord/ext/bridge/context.py @@ -141,13 +141,9 @@ class BridgeExtContext(BridgeContext, Context): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self._original_response_message: Optional[Message] = None async def _respond(self, *args, **kwargs) -> Message: - message = await self._get_super("reply")(*args, **kwargs) - if self._original_response_message == None: - self._original_response_message = message - return message + return await self._get_super("reply")(*args, **kwargs) async def _defer(self, *args, **kwargs) -> None: return await self._get_super("trigger_typing")(*args, **kwargs) diff --git a/discord/ext/commands/context.py b/discord/ext/commands/context.py index de2097f3f3..aac3ab0b85 100644 --- a/discord/ext/commands/context.py +++ b/discord/ext/commands/context.py @@ -149,6 +149,7 @@ def __init__( self.subcommand_passed: Optional[str] = subcommand_passed self.command_failed: bool = command_failed self.current_parameter: Optional[inspect.Parameter] = current_parameter + self._original_response_message: Optional[Message] = None self._state: ConnectionState = self.message._state async def invoke(self, command: Command[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs) -> T: @@ -396,4 +397,22 @@ async def send_help(self, *args: Any) -> Any: @discord.utils.copy_doc(Message.reply) async def reply(self, content: Optional[str] = None, **kwargs: Any) -> Message: - return await self.message.reply(content, **kwargs) + msg = await self.message.reply(content, **kwargs) + if self._original_response_message is None: + self._original_response_message = msg + return msg + + async def delete(self, *, delay: Optional[float] = None, reason: Optional[str] = None) -> None: + """|coro| + + Deletes the original response message, if it exists. + + Parameters + ----------- + delay: Optional[:class:`float`] + If provided, the number of seconds to wait before deleting the message. + reason: Optional[:class:`str`] + The reason for deleting the message. Shows up on the audit log. + """ + if self._original_response_message: + await self._original_response_message.delete(delay=delay, reason=reason) From ead00ea6b97d899e34da04bbc4f2ce7f5579067b Mon Sep 17 00:00:00 2001 From: baronkobama Date: Thu, 12 May 2022 20:04:15 -0500 Subject: [PATCH 2/3] Consolidate into ext.bridge.context --- discord/ext/bridge/context.py | 21 ++++++++++++++++++++- discord/ext/commands/context.py | 21 +-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/discord/ext/bridge/context.py b/discord/ext/bridge/context.py index 69f221e8d9..9d17316ec2 100644 --- a/discord/ext/bridge/context.py +++ b/discord/ext/bridge/context.py @@ -141,9 +141,13 @@ class BridgeExtContext(BridgeContext, Context): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self._original_response_message: Optional[Message] = None async def _respond(self, *args, **kwargs) -> Message: - return await self._get_super("reply")(*args, **kwargs) + message = await self._get_super("reply")(*args, **kwargs) + if self._original_response_message is None: + self._original_response_message = message + return message async def _defer(self, *args, **kwargs) -> None: return await self._get_super("trigger_typing")(*args, **kwargs) @@ -151,6 +155,21 @@ async def _defer(self, *args, **kwargs) -> None: async def _edit(self, *args, **kwargs) -> Message: return await self._original_response_message.edit(*args, **kwargs) + async def delete(self, *, delay: Optional[float] = None, reason: Optional[str] = None) -> None: + """|coro| + + Deletes the original response message, if it exists. + + Parameters + ----------- + delay: Optional[:class:`float`] + If provided, the number of seconds to wait before deleting the message. + reason: Optional[:class:`str`] + The reason for deleting the message. Shows up on the audit log. + """ + if self._original_response_message: + await self._original_response_message.delete(delay=delay, reason=reason) + if TYPE_CHECKING: # This is a workaround for mypy not being able to resolve the type of BridgeCommand. diff --git a/discord/ext/commands/context.py b/discord/ext/commands/context.py index aac3ab0b85..de2097f3f3 100644 --- a/discord/ext/commands/context.py +++ b/discord/ext/commands/context.py @@ -149,7 +149,6 @@ def __init__( self.subcommand_passed: Optional[str] = subcommand_passed self.command_failed: bool = command_failed self.current_parameter: Optional[inspect.Parameter] = current_parameter - self._original_response_message: Optional[Message] = None self._state: ConnectionState = self.message._state async def invoke(self, command: Command[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs) -> T: @@ -397,22 +396,4 @@ async def send_help(self, *args: Any) -> Any: @discord.utils.copy_doc(Message.reply) async def reply(self, content: Optional[str] = None, **kwargs: Any) -> Message: - msg = await self.message.reply(content, **kwargs) - if self._original_response_message is None: - self._original_response_message = msg - return msg - - async def delete(self, *, delay: Optional[float] = None, reason: Optional[str] = None) -> None: - """|coro| - - Deletes the original response message, if it exists. - - Parameters - ----------- - delay: Optional[:class:`float`] - If provided, the number of seconds to wait before deleting the message. - reason: Optional[:class:`str`] - The reason for deleting the message. Shows up on the audit log. - """ - if self._original_response_message: - await self._original_response_message.delete(delay=delay, reason=reason) + return await self.message.reply(content, **kwargs) From 1508cdcff0f38f23286f03e332d28356468b1f05 Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 13 May 2022 18:32:37 +0300 Subject: [PATCH 3/3] Update discord/commands/context.py --- discord/commands/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index b745b883e9..11150b3d1b 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -273,7 +273,7 @@ async def delete(self, *, delay: Optional[float] = None) -> None: HTTPException Deleting the message failed. Forbidden - Deleted a message that is not yours. + You do not have proper permissions to delete the message. """ if not self.interaction.response.is_done(): await self.defer()