Skip to content

Commit

Permalink
feat: add guild_count property
Browse files Browse the repository at this point in the history
  • Loading branch information
AstreaTSS committed Nov 14, 2024
1 parent 5b07e41 commit c9ddec4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion 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 Expand Up @@ -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:
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 c9ddec4

Please sign in to comment.