Skip to content

Commit

Permalink
feat: fill approximate_member_count and `approximate_presence_count…
Browse files Browse the repository at this point in the history
…` fields when fetching Guilds (#892)
  • Loading branch information
Victorsitou committed Jul 14, 2023
1 parent b38090f commit 945318a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/892.feature.rst
@@ -0,0 +1 @@
Added ``with_counts`` parameter to :meth:`Client.fetch_guild` and :meth:`Client.fetch_guilds`, used to determine whether to include the guild's member count and presence information.
17 changes: 14 additions & 3 deletions disnake/client.py
Expand Up @@ -1731,6 +1731,7 @@ def fetch_guilds(
limit: Optional[int] = 100,
before: Optional[SnowflakeTime] = None,
after: Optional[SnowflakeTime] = None,
with_counts: bool = True,
) -> GuildIterator:
"""Retrieves an :class:`.AsyncIterator` that enables receiving your guilds.
Expand Down Expand Up @@ -1772,6 +1773,11 @@ def fetch_guilds(
Retrieve guilds after this date or object.
If a datetime is provided, it is recommended to use a UTC aware datetime.
If the datetime is naive, it is assumed to be local time.
with_counts: :class:`bool`
Whether to include approximate member and presence counts for the guilds.
Defaults to ``True``.
.. versionadded:: 2.10
Raises
------
Expand All @@ -1783,7 +1789,7 @@ def fetch_guilds(
:class:`.Guild`
The guild with the guild data parsed.
"""
return GuildIterator(self, limit=limit, before=before, after=after)
return GuildIterator(self, limit=limit, before=before, after=after, with_counts=with_counts)

async def fetch_template(self, code: Union[Template, str]) -> Template:
"""|coro|
Expand Down Expand Up @@ -1811,7 +1817,7 @@ async def fetch_template(self, code: Union[Template, str]) -> Template:
data = await self.http.get_template(code)
return Template(data=data, state=self._connection)

async def fetch_guild(self, guild_id: int, /) -> Guild:
async def fetch_guild(self, guild_id: int, /, *, with_counts: bool = True) -> Guild:
"""|coro|
Retrieves a :class:`.Guild` from the given ID.
Expand All @@ -1829,6 +1835,11 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
----------
guild_id: :class:`int`
The ID of the guild to retrieve.
with_counts: :class:`bool`
Whether to include approximate member and presence counts for the guild.
Defaults to ``True``.
.. versionadded:: 2.10
Raises
------
Expand All @@ -1842,7 +1853,7 @@ async def fetch_guild(self, guild_id: int, /) -> Guild:
:class:`.Guild`
The guild from the given ID.
"""
data = await self.http.get_guild(guild_id)
data = await self.http.get_guild(guild_id, with_counts=with_counts)
return Guild(data=data, state=self._connection)

async def fetch_guild_preview(
Expand Down
2 changes: 2 additions & 0 deletions disnake/http.py
Expand Up @@ -1284,9 +1284,11 @@ def get_guilds(
limit: int,
before: Optional[Snowflake] = None,
after: Optional[Snowflake] = None,
with_counts: bool = True,
) -> Response[List[guild.Guild]]:
params: Dict[str, Any] = {
"limit": limit,
"with_counts": int(with_counts),
}

if before:
Expand Down
13 changes: 10 additions & 3 deletions disnake/iterators.py
Expand Up @@ -674,7 +674,9 @@ class GuildIterator(_AsyncIterator["Guild"]):
Object after which all guilds must be.
"""

def __init__(self, bot, limit: Optional[int], before=None, after=None) -> None:
def __init__(
self, bot, limit: Optional[int], before=None, after=None, with_counts: bool = True
) -> None:
if isinstance(before, datetime.datetime):
before = Object(id=time_snowflake(before, high=False))
if isinstance(after, datetime.datetime):
Expand All @@ -684,6 +686,7 @@ def __init__(self, bot, limit: Optional[int], before=None, after=None) -> None:
self.limit = limit
self.before = before
self.after = after
self.with_counts = with_counts

self._filter = None

Expand Down Expand Up @@ -744,7 +747,9 @@ async def _retrieve_guilds(self, retrieve) -> List[Guild]:
async def _retrieve_guilds_before_strategy(self, retrieve):
"""Retrieve guilds using before parameter."""
before = self.before.id if self.before else None
data: List[GuildPayload] = await self.get_guilds(retrieve, before=before)
data: List[GuildPayload] = await self.get_guilds(
retrieve, before=before, with_counts=self.with_counts
)
if len(data):
if self.limit is not None:
self.limit -= retrieve
Expand All @@ -754,7 +759,9 @@ async def _retrieve_guilds_before_strategy(self, retrieve):
async def _retrieve_guilds_after_strategy(self, retrieve):
"""Retrieve guilds using after parameter."""
after = self.after.id if self.after else None
data: List[GuildPayload] = await self.get_guilds(retrieve, after=after)
data: List[GuildPayload] = await self.get_guilds(
retrieve, after=after, with_counts=self.with_counts
)
if len(data):
if self.limit is not None:
self.limit -= retrieve
Expand Down

0 comments on commit 945318a

Please sign in to comment.