Skip to content

Commit 4ac3ffc

Browse files
authored
feat: implement channel.get_history (#758)
* feat: implement channel.get_history helper method * ci: check * check
1 parent 556eeea commit 4ac3ffc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

interactions/api/models/channel.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,56 @@ async def create_invite(
10491049

10501050
return Invite(**res, _client=self._client)
10511051

1052+
async def get_history(self, limit: int = 100) -> List["Message"]: # noqa
1053+
"""
1054+
Gets messages from the channel's history.
1055+
1056+
:param limit?: The amount of messages to get. Default 100
1057+
:type limit?: int
1058+
:return: A list of messages
1059+
:rtype: List[Message]
1060+
"""
1061+
1062+
if not self._client:
1063+
raise AttributeError("HTTPClient not found!")
1064+
1065+
from .message import Message
1066+
1067+
_messages: List[Message] = []
1068+
_before: int = None
1069+
while limit > 100:
1070+
_msgs = [
1071+
Message(**res, _client=self._client)
1072+
for res in await self._client.get_channel_messages(
1073+
channel_id=int(self.id),
1074+
limit=100,
1075+
before=_before,
1076+
)
1077+
]
1078+
limit -= 100
1079+
_before = int(_messages[-1].id)
1080+
1081+
for msg in _msgs:
1082+
if msg in _messages:
1083+
return _messages
1084+
else:
1085+
_messages.append(msg)
1086+
1087+
if limit > 0:
1088+
_msgs = [
1089+
Message(**res, _client=self._client)
1090+
for res in await self._client.get_channel_messages(
1091+
channel_id=int(self.id), limit=limit, before=_before
1092+
)
1093+
]
1094+
for msg in _msgs:
1095+
if msg in _messages:
1096+
return _messages
1097+
else:
1098+
_messages.append(msg)
1099+
1100+
return _messages
1101+
10521102

10531103
class Thread(Channel):
10541104
"""An object representing a thread.

interactions/api/models/channel.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,6 @@ class Channel(DictSerializerMixin):
224224
target_user_id: int = MISSING,
225225
target_application_id: int = MISSING,
226226
) -> Invite: ...
227+
async def get_history(self, limit: int = 100) -> List["Message"]: ...
227228

228229
class Thread(Channel): ...

0 commit comments

Comments
 (0)