add support for delete_after kwarg for interaction and webhook messages #79

Open
ethanolchik wants to merge 4 commits from ethanolchik/2.0 into 2.0
3 changed files with 62 additions and 11 deletions

View File

@ -465,7 +465,6 @@ class Context(discord.abc.Messageable, Generic[BotT]):
kwargs.pop("nonce", None)
kwargs.pop("stickers", None)
kwargs.pop("reference", None)
kwargs.pop("delete_after", None)
kwargs.pop("mention_author", None)
if not (

View File

@ -337,7 +337,7 @@ class Interaction:
self._state.store_view(view, message.id)
return message
async def delete_original_message(self) -> None:
async def delete_original_message(self, delay: Optional[float] = None) -> None:
"""|coro|
Deletes the original interaction response message.
@ -345,6 +345,14 @@ class Interaction:
This is a lower level interface to :meth:`InteractionMessage.delete` in case
you do not want to fetch the message and save an HTTP request.
Parameters
------------
delay: Optional[:class:`float`]
If provided, the number of seconds to wait before deleting the message.
The waiting is done in the background and deletion failures are ignored.
.. versionadded:: 2.0
Raises
-------
HTTPException
@ -352,6 +360,8 @@ class Interaction:
Forbidden
Deleted a message that is not yours.
"""
if delay is not None:
await asyncio.sleep(delay)
adapter = async_context.get()
Gnome-py commented 2021-10-01 16:06:38 +00:00 (Migrated from github.com)
Review

This is not done in the background as the docs say, please make it do.

This is not done in the background as the docs say, please make it do.
await adapter.delete_original_interaction_response(
self.application_id,
@ -460,6 +470,7 @@ class InteractionResponse:
view: View = MISSING,
tts: bool = False,
ephemeral: bool = False,
delete_after: Optional[float] = None,
) -> None:
"""|coro|
@ -483,6 +494,9 @@ class InteractionResponse:
Indicates if the message should only be visible to the user who started the interaction.
If a view is sent with an ephemeral message and it has no timeout set then the timeout
is set to 15 minutes.
delete_after: Optional[:class:`float`]
The amount of seconds the bot should wait before deleting the message sent.
.. versionadded:: 2.0
Raises
-------
@ -539,6 +553,8 @@ class InteractionResponse:
self._parent._state.store_view(view)
self.responded_at = utils.utcnow()
if delete_after is not None:
self._parent.delete_original_message(delay=delete_after)
async def edit_message(
self,
@ -733,7 +749,7 @@ class InteractionMessage(Message):
allowed_mentions=allowed_mentions,
)
async def delete(self, *, delay: Optional[float] = None) -> None:
async def delete(self, *, delay: Optional[float] = None, silent: bool = False) -> None:
"""|coro|
Deletes the message.
@ -743,6 +759,12 @@ class InteractionMessage(Message):
delay: Optional[:class:`float`]
If provided, the number of seconds to wait before deleting the message.
The waiting is done in the background and deletion failures are ignored.
silent: :class:`bool`
If silent is set to ``True``, the error will not be raised, it will be ignored.
This defaults to ``False`
.. versionadded:: 2.0
Raises
------
@ -757,12 +779,15 @@ class InteractionMessage(Message):
if delay is not None:
async def inner_call(delay: float = delay):
await asyncio.sleep(delay)
try:
await self._state._interaction.delete_original_message()
await self._state._interaction.delete_original_message(delay=delay)
except HTTPException:
pass
asyncio.create_task(inner_call())
else:
await self._state._interaction.delete_original_message()
try:
await self._state._interaction.delete_original_message(delay=delay)
except Exception:
if not silent:
raise

View File

@ -717,7 +717,7 @@ class WebhookMessage(Message):
allowed_mentions=allowed_mentions,
)
async def delete(self, *, delay: Optional[float] = None) -> None:
async def delete(self, *, delay: Optional[float] = None, silent: bool = False) -> None:
"""|coro|
Deletes the message.
@ -727,6 +727,12 @@ class WebhookMessage(Message):
delay: Optional[:class:`float`]
If provided, the number of seconds to wait before deleting the message.
The waiting is done in the background and deletion failures are ignored.
silent: :class:`bool`
If silent is set to ``True``, the error will not be raised, it will be ignored.
This defaults to ``False`
.. versionadded:: 2.0
Raises
------
@ -741,15 +747,18 @@ class WebhookMessage(Message):
if delay is not None:
async def inner_call(delay: float = delay):
await asyncio.sleep(delay)
try:
await self._state._webhook.delete_message(self.id)
await self._state._webhook.delete_message(self.id, delay)
except HTTPException:
pass
asyncio.create_task(inner_call())
else:
await self._state._webhook.delete_message(self.id)
try:
await self._state._webhook.delete_message(self.id, delay)
except Exception:
if not silent:
raise
class BaseWebhook(Hashable):
@ -1270,6 +1279,7 @@ class Webhook(BaseWebhook):
view: View = MISSING,
thread: Snowflake = MISSING,
wait: bool = False,
delete_after: Optional[float] = None,
) -> Optional[WebhookMessage]:
"""|coro|
@ -1335,6 +1345,11 @@ class Webhook(BaseWebhook):
The thread to send this webhook to.
.. versionadded:: 2.0
delete_after: Optional[:class:`float`]
If provided, the number of seconds to wait before deleting the message.
The waiting is done in the background and deletion failures are ignored.
.. versionadded:: 2.0
Raises
--------
@ -1416,6 +1431,9 @@ class Webhook(BaseWebhook):
if view is not MISSING and not view.is_finished():
message_id = None if msg is None else msg.id
self._state.store_view(view, message_id)
if delete_after is not None and msg is not None:
await msg.delete(delay=delete_after)
return msg
@ -1570,7 +1588,7 @@ class Webhook(BaseWebhook):
self._state.store_view(view, message_id)
return message
async def delete_message(self, message_id: int, /) -> None:
async def delete_message(self, message_id: int, delay: Optional[float] = None, /) -> None:
"""|coro|
Deletes a message owned by this webhook.
@ -1584,6 +1602,12 @@ class Webhook(BaseWebhook):
------------
message_id: :class:`int`
The message ID to delete.
delay: Optional[:class:`float`]
If provided, the number of seconds to wait before deleting the message.
The waiting is done in the background and deletion failures are ignored.
.. versionadded:: 2.0
Raises
-------
@ -1595,6 +1619,9 @@ class Webhook(BaseWebhook):
if self.token is None:
raise InvalidArgument("This webhook does not have a token associated with it")
if delay is not None:
await asyncio.sleep(delay)
adapter = async_context.get()
await adapter.delete_webhook_message(
self.id,