Skip to content

Commit ad7f451

Browse files
authored
feat: implement get_all_members helper methods (#675)
1 parent 4bc73c2 commit ad7f451

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

interactions/api/models/channel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ async def purge(
702702
) -> List["Message"]: # noqa
703703
"""
704704
Purges a given amount of messages from a channel. You can specify a check function to exclude specific messages.
705+
706+
.. warning:: Calling this method can lead to rate-limits when purging higher amounts of messages.
707+
705708
.. code-block:: python
706709
def check_pinned(message):
707710
return not message.pinned # This returns `True` only if the message is the message is not pinned

interactions/api/models/guild.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ async def create_channel(
699699
:param topic?: The topic of that channel
700700
:type topic: Optional[str]
701701
:param bitrate?: (voice channel only) The bitrate (in bits) of the voice channel
702-
:type bitrate Optional[int]
702+
:type bitrate: Optional[int]
703703
:param user_limit?: (voice channel only) Maximum amount of users in the channel
704704
:type user_limit: Optional[int]
705705
:param rate_limit_per_use?: Amount of seconds a user has to wait before sending another message (0-21600)
@@ -797,7 +797,7 @@ async def modify_channel(
797797
:param topic?: The topic of that channel, defaults to the current value of the channel
798798
:type topic: Optional[str]
799799
:param bitrate?: (voice channel only) The bitrate (in bits) of the voice channel, defaults to the current value of the channel
800-
:type bitrate Optional[int]
800+
:type bitrate: Optional[int]
801801
:param user_limit?: (voice channel only) Maximum amount of users in the channel, defaults to the current value of the channel
802802
:type user_limit: Optional[int]
803803
:param rate_limit_per_use?: Amount of seconds a user has to wait before sending another message (0-21600), defaults to the current value of the channel
@@ -1626,6 +1626,34 @@ async def search_members(self, query: str, limit: Optional[int] = 1) -> List[Mem
16261626
)
16271627
return [Member(**member, _client=self._client) for member in res]
16281628

1629+
async def get_all_members(self) -> List[Member]:
1630+
"""
1631+
Gets all members of a guild.
1632+
1633+
.. warning:: Calling this method can lead to rate-limits in larger guilds.
1634+
1635+
:return: Returns a list of all members of the guild
1636+
:rtype: List[Member]
1637+
"""
1638+
if not self._client:
1639+
raise AttributeError("HTTPClient not found!")
1640+
1641+
_all_members: List[dict] = []
1642+
_last_member: Member
1643+
_members: List[dict] = await self._client.get_list_of_members(
1644+
guild_id=int(self.id), limit=100
1645+
)
1646+
if len(_members) == 100:
1647+
while len(_members) >= 100:
1648+
_all_members.extend(_members)
1649+
_last_member = Member(**_members[-1])
1650+
_members = await self._client.get_list_of_members(
1651+
guild_id=int(self.id), limit=100, after=int(_last_member.id)
1652+
)
1653+
_all_members.extend(_members)
1654+
1655+
return [Member(**_, _client=self._client) for _ in _all_members]
1656+
16291657

16301658
class GuildPreview(DictSerializerMixin):
16311659
"""

interactions/api/models/guild.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ class Guild(DictSerializerMixin):
412412
query: str,
413413
limit: Optional[int] = 1
414414
) -> List[Member]: ...
415+
async def get_all_members(self) -> List[Member]: ...
415416

416417
class GuildPreview(DictSerializerMixin):
417418
_json: dict

0 commit comments

Comments
 (0)