Skip to content

Commit

Permalink
feat: add guild_count property (#1743)
Browse files Browse the repository at this point in the history
* feat: add guild_count property

* docs: remove incorrect comment

Signed-off-by: Astrea <25420078+AstreaTSS@users.noreply.github.com>

---------

Signed-off-by: Astrea <25420078+AstreaTSS@users.noreply.github.com>
  • Loading branch information
AstreaTSS authored Jan 3, 2025
1 parent 27fee39 commit dea3c79
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions interactions/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
2 changes: 1 addition & 1 deletion interactions/ext/debug_extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
10 changes: 10 additions & 0 deletions interactions/models/discord/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*,
Expand Down
2 changes: 2 additions & 0 deletions interactions/models/discord/user.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*,
Expand Down

0 comments on commit dea3c79

Please sign in to comment.