diff options
| author | Rapptz <[email protected]> | 2016-05-10 07:52:22 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2016-05-10 07:52:22 -0400 |
| commit | 1b601097d2a5e5c2a3ab1e7e257472212ee63aec (patch) | |
| tree | 080bf027cffca902619b99365e50c8e05817e00a | |
| parent | Fix issue with Member.joined_at being None. (diff) | |
| download | discord.py-1b601097d2a5e5c2a3ab1e7e257472212ee63aec.tar.xz discord.py-1b601097d2a5e5c2a3ab1e7e257472212ee63aec.zip | |
Add Client.delete_messages for bulk delete.
| -rw-r--r-- | discord/client.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/discord/client.py b/discord/client.py index f56f84bf..b90bb813 100644 --- a/discord/client.py +++ b/discord/client.py @@ -978,6 +978,48 @@ class Client: yield from response.release() @asyncio.coroutine + def delete_messages(self, messages): + """|coro| + + Deletes a list of messages. This is similar to :func:`delete_message` + except it bulk deletes multiple messages. + + The channel to check where the message is deleted from is handled via + the first element of the iterable's ``.channel.id`` attributes. If the + channel is not consistent throughout the entire sequence, then an + :exc:`HTTPException` will be raised. + + Parameters + ----------- + messages : iterable of :class:`Message` + An iterable of messages denoting which ones to bulk delete. + + Raises + ------ + ClientException + The number of messages to delete is less than 2 or more than 100. + Forbidden + You do not have proper permissions to delete the messages. + HTTPException + Deleting the messages failed. + """ + + messages = list(messages) + if len(messages) > 100 or len(messages) < 2: + raise ClientException('Can only delete messages in the range of [2, 100]') + + channel_id = messages[0].channel.id + url = '{0}/{1}/messages/bulk_delete'.format(endpoints.CHANNELS, channel_id) + payload = { + 'messages': [m.id for m in messages] + } + + response = yield from self.session.post(url, headers=self.headers, data=utils.to_json(payload)) + log.debug(request_logging_format.format(method='POST', response=response)) + yield from utils._verify_successful_response(response) + yield from response.release() + + @asyncio.coroutine def edit_message(self, message, new_content): """|coro| |