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
37 changes: 35 additions & 2 deletions interactions/api/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from .webhook import Webhook

if TYPE_CHECKING:
from ...client.bot import Client
from ..http.client import HTTPClient
from .gw import AutoModerationRule, VoiceState
from .message import Message
Expand Down Expand Up @@ -2067,9 +2068,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 Expand Up @@ -3036,6 +3037,38 @@ def discovery_splash_url(self) -> Optional[str]:
else None
)

@classmethod
async def get_all_guilds(cls, client: "Client") -> List["Guild"]:
"""
.. versionadded:: 4.4.0

Gets all guilds that the bot is present in.

:param Client client: The bot instance
:return: List of guilds
:rtype: List[Guild]
"""

_after = None
_all: list = []

res: list = await client._http.get_self_guilds(limit=200)

while len(res) >= 1000:

for guild in res:
_all.append(Guild(**guild))
_after = int(res[-1]["id"])

res = await client._http.get_self_guilds(
after=_after,
)

for guild in res:
_all.append(Guild(**guild))

return _all


@define()
class GuildPreview(DictSerializerMixin, IDMixin):
Expand Down
8 changes: 4 additions & 4 deletions interactions/client/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ async def __get_all_commands(self) -> None:
# 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()
_guild_ids = [int(_["id"]) for _ in _guilds]
_guilds = await Guild.get_all_guilds(self)
_guild_ids = [int(_.id) for _ in _guilds]
self._scopes.update(_guild_ids)
_cmds = await self._http.get_application_commands(
application_id=self.me.id, with_localizations=True
Expand Down Expand Up @@ -618,8 +618,8 @@ 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()
_guild_ids = [int(_["id"]) for _ in _guilds]
_guilds = await Guild.get_all_guilds(self)
_guild_ids = [int(_.id) for _ in _guilds]
self._scopes.update(_guild_ids)
_cmds = await self._http.get_application_commands(
application_id=self.me.id, with_localizations=True
Expand Down