From 6046af92957d875467c03775f4f9a2d4642df333 Mon Sep 17 00:00:00 2001 From: Victorsitou <67214928+Victorsitou@users.noreply.github.com> Date: Fri, 14 Jul 2023 11:20:42 -0400 Subject: [PATCH] feat: add `with_counts` to `fetch_guild(s)` --- changelog/892.feature.rst | 2 +- disnake/client.py | 13 ++++++++++--- disnake/iterators.py | 13 ++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/changelog/892.feature.rst b/changelog/892.feature.rst index 78f7ff0d79..87123d8fb5 100644 --- a/changelog/892.feature.rst +++ b/changelog/892.feature.rst @@ -1 +1 @@ -Guilds fetched with :meth:`Client.fetch_guilds` now have ``approximate_member_count`` and ``approximate_presence_count`` fields filled. +Added ``with_counts`` field to :meth:`Client.fetch_guild` and :meth:`Client.fetch_guilds`, used to determine whether or not to include the guild's member count and presence information. diff --git a/disnake/client.py b/disnake/client.py index 77c41d12bb..67d84e490b 100644 --- a/disnake/client.py +++ b/disnake/client.py @@ -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. @@ -1772,6 +1773,9 @@ 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``. Raises ------ @@ -1783,7 +1787,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| @@ -1811,7 +1815,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. @@ -1829,6 +1833,9 @@ 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``. Raises ------ @@ -1842,7 +1849,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( diff --git a/disnake/iterators.py b/disnake/iterators.py index ce5c9cd252..583c3bd02f 100644 --- a/disnake/iterators.py +++ b/disnake/iterators.py @@ -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], with_counts: bool, before=None, after=None + ) -> None: if isinstance(before, datetime.datetime): before = Object(id=time_snowflake(before, high=False)) if isinstance(after, datetime.datetime): @@ -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 @@ -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 @@ -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