Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BridgeExtContext.delete() method #1348

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions discord/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved
"""
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]]:
Expand Down
6 changes: 1 addition & 5 deletions discord/ext/bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@ class BridgeExtContext(BridgeContext, Context):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_response_message: Optional[Message] = None
Lulalaby marked this conversation as resolved.
Show resolved Hide resolved

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)
Expand Down
21 changes: 20 additions & 1 deletion discord/ext/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
baronkobama marked this conversation as resolved.
Show resolved Hide resolved
"""|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)