Skip to content

Commit c9ddec4

Browse files
committed
feat: add guild_count property
1 parent 5b07e41 commit c9ddec4

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

interactions/client/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,16 @@ def guilds(self) -> List["Guild"]:
534534
"""Returns a list of all guilds the bot is in."""
535535
return self.user.guilds
536536

537+
@property
538+
def guild_count(self) -> int:
539+
"""
540+
Returns the number of guilds the bot is in.
541+
542+
This function is faster than using `len(client.guilds)` as it does not require using the cache.
543+
As such, this is preferred when you only need the count of guilds.
544+
"""
545+
return self.user.guild_count
546+
537547
@property
538548
def status(self) -> Status:
539549
"""
@@ -867,7 +877,9 @@ async def _on_websocket_ready(self, event: events.RawGatewayEvent) -> None:
867877
self._user._add_guilds(expected_guilds)
868878

869879
if not self._startup:
870-
while len(self.guilds) != len(expected_guilds):
880+
while len(self.guilds) != len(
881+
expected_guilds
882+
): # TODO: potentially use self.guild_count instead of len(self.guilds)
871883
try: # wait to let guilds cache
872884
await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout)
873885
except asyncio.TimeoutError:

interactions/ext/debug_extension/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def debug_info(self, ctx: InteractionContext) -> None:
7676

7777
e.add_field("Loaded Exts", ", ".join(self.bot.ext))
7878

79-
e.add_field("Guilds", str(len(self.bot.guilds)))
79+
e.add_field("Guilds", str(self.bot.guild_count))
8080

8181
await ctx.send(embeds=[e])
8282

interactions/models/discord/user.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ def guilds(self) -> List["Guild"]:
239239
"""The guilds the user is in."""
240240
return list(filter(None, (self._client.cache.get_guild(guild_id) for guild_id in self._guild_ids)))
241241

242+
@property
243+
def guild_count(self) -> int:
244+
"""
245+
Returns the number of guilds the bot is in.
246+
247+
This function is faster than using `len(client_user.guilds)` as it does not require using the cache.
248+
As such, this is preferred when you only need the count of guilds.
249+
"""
250+
return len(self._guild_ids or ())
251+
242252
async def edit(
243253
self,
244254
*,

interactions/models/discord/user.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class ClientUser(User):
9595
def _add_guilds(self, guild_ids: Set["Snowflake_Type"]) -> None: ...
9696
@property
9797
def guilds(self) -> List["Guild"]: ...
98+
@property
99+
def guild_count(self) -> int: ...
98100
async def edit(
99101
self,
100102
*,

0 commit comments

Comments
 (0)