From b3edb31df5500e8e850450577e9541af07a9508b Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 18 Jun 2016 02:09:50 -0400 Subject: [PATCH] Add support for message pinning. This includes `Client.pin_message`, `Client.unpin_message` and `Client.pins_from`. This also adds the `Message.pinned` attribute to the `Message` object. --- discord/client.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ discord/http.py | 12 ++++++++ discord/message.py | 7 +++-- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/discord/client.py b/discord/client.py index d786cca47..039188efe 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1110,6 +1110,75 @@ class Client: data = yield from self.http.get_message(channel.id, id) return Message(channel=channel, **data) + @asyncio.coroutine + def pin_message(self, message): + """|coro| + + Pins a message. You must have Manage Messages permissions + to do this in a non-private channel context. + + Parameters + ----------- + message: :class:`Message` + The message to pin. + + Raises + ------- + Forbidden + You do not have permissions to pin the message. + NotFound + The message or channel was not found. + HTTPException + Pinning the message failed, probably due to the channel + having more than 50 pinned messages. + """ + yield from self.http.pin_message(message.channel.id, message.id) + + @asyncio.coroutine + def unpin_message(self, message): + """|coro| + + Unpins a message. You must have Manage Messages permissions + to do this in a non-private channel context. + + Parameters + ----------- + message: :class:`Message` + The message to unpin. + + Raises + ------- + Forbidden + You do not have permissions to unpin the message. + NotFound + The message or channel was not found. + HTTPException + Unpinning the message failed. + """ + yield from self.http.unpin_message(message.channel.id, message.id) + + @asyncio.coroutine + def pins_from(self, channel): + """|coro| + + Returns a list of :class:`Message` that are currently pinned for + the specified :class:`Channel` or :class:`PrivateChannel`. + + Parameters + ----------- + channel: :class:`Channel` or :class:`PrivateChannel` + The channel to look through pins for. + + Raises + ------- + NotFound + The channel was not found. + HTTPException + Retrieving the pinned messages failed. + """ + + data = yield from self.http.pins_from(channel.id) + return [Message(channel=channel, **m) for m in data] def _logs_from(self, channel, limit=100, before=None, after=None): """|coro| diff --git a/discord/http.py b/discord/http.py index ba531a916..d7e90b880 100644 --- a/discord/http.py +++ b/discord/http.py @@ -278,6 +278,18 @@ class HTTPClient: return self.get(url, params=params, bucket=_func_()) + def pin_message(self, channel_id, message_id): + url = '{0.CHANNELS}/{1}/pins/{2}'.format(self, channel_id, message_id) + return self.put(url, bucket=_func_()) + + def unpin_message(self, channel_id, message_id): + url = '{0.CHANNELS}/{1}/pins/{2}'.format(self, channel_id, message_id) + return self.delete(url, bucket=_func_()) + + def pins_from(self, channel_id): + url = '{0.CHANNELS}/{1}/pins'.format(self, channel_id) + return self.get(url, bucket=_func_()) + # Member management def kick(self, user_id, guild_id): diff --git a/discord/message.py b/discord/message.py index ea7bfd644..93d33f8a9 100644 --- a/discord/message.py +++ b/discord/message.py @@ -90,12 +90,14 @@ class Message: The message ID. attachments : list A list of attachments given to a message. + pinned: bool + Specifies if the message is currently pinned. """ __slots__ = [ 'edited_timestamp', 'timestamp', 'tts', 'content', 'channel', 'mention_everyone', 'embeds', 'id', 'mentions', 'author', 'channel_mentions', 'server', '_raw_mentions', 'attachments', - '_clean_content', '_raw_channel_mentions', 'nonce', + '_clean_content', '_raw_channel_mentions', 'nonce', 'pinned', 'role_mentions', '_raw_role_mentions' ] def __init__(self, **kwargs): @@ -108,7 +110,8 @@ class Message: # sometimes the .%f modifier is missing self.edited_timestamp = utils.parse_time(data.get('edited_timestamp')) self.timestamp = utils.parse_time(data.get('timestamp')) - self.tts = data.get('tts') + self.tts = data.get('tts', False) + self.pinned = data.get('pinned', False) self.content = data.get('content') self.mention_everyone = data.get('mention_everyone') self.embeds = data.get('embeds')