Skip to content
20 changes: 17 additions & 3 deletions interactions/api/http/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
4 changes: 2 additions & 2 deletions interactions/api/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 28 additions & 2 deletions interactions/client/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down