Skip to content
28 changes: 27 additions & 1 deletion interactions/api/http/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,40 @@ async def remove_guild_member(
Route("DELETE", f"/guilds/{guild_id}/members/{user_id}"), reason=reason
)

async def begin_guild_prune(
self,
guild_id: int,
days: int = 7,
compute_prune_count: bool = True,
include_roles: Optional[List[int]] = None,
) -> dict:
"""
Begins a prune operation.

:param guild_id: Guild ID snowflake
:param days: Number of days to count. Defaults to ``7``.
:param compute_prune_count: Whether the returned "pruned" dict contains the computed prune count or None.
:param include_roles: Role IDs to include, if given.
:return: A dict containing `{"pruned": int}` or `{"pruned": None}`
"""

payload = {
"days": days,
"compute_prune_count": compute_prune_count,
}
if include_roles:
payload["include_roles"] = ", ".join(str(x) for x in include_roles)

return await self._req.request(Route("POST", f"/guilds/{guild_id}/prune"), json=payload)

async def get_guild_prune_count(
self, guild_id: int, days: int = 7, include_roles: Optional[List[int]] = None
) -> dict:
"""
Retrieves a dict from an API that results in how many members would be pruned given the amount of days.

:param guild_id: Guild ID snowflake.
:param days: Number of days to count. Defaults to ``7``.
:param days: Number of days to count. Defaults to 7.
:param include_roles: Role IDs to include, if given.
:return: A dict denoting `{"pruned": int}`
"""
Expand Down
59 changes: 59 additions & 0 deletions interactions/api/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,65 @@ async def get_all_bans(self) -> List[Dict[str, User]]:

return res

async def prune(
self,
days: int = 7,
compute_prune_count: bool = True,
include_roles: Optional[Union[List[Role], List[int]]] = MISSING,
) -> Optional[int]:
"""
Begins a prune operation.

:param days: Number of days to count. Defaults to 7.
:param compute_prune_count: Whether the returned "pruned" dict contains the computed prune count or None.
:param include_roles: Role IDs to include, if given.
:return: The number of pruned members, if compute_prune_count is not false. Otherwise returns None.
:rtype: Optional[int]
"""
if not self._client:
raise LibraryException(code=13)

if include_roles is not MISSING:
_roles = [role.id if isinstance(role, Role) else role for role in include_roles]
else:
_roles = None

res: dict = await self._client.begin_guild_prune(
guild_id=int(self.id),
days=days,
compute_prune_count=compute_prune_count,
include_roles=_roles,
)

return res.get("pruned")

async def get_prune_count(
self, days: int = 7, include_roles: Optional[Union[List[Role], List[int]]] = MISSING
) -> int:
"""
Returns the number of members that would be removed in a prune operation.

:param days: Number of days to count. Defaults to 7.
:param include_roles: Role IDs to include, if given.
:return: The number of members that would be pruned.
:rtype: int
"""
if not self._client:
raise LibraryException(code=13)

if include_roles is not MISSING:
_roles = [role.id if isinstance(role, Role) else role for role in include_roles]
else:
_roles = None

res: dict = await self._client.get_guild_prune_count(
guild_id=int(self.id),
days=days,
include_roles=_roles,
)

return res.get("pruned")

async def get_emoji(
self,
emoji_id: int,
Expand Down