Skip to content

Commit dea3c79

Browse files
authored
feat: add guild_count property (#1743)
* feat: add guild_count property * docs: remove incorrect comment Signed-off-by: Astrea <[email protected]> --------- Signed-off-by: Astrea <[email protected]>
1 parent 27fee39 commit dea3c79

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

interactions/client/client.py

Lines changed: 10 additions & 0 deletions
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
"""

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)