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 all commits
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
You do not have proper permissions to delete the message.
"""
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
17 changes: 16 additions & 1 deletion discord/ext/bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(self, *args, **kwargs):

async def _respond(self, *args, **kwargs) -> Message:
message = await self._get_super("reply")(*args, **kwargs)
if self._original_response_message == None:
if self._original_response_message is None:
self._original_response_message = message
return message

Expand All @@ -155,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.
Expand Down