From 945318a099dfbaec89de5ff0332c30a027a7645c Mon Sep 17 00:00:00 2001 From: Victor <67214928+Victorsitou@users.noreply.github.com> Date: Fri, 14 Jul 2023 14:04:47 -0400 Subject: [PATCH] feat: fill `approximate_member_count` and `approximate_presence_count` fields when fetching Guilds (#892) --- changelog/892.feature.rst | 1 + disnake/client.py | 17 ++++++++++++++--- disnake/http.py | 2 ++ disnake/iterators.py | 13 ++++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 changelog/892.feature.rst diff --git a/changelog/892.feature.rst b/changelog/892.feature.rst new file mode 100644 index 0000000000..f6241a6e58 --- /dev/null +++ b/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. diff --git a/disnake/client.py b/disnake/client.py index 77c41d12bb..d5c1d8cf33 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,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 ------ @@ -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| @@ -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. @@ -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 ------ @@ -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( diff --git a/disnake/http.py b/disnake/http.py index 00cd9d3761..f289620a8d 100644 --- a/disnake/http.py +++ b/disnake/http.py @@ -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: diff --git a/disnake/iterators.py b/disnake/iterators.py index ce5c9cd252..ea8347effd 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], 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): @@ -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