Browse Source

Allow Guild.fetch_members to take in limit=None

Fixes #2473
pull/2504/head
Rapptz 5 years ago
parent
commit
bfaf8ecfdb
  1. 8
      discord/guild.py
  2. 30
      discord/iterators.py

8
discord/guild.py

@ -633,7 +633,7 @@ class Guild(Hashable):
@property
def discovery_splash_url(self):
""":class:`Asset`: Returns the guild's discovery splash asset.
.. versionadded:: 1.3.0
"""
return self.discovery_splash_url_as()
@ -1130,7 +1130,7 @@ class Guild(Hashable):
return [convert(d) for d in data]
def fetch_members(self, *, limit=1, after=None):
def fetch_members(self, *, limit=1000, after=None):
"""|coro|
Retrieves an :class:`.AsyncIterator` that enables receiving the guild's members.
@ -1146,8 +1146,8 @@ class Guild(Hashable):
Parameters
----------
limit: Optional[:class:`int`]
The number of members to retrieve.
Defaults to 1.
The number of members to retrieve. Defaults to 1000.
Pass ``None`` to fetch all members. Note that this is potentially slow.
after: Optional[Union[:class:`.abc.Snowflake`, :class:`datetime.datetime`]]
Retrieve members after this date or object.
If a date is provided it must be a timezone-naive datetime representing UTC time.

30
discord/iterators.py

@ -589,7 +589,7 @@ class GuildIterator(_AsyncIterator):
return data
class MemberIterator(_AsyncIterator):
def __init__(self, guild, limit=1, after=None):
def __init__(self, guild, limit=1000, after=None):
if isinstance(after, datetime.datetime):
after = Object(id=time_snowflake(after, high=True))
@ -611,16 +611,30 @@ class MemberIterator(_AsyncIterator):
except asyncio.QueueEmpty:
raise NoMoreItems()
async def fill_members(self):
if self.limit > 0:
retrieve = self.limit if self.limit <= 1000 else 1000
def _get_retrieve(self):
l = self.limit
if l is None:
r = 1000
elif l <= 1000:
r = l
else:
r = 1000
self.retrieve = r
return r > 0
async def fill_members(self):
if self._get_retrieve():
after = self.after.id if self.after else None
data = await self.get_members(self.guild.id, retrieve, after)
data = await self.get_members(self.guild.id, self.retrieve, after)
if not data:
# no data, terminate
return
if data:
self.limit -= retrieve
self.after = Object(id=int(data[-1]['user']['id']))
if len(data) < 1000:
self.limit = 0 # terminate loop
self.after = Object(id=int(data[-1]['user']['id']))
for element in reversed(data):
await self.members.put(self.create_member(element))

Loading…
Cancel
Save