diff --git a/interactions/api/http/guild.py b/interactions/api/http/guild.py index c8b752447..6b6705a48 100644 --- a/interactions/api/http/guild.py +++ b/interactions/api/http/guild.py @@ -20,13 +20,27 @@ class GuildRequest: def __init__(self) -> None: pass - async def get_self_guilds(self) -> List[dict]: + async def get_self_guilds( + self, limit: Optional[int] = 200, before: Optional[int] = None, after: Optional[int] = None + ) -> List[dict]: """ Gets all guild objects associated with the current bot user. - :return a list of partial guild objects the current bot user is a part of. + :param limit: Number of guilds to return. Defaults to 200. + :param before: Consider only users before the given Guild ID snowflake. + :param after: Consider only users after the given Guild ID snowflake. + :return: A list of partial guild objects the current bot user is a part of. """ - request = await self._req.request(Route("GET", "/users/@me/guilds")) + + params = {} + if limit is not None: + params["limit"] = limit + if before: + params["before"] = before + if after: + params["after"] = after + + request = await self._req.request(Route("GET", "/users/@me/guilds"), params=params) for guild in request: if guild.get("id"): diff --git a/interactions/api/models/guild.py b/interactions/api/models/guild.py index 681e43243..85017e7f5 100644 --- a/interactions/api/models/guild.py +++ b/interactions/api/models/guild.py @@ -2067,9 +2067,9 @@ async def get_all_bans(self) -> List[Dict[str, User]]: for ban in res: ban["user"] = User(**ban["user"]) - _all.append(res) + _all.extend(res) - return res + return _all async def prune( self, diff --git a/interactions/client/bot.py b/interactions/client/bot.py index ab9ddd130..b58f9a14c 100644 --- a/interactions/client/bot.py +++ b/interactions/client/bot.py @@ -507,13 +507,39 @@ async def wait_until_ready(self) -> None: """ await self._websocket.wait_until_ready() + async def _get_all_guilds(self) -> List[dict]: + """ + Gets all guilds that the bot is present in. + + :return: List of guilds + :rtype: List[dict] + """ + + _after = None + _all: list = [] + + res = await self._http.get_self_guilds(limit=200) + + while len(res) >= 200: + + _all.extend(res) + _after = int(res[-1]["id"]) + + res = await self._http.get_self_guilds( + after=_after, + ) + + _all.extend(res) + + return _all + async def __get_all_commands(self) -> None: # this method is just copied from the sync method # I expect this to be changed in the sync rework # until then this will deliver a cache if sync is off to make autocomplete work bug-free # but even with sync off, we should cache all commands here always - _guilds = await self._http.get_self_guilds() + _guilds = await self._get_all_guilds() _guild_ids = [int(_["id"]) for _ in _guilds] self._scopes.update(_guild_ids) _cmds = await self._http.get_application_commands( @@ -618,7 +644,7 @@ async def __sync(self) -> None: # sourcery no-metrics # sourcery skip: low-code-quality log.debug("starting command sync") - _guilds = await self._http.get_self_guilds() + _guilds = await self._get_all_guilds() _guild_ids = [int(_["id"]) for _ in _guilds] self._scopes.update(_guild_ids) _cmds = await self._http.get_application_commands(