Browse Source

Add reason param to purge and delete_messages

Co-authored-by: Danny <[email protected]>
pull/7494/head
Vaskel 3 years ago
committed by GitHub
parent
commit
86797dd9ab
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      discord/channel.py

25
discord/channel.py

@ -89,7 +89,7 @@ if TYPE_CHECKING:
from .types.snowflake import SnowflakeList
async def _single_delete_strategy(messages: Iterable[Message]):
async def _single_delete_strategy(messages: Iterable[Message], *, reason: Optional[str] = None):
for m in messages:
await m.delete()
@ -360,7 +360,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
{'topic': self.topic, 'nsfw': self.nsfw, 'rate_limit_per_user': self.slowmode_delay}, name=name, reason=reason
)
async def delete_messages(self, messages: Iterable[Snowflake], /) -> None:
async def delete_messages(self, messages: Iterable[Snowflake], *, reason: Optional[str] = None) -> None:
"""|coro|
Deletes a list of messages. This is similar to :meth:`Message.delete`
@ -380,10 +380,14 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
``messages`` parameter is now positional-only.
The ``reason`` keyword-only parameter was added.
Parameters
-----------
messages: Iterable[:class:`abc.Snowflake`]
An iterable of messages denoting which ones to bulk delete.
reason: Optional[:class:`str`]
The reason for deleting the messages. Shows up on the audit log.
Raises
------
@ -411,7 +415,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
raise ClientException('Can only bulk delete messages up to 100 messages')
message_ids: SnowflakeList = [m.id for m in messages]
await self._state.http.delete_messages(self.id, message_ids)
await self._state.http.delete_messages(self.id, message_ids, reason=reason)
async def purge(
self,
@ -423,6 +427,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
around: Optional[SnowflakeTime] = None,
oldest_first: Optional[bool] = False,
bulk: bool = True,
reason: Optional[str] = None,
) -> List[Message]:
"""|coro|
@ -435,6 +440,10 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
The :attr:`~Permissions.read_message_history` permission is
also needed to retrieve message history.
.. versionchanged:: 2.0
The ``reason`` keyword-only parameter was added.
Examples
---------
@ -466,6 +475,8 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
If ``True``, use bulk delete. Setting this to ``False`` is useful for mass-deleting
a bot's own messages without :attr:`Permissions.manage_messages`. When ``True``, will
fall back to single delete if messages are older than two weeks.
reason: Optional[:class:`str`]
The reason for purging the messages. Shows up on the audit log.
Raises
-------
@ -493,7 +504,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
async for message in iterator:
if count == 100:
to_delete = ret[-100:]
await strategy(to_delete)
await strategy(to_delete, reason=reason)
count = 0
await asyncio.sleep(1)
@ -506,7 +517,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
await ret[-1].delete()
elif count >= 2:
to_delete = ret[-count:]
await strategy(to_delete)
await strategy(to_delete, reason=reason)
count = 0
strategy = _single_delete_strategy
@ -514,11 +525,11 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
count += 1
ret.append(message)
# SOme messages remaining to poll
# Some messages remaining to poll
if count >= 2:
# more than 2 messages -> bulk delete
to_delete = ret[-count:]
await strategy(to_delete)
await strategy(to_delete, reason=reason)
elif count == 1:
# delete a single message
await ret[-1].delete()

Loading…
Cancel
Save