Browse Source

Allow TextChannel.delete_messages to take lists of 0 or 1 element.

pull/615/head
Rapptz 8 years ago
parent
commit
64cba11656
  1. 30
      discord/channel.py

30
discord/channel.py

@ -167,11 +167,18 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
Deletes a list of messages. This is similar to :meth:`Message.delete` Deletes a list of messages. This is similar to :meth:`Message.delete`
except it bulk deletes multiple messages. except it bulk deletes multiple messages.
As a special case, if the number of messages is 0, then nothing
is done. If the number of messages is 1 then single message
delete is done. If it's more than two, then bulk delete is used.
You cannot bulk delete more than 100 messages or messages that
are older than 14 days old.
Usable only by bot accounts. Usable only by bot accounts.
Parameters Parameters
----------- -----------
messages: iterable of :class:`Message` messages: Iterable[:class:`abc.Snowflake`]
An iterable of messages denoting which ones to bulk delete. An iterable of messages denoting which ones to bulk delete.
reason: Optional[str] reason: Optional[str]
The reason for bulk deleting these messages. Shows up on the audit log. The reason for bulk deleting these messages. Shows up on the audit log.
@ -179,22 +186,29 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
Raises Raises
------ ------
ClientException ClientException
The number of messages to delete is less than 2 or more than 100. The number of messages to delete more than 100.
Forbidden Forbidden
You do not have proper permissions to delete the messages or You do not have proper permissions to delete the messages or
you're not using a bot account. you're not using a bot account.
HTTPException HTTPException
Deleting the messages failed. Deleting the messages failed.
""" """
if not isinstance(messages, (list, tuple)):
messages = list(messages)
messages = list(messages) if len(messages) == 0:
if len(messages) > 100 or len(messages) < 2: return # do nothing
raise ClientException('Can only delete messages in the range of [2, 100]')
message_ids = [m.id for m in messages] if len(messages) == 1:
channel = yield from self._get_channel() message_id = messages[0].id
yield from self._state.http.delete_message(self.id, message_id, reason=reason)
return
yield from self._state.http.delete_messages(channel.id, message_ids, reason=reason) if len(messages) > 100:
raise ClientException('Can only bulk delete messages up to 100 messages')
message_ids = [m.id for m in messages]
yield from self._state.http.delete_messages(self.id, message_ids, reason=reason)
@asyncio.coroutine @asyncio.coroutine
def purge(self, *, limit=100, check=None, before=None, after=None, around=None, reason=None): def purge(self, *, limit=100, check=None, before=None, after=None, around=None, reason=None):

Loading…
Cancel
Save