Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
✨ Adding message history (#380)
Browse files Browse the repository at this point in the history
* ✨ Adding message history

* Update pincer/objects/guild/channel.py

Co-authored-by: Endercheif <45527309+Endercheif@users.noreply.github.com>

* Update pincer/objects/guild/channel.py

Co-authored-by: Endercheif <45527309+Endercheif@users.noreply.github.com>

* Update pincer/objects/guild/channel.py

Co-authored-by: Endercheif <45527309+Endercheif@users.noreply.github.com>

* ♻️ getting id from raw_messages[-1]

* 📝 Added typing to the docstring

Co-authored-by: Endercheif <45527309+Endercheif@users.noreply.github.com>
  • Loading branch information
Sigmanificient and Enderchief committed Jan 13, 2022
1 parent c1cff9e commit 5ca1ebd
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pincer/objects/guild/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,57 @@ async def fetch_message(self, message_id: int) -> UserMessage:
await self._http.get(f"channels/{self.id}/messages/{message_id}")
)

async def history(
self, limit: int = 50,
before: Optional[Union[int, Snowflake]] = None,
after: Optional[Union[int, Snowflake]] = None,
around: Optional[Union[int, Snowflake]] = None,
) -> AsyncIterator[UserMessage]:
"""|coro|
Returns a list of messages in this channel.
Parameters
----------
around : Optional[Union[:class:`int`, :class:`Snowflake`]]
The message ID to look around.
after : Optional[Union[:class:`int`, :class:`Snowflake`]]
The message ID to look after.
before : Optional[Union[:class:`int`, :class:`Snowflake`]]
The message ID to look before.
limit : Optional[Union[:class:`int`, :class:`Snowflake`]]
The maximum number of messages to return.
Returns
-------
AsyncIterator[:class:`~pincer.objects.message.user_message.UserMessage`]
An iterator of messages.
"""

if limit is None:
limit = float('inf')

while limit > 0:
retrieve = min(limit, 100)

raw_messages = await self._http.get(
f'/channels/{self.id}/messages',
params={
'limit': retrieve,
'before': before,
'after': after,
'around': around,
}
)

if not raw_messages:
break

for _message in raw_messages:
yield UserMessage.from_dict(_message)

before = raw_messages[-1]['id']
limit -= retrieve


class VoiceChannel(Channel):
"""A subclass of ``Channel`` for voice channels with all the same attributes."""
Expand Down

0 comments on commit 5ca1ebd

Please sign in to comment.