Skip to content

Commit 7d7b2f5

Browse files
committed
Add Message.pinned_at and fix Message.pins
1 parent 612657b commit 7d7b2f5

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

discord/abc.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ 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, *, before: Optional[SnowflakeTime] = None, limit: Optional[int] = None) -> List[Message]:
1713+
async def pins(self, *, before: Optional[datetime] = None, limit: Optional[int] = None) -> List[Message]:
17141714
"""|coro|
17151715
17161716
Retrieves a maximum of 50 pinned messages from the destination.
@@ -1728,8 +1728,8 @@ async def pins(self, *, before: Optional[SnowflakeTime] = None, limit: Optional[
17281728
17291729
Parameters
17301730
-----------
1731-
before: Optional[Union[:class:`~discord.abc.Snowflake`, :class:`datetime.datetime`]]
1732-
Retrieve pinned messages before this date or message.
1731+
before: Optional[:class:`datetime.datetime`]
1732+
Retrieve pinned messages before this time.
17331733
If a datetime is provided, it is recommended to use a UTC aware datetime.
17341734
If the datetime is naive, it is assumed to be local time.
17351735
limit: Optional[int]
@@ -1749,13 +1749,17 @@ async def pins(self, *, before: Optional[SnowflakeTime] = None, limit: Optional[
17491749
List[:class:`~discord.Message`]
17501750
The messages that are currently pinned.
17511751
"""
1752-
if isinstance(before, datetime):
1753-
before = Object(id=utils.time_snowflake(before, high=False))
1752+
state = self._state
1753+
if before is not None:
1754+
if not isinstance(before, datetime):
1755+
raise TypeError(f'before must be a datetime object, not {before.__class__!r}')
1756+
if before.tzinfo is None:
1757+
raise TypeError(
1758+
'before must be an aware datetime. Consider using discord.utils.utcnow() or datetime.datetime.now().astimezone() for local time.'
1759+
)
17541760

17551761
channel = await self._get_channel()
1756-
state = self._state
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"]]
1762+
data = await state.http.pins_from(channel.id, before=before.isoformat() if before else None, limit=limit)
17591763

17601764
async def history(
17611765
self,

discord/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ def unpin_message(self, channel_id: Snowflake, message_id: Snowflake, reason: Op
10541054
def pins_from(
10551055
self,
10561056
channel_id: Snowflake,
1057-
before: Optional[Snowflake] = None,
1057+
before: Optional[str] = None,
10581058
limit: Optional[int] = None,
10591059
) -> Response[message.ChannelPins]:
10601060
params = {}

discord/message.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,7 @@ class Message(PartialMessage, Hashable):
21712171
'call',
21722172
'purchase_notification',
21732173
'message_snapshots',
2174+
'_pinned_at',
21742175
)
21752176

21762177
if TYPE_CHECKING:
@@ -2210,6 +2211,8 @@ def __init__(
22102211
self.application_id: Optional[int] = utils._get_as_snowflake(data, 'application_id')
22112212
self.stickers: List[StickerItem] = [StickerItem(data=d, state=state) for d in data.get('sticker_items', [])]
22122213
self.message_snapshots: List[MessageSnapshot] = MessageSnapshot._from_value(state, data.get('message_snapshots'))
2214+
# Set by Messageable.pins
2215+
self._pinned_at: Optional[datetime.datetime] = None
22132216

22142217
self.poll: Optional[Poll] = None
22152218
try:
@@ -2629,6 +2632,18 @@ def thread(self) -> Optional[Thread]:
26292632
if self.guild is not None:
26302633
# Fall back to guild threads in case one was created after the message
26312634
return self._thread or self.guild.get_thread(self.id)
2635+
2636+
@property
2637+
def pinned_at(self) -> Optional[datetime.datetime]:
2638+
"""Optional[:class:`datetime.datetime`]: An aware UTC datetime object containing the time
2639+
when the message was pinned.
2640+
2641+
.. note::
2642+
This is only set for messages that are returned by :meth:`Messageable.pins`.
2643+
2644+
.. versionadded:: 2.6
2645+
"""
2646+
return self._pinned_at
26322647

26332648
@property
26342649
@deprecated('interaction_metadata')

0 commit comments

Comments
 (0)