From 1b601097d2a5e5c2a3ab1e7e257472212ee63aec Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 10 May 2016 07:52:22 -0400 Subject: [PATCH] Add Client.delete_messages for bulk delete. --- discord/client.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/discord/client.py b/discord/client.py index f56f84bfc..b90bb8139 100644 --- a/discord/client.py +++ b/discord/client.py @@ -977,6 +977,48 @@ class Client: yield from utils._verify_successful_response(response) 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|