Skip to content

Commit 47ed09d

Browse files
feat: Add full support for prune endpoints (#1062)
* feat: Add `begin_guild_prune` * Update guild.py * Update guild.py * Update guild.py * Remove weird 7 formatting thing * ci: correct from checks. * Add min max counts * Update guild.py * ci: correct from checks. * Add min max to helpers * Fix numbers (sorry ed) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 78d50b6 commit 47ed09d

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

interactions/api/http/guild.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,40 @@ async def remove_guild_member(
601601
Route("DELETE", f"/guilds/{guild_id}/members/{user_id}"), reason=reason
602602
)
603603

604+
async def begin_guild_prune(
605+
self,
606+
guild_id: int,
607+
days: int = 7,
608+
compute_prune_count: bool = True,
609+
include_roles: Optional[List[int]] = None,
610+
) -> dict:
611+
"""
612+
Begins a prune operation.
613+
614+
:param guild_id: Guild ID snowflake
615+
:param days: Number of days to count, minimum 1, maximum 30. Defaults to 7.
616+
:param compute_prune_count: Whether the returned "pruned" dict contains the computed prune count or None.
617+
:param include_roles: Role IDs to include, if given.
618+
:return: A dict containing `{"pruned": int}` or `{"pruned": None}`
619+
"""
620+
621+
payload = {
622+
"days": days,
623+
"compute_prune_count": compute_prune_count,
624+
}
625+
if include_roles:
626+
payload["include_roles"] = ", ".join(str(x) for x in include_roles)
627+
628+
return await self._req.request(Route("POST", f"/guilds/{guild_id}/prune"), json=payload)
629+
604630
async def get_guild_prune_count(
605631
self, guild_id: int, days: int = 7, include_roles: Optional[List[int]] = None
606632
) -> dict:
607633
"""
608634
Retrieves a dict from an API that results in how many members would be pruned given the amount of days.
609635
610636
:param guild_id: Guild ID snowflake.
611-
:param days: Number of days to count. Defaults to ``7``.
637+
:param days: Number of days to count, minimum 1, maximum 30. Defaults to 7.
612638
:param include_roles: Role IDs to include, if given.
613639
:return: A dict denoting `{"pruned": int}`
614640
"""

interactions/api/models/guild.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,71 @@ async def get_all_bans(self) -> List[Dict[str, User]]:
20302030

20312031
return res
20322032

2033+
async def prune(
2034+
self,
2035+
days: int = 7,
2036+
compute_prune_count: bool = True,
2037+
include_roles: Optional[Union[List[Role], List[int], List[Snowflake], List[str]]] = MISSING,
2038+
) -> Optional[int]:
2039+
"""
2040+
Begins a prune operation.
2041+
2042+
:param days: Number of days to count, minimum 1, maximum 30. Defaults to 7.
2043+
:param compute_prune_count: Whether the returned "pruned" dict contains the computed prune count or None.
2044+
:param include_roles: Role IDs to include, if given.
2045+
:return: The number of pruned members, if compute_prune_count is not false. Otherwise returns None.
2046+
:rtype: Optional[int]
2047+
"""
2048+
if not self._client:
2049+
raise LibraryException(code=13)
2050+
2051+
if include_roles is not MISSING:
2052+
_roles = [
2053+
int(role.id) if isinstance(role, Role) else int(role) for role in include_roles
2054+
]
2055+
else:
2056+
_roles = None
2057+
2058+
res: dict = await self._client.begin_guild_prune(
2059+
guild_id=int(self.id),
2060+
days=days,
2061+
compute_prune_count=compute_prune_count,
2062+
include_roles=_roles,
2063+
)
2064+
2065+
return res.get("pruned")
2066+
2067+
async def get_prune_count(
2068+
self,
2069+
days: int = 7,
2070+
include_roles: Optional[Union[List[Role], List[int], List[Snowflake], List[str]]] = MISSING,
2071+
) -> int:
2072+
"""
2073+
Returns the number of members that would be removed in a prune operation.
2074+
2075+
:param days: Number of days to count, minimum 1, maximum 30. Defaults to 7.
2076+
:param include_roles: Role IDs to include, if given.
2077+
:return: The number of members that would be pruned.
2078+
:rtype: int
2079+
"""
2080+
if not self._client:
2081+
raise LibraryException(code=13)
2082+
2083+
if include_roles is not MISSING:
2084+
_roles = [
2085+
int(role.id) if isinstance(role, Role) else int(role) for role in include_roles
2086+
]
2087+
else:
2088+
_roles = None
2089+
2090+
res: dict = await self._client.get_guild_prune_count(
2091+
guild_id=int(self.id),
2092+
days=days,
2093+
include_roles=_roles,
2094+
)
2095+
2096+
return res.get("pruned")
2097+
20332098
async def get_emoji(
20342099
self,
20352100
emoji_id: int,

0 commit comments

Comments
 (0)