From c9ddec4bd18f4b09fec7c8a7b69e9e699392dea1 Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:31:11 -0500 Subject: [PATCH 1/2] feat: add guild_count property --- interactions/client/client.py | 14 +++++++++++++- interactions/ext/debug_extension/__init__.py | 2 +- interactions/models/discord/user.py | 10 ++++++++++ interactions/models/discord/user.pyi | 2 ++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/interactions/client/client.py b/interactions/client/client.py index 3c4fed523..5eef5b9cf 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -534,6 +534,16 @@ def guilds(self) -> List["Guild"]: """Returns a list of all guilds the bot is in.""" return self.user.guilds + @property + def guild_count(self) -> int: + """ + Returns the number of guilds the bot is in. + + This function is faster than using `len(client.guilds)` as it does not require using the cache. + As such, this is preferred when you only need the count of guilds. + """ + return self.user.guild_count + @property def status(self) -> Status: """ @@ -867,7 +877,9 @@ async def _on_websocket_ready(self, event: events.RawGatewayEvent) -> None: self._user._add_guilds(expected_guilds) if not self._startup: - while len(self.guilds) != len(expected_guilds): + while len(self.guilds) != len( + expected_guilds + ): # TODO: potentially use self.guild_count instead of len(self.guilds) try: # wait to let guilds cache await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout) except asyncio.TimeoutError: diff --git a/interactions/ext/debug_extension/__init__.py b/interactions/ext/debug_extension/__init__.py index b018448a8..d05050b90 100644 --- a/interactions/ext/debug_extension/__init__.py +++ b/interactions/ext/debug_extension/__init__.py @@ -76,7 +76,7 @@ async def debug_info(self, ctx: InteractionContext) -> None: e.add_field("Loaded Exts", ", ".join(self.bot.ext)) - e.add_field("Guilds", str(len(self.bot.guilds))) + e.add_field("Guilds", str(self.bot.guild_count)) await ctx.send(embeds=[e]) diff --git a/interactions/models/discord/user.py b/interactions/models/discord/user.py index bfe62efeb..0ac24ba35 100644 --- a/interactions/models/discord/user.py +++ b/interactions/models/discord/user.py @@ -239,6 +239,16 @@ def guilds(self) -> List["Guild"]: """The guilds the user is in.""" return list(filter(None, (self._client.cache.get_guild(guild_id) for guild_id in self._guild_ids))) + @property + def guild_count(self) -> int: + """ + Returns the number of guilds the bot is in. + + This function is faster than using `len(client_user.guilds)` as it does not require using the cache. + As such, this is preferred when you only need the count of guilds. + """ + return len(self._guild_ids or ()) + async def edit( self, *, diff --git a/interactions/models/discord/user.pyi b/interactions/models/discord/user.pyi index f84af990d..c0dfdece9 100644 --- a/interactions/models/discord/user.pyi +++ b/interactions/models/discord/user.pyi @@ -95,6 +95,8 @@ class ClientUser(User): def _add_guilds(self, guild_ids: Set["Snowflake_Type"]) -> None: ... @property def guilds(self) -> List["Guild"]: ... + @property + def guild_count(self) -> int: ... async def edit( self, *, From d9550f2ac5bba8afaec5563f306f418024db3f10 Mon Sep 17 00:00:00 2001 From: Astrea <25420078+AstreaTSS@users.noreply.github.com> Date: Thu, 2 Jan 2025 21:24:03 -0500 Subject: [PATCH 2/2] docs: remove incorrect comment Signed-off-by: Astrea <25420078+AstreaTSS@users.noreply.github.com> --- interactions/client/client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/interactions/client/client.py b/interactions/client/client.py index 5eef5b9cf..0407fc69c 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -877,9 +877,7 @@ async def _on_websocket_ready(self, event: events.RawGatewayEvent) -> None: self._user._add_guilds(expected_guilds) if not self._startup: - while len(self.guilds) != len( - expected_guilds - ): # TODO: potentially use self.guild_count instead of len(self.guilds) + while len(self.guilds) != len(expected_guilds): try: # wait to let guilds cache await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout) except asyncio.TimeoutError: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy