Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement get_all_members helper methods #675

Merged
merged 3 commits into from Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions interactions/api/models/channel.py
Expand Up @@ -618,6 +618,9 @@ async def purge(
) -> List["Message"]: # noqa
"""
Purges a given amount of messages from a channel. You can specify a check function to exclude specific messages.

.. warning:: Calling this method can lead to rate-limits when purging higher amounts of messages.

.. code-block:: python
def check_pinned(message):
return not message.pinned # This returns `True` only if the message is the message is not pinned
Expand Down
32 changes: 30 additions & 2 deletions interactions/api/models/guild.py
Expand Up @@ -699,7 +699,7 @@ async def create_channel(
:param topic?: The topic of that channel
:type topic: Optional[str]
:param bitrate?: (voice channel only) The bitrate (in bits) of the voice channel
:type bitrate Optional[int]
:type bitrate: Optional[int]
:param user_limit?: (voice channel only) Maximum amount of users in the channel
:type user_limit: Optional[int]
:param rate_limit_per_use?: Amount of seconds a user has to wait before sending another message (0-21600)
Expand Down Expand Up @@ -791,7 +791,7 @@ async def modify_channel(
:param topic?: The topic of that channel, defaults to the current value of the channel
:type topic: Optional[str]
:param bitrate?: (voice channel only) The bitrate (in bits) of the voice channel, defaults to the current value of the channel
:type bitrate Optional[int]
:type bitrate: Optional[int]
:param user_limit?: (voice channel only) Maximum amount of users in the channel, defaults to the current value of the channel
:type user_limit: Optional[int]
: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
Expand Down Expand Up @@ -1600,6 +1600,34 @@ async def search_members(self, query: str, limit: Optional[int] = 1) -> List[Mem
)
return [Member(**member, _client=self._client) for member in res]

async def get_all_members(self) -> List[Member]:
"""
Gets all members of a guild.

.. warning:: Calling this method can lead to rate-limits in larger guilds.

:return: Returns a list of all members of the guild
:rtype: List[Member]
"""
if not self._client:
raise AttributeError("HTTPClient not found!")

_all_members: List[dict] = []
_last_member: Member
_members: List[dict] = await self._client.get_list_of_members(
guild_id=int(self.id), limit=100
)
if len(_members) == 100:
while len(_members) >= 100:
_all_members.extend(_members)
_last_member = Member(**_members[-1])
_members = await self._client.get_list_of_members(
guild_id=int(self.id), limit=100, after=int(_last_member.id)
)
_all_members.extend(_members)

return [Member(**_, _client=self._client) for _ in _all_members]


class GuildPreview(DictSerializerMixin):
"""
Expand Down
1 change: 1 addition & 0 deletions interactions/api/models/guild.pyi
Expand Up @@ -409,6 +409,7 @@ class Guild(DictSerializerMixin):
query: str,
limit: Optional[int] = 1
) -> List[Member]: ...
async def get_all_members(self) -> List[Member]: ...

class GuildPreview(DictSerializerMixin):
_json: dict
Expand Down