Skip to content

Commit 612657b

Browse files
committed
Use new pins endpoints
1 parent 2685516 commit 612657b

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

discord/abc.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,17 +1710,33 @@ async def fetch_message(self, id: int, /) -> Message:
17101710
data = await self._state.http.get_message(channel.id, id)
17111711
return self._state.create_message(channel=channel, data=data)
17121712

1713-
async def pins(self) -> List[Message]:
1713+
async def pins(self, *, before: Optional[SnowflakeTime] = None, limit: Optional[int] = None) -> List[Message]:
17141714
"""|coro|
17151715
1716-
Retrieves all messages that are currently pinned in the channel.
1716+
Retrieves a maximum of 50 pinned messages from the destination.
1717+
1718+
Requires the :attr:`~discord.Permissions.view_channel` permission.
1719+
1720+
No pins will be returned if the user is missing the
1721+
:attr:`~discord.Permissions.read_message_history` permission.
17171722
17181723
.. note::
17191724
17201725
Due to a limitation with the Discord API, the :class:`.Message`
17211726
objects returned by this method do not contain complete
17221727
:attr:`.Message.reactions` data.
17231728
1729+
Parameters
1730+
-----------
1731+
before: Optional[Union[:class:`~discord.abc.Snowflake`, :class:`datetime.datetime`]]
1732+
Retrieve pinned messages before this date or message.
1733+
If a datetime is provided, it is recommended to use a UTC aware datetime.
1734+
If the datetime is naive, it is assumed to be local time.
1735+
limit: Optional[int]
1736+
The maximum number of pinned messages to retrieve. Defaults to 50.
1737+
1738+
This must be a number between 1 and 50.
1739+
17241740
Raises
17251741
-------
17261742
~discord.Forbidden
@@ -1733,11 +1749,13 @@ async def pins(self) -> List[Message]:
17331749
List[:class:`~discord.Message`]
17341750
The messages that are currently pinned.
17351751
"""
1752+
if isinstance(before, datetime):
1753+
before = Object(id=utils.time_snowflake(before, high=False))
17361754

17371755
channel = await self._get_channel()
17381756
state = self._state
1739-
data = await state.http.pins_from(channel.id)
1740-
return [state.create_message(channel=channel, data=m) for m in data]
1757+
data = await state.http.pins_from(channel.id, before=before.id if before else None, limit=limit)
1758+
return [state.create_message(channel=channel, data=m["message"]) for m in data["items"]]
17411759

17421760
async def history(
17431761
self,

discord/http.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ def publish_message(self, channel_id: Snowflake, message_id: Snowflake) -> Respo
10361036
def pin_message(self, channel_id: Snowflake, message_id: Snowflake, reason: Optional[str] = None) -> Response[None]:
10371037
r = Route(
10381038
'PUT',
1039-
'/channels/{channel_id}/pins/{message_id}',
1039+
'/channels/{channel_id}/messages/pins/{message_id}',
10401040
channel_id=channel_id,
10411041
message_id=message_id,
10421042
)
@@ -1045,14 +1045,25 @@ def pin_message(self, channel_id: Snowflake, message_id: Snowflake, reason: Opti
10451045
def unpin_message(self, channel_id: Snowflake, message_id: Snowflake, reason: Optional[str] = None) -> Response[None]:
10461046
r = Route(
10471047
'DELETE',
1048-
'/channels/{channel_id}/pins/{message_id}',
1048+
'/channels/{channel_id}/messages/pins/{message_id}',
10491049
channel_id=channel_id,
10501050
message_id=message_id,
10511051
)
10521052
return self.request(r, reason=reason)
10531053

1054-
def pins_from(self, channel_id: Snowflake) -> Response[List[message.Message]]:
1055-
return self.request(Route('GET', '/channels/{channel_id}/pins', channel_id=channel_id))
1054+
def pins_from(
1055+
self,
1056+
channel_id: Snowflake,
1057+
before: Optional[Snowflake] = None,
1058+
limit: Optional[int] = None,
1059+
) -> Response[message.ChannelPins]:
1060+
params = {}
1061+
if before is not None:
1062+
params['before'] = before
1063+
if limit is not None:
1064+
params['limit'] = limit
1065+
1066+
return self.request(Route('GET', '/channels/{channel_id}/messages/pins', channel_id=channel_id), params=params)
10561067

10571068
# Member management
10581069

discord/types/message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,13 @@ class AllowedMentions(TypedDict):
237237
roles: SnowflakeList
238238
users: SnowflakeList
239239
replied_user: bool
240+
241+
242+
class MessagePin(TypedDict):
243+
pinned_at: str
244+
message: Message
245+
246+
247+
class ChannelPins(TypedDict):
248+
items: List[MessagePin]
249+
has_more: bool

0 commit comments

Comments
 (0)