Skip to content

Commit e893c18

Browse files
authored
feat: add bulk banning (#1695)
* chore: bump discord_typings vbersion * feat: add bulk banning
1 parent 720d935 commit e893c18

File tree

8 files changed

+326
-252
lines changed

8 files changed

+326
-252
lines changed

interactions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
BrandColors,
8484
BrandColours,
8585
Buckets,
86+
BulkBanResponse,
8687
Button,
8788
ButtonStyle,
8889
CallbackObject,
@@ -408,6 +409,7 @@
408409
"BrandColors",
409410
"BrandColours",
410411
"Buckets",
412+
"BulkBanResponse",
411413
"Button",
412414
"ButtonStyle",
413415
"CallbackObject",

interactions/api/http/http_requests/guild.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ async def remove_guild_ban(
299299
Route("DELETE", "/guilds/{guild_id}/bans/{user_id}", guild_id=guild_id, user_id=user_id), reason=reason
300300
)
301301

302+
async def bulk_guild_ban(
303+
self,
304+
guild_id: "Snowflake_Type",
305+
user_ids: "list[Snowflake_Type]",
306+
delete_message_seconds: int = 0,
307+
reason: str | None = None,
308+
) -> discord_typings.BulkBanData:
309+
"""
310+
Ban a list of users from the guild.
311+
312+
Args:
313+
guild_id: The ID of the guild to create the ban in
314+
user_ids: List of user ids to ban (max 200)
315+
delete_message_seconds: Number of seconds to delete messages for (0-604800)
316+
reason: The reason for this action
317+
318+
Returns:
319+
Bulk ban object
320+
321+
"""
322+
payload = {"delete_message_days": delete_message_seconds, "user_ids": user_ids}
323+
result = await self.request(
324+
Route("POST", "/guilds/{guild_id}/bulk-ban", guild_id=guild_id), payload=payload, reason=reason
325+
)
326+
return cast(discord_typings.BulkBanData, result)
327+
302328
async def get_guild_prune_count(
303329
self,
304330
guild_id: "Snowflake_Type",

interactions/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
BaseUser,
3030
BrandColors,
3131
BrandColours,
32+
BulkBanResponse,
3233
Button,
3334
ButtonStyle,
3435
ChannelFlags,
@@ -358,6 +359,7 @@
358359
"BrandColors",
359360
"BrandColours",
360361
"Buckets",
362+
"BulkBanResponse",
361363
"Button",
362364
"ButtonStyle",
363365
"CallbackObject",

interactions/models/discord/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
AuditLogEntry,
129129
AuditLogHistory,
130130
BaseGuild,
131+
BulkBanResponse,
131132
Guild,
132133
GuildBan,
133134
GuildIntegration,
@@ -205,6 +206,7 @@
205206
"BaseUser",
206207
"BrandColors",
207208
"BrandColours",
209+
"BulkBanResponse",
208210
"Button",
209211
"ButtonStyle",
210212
"ChannelFlags",

interactions/models/discord/guild.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
__all__ = (
6363
"GuildBan",
64+
"BulkBanResponse",
6465
"BaseGuild",
6566
"GuildWelcome",
6667
"GuildPreview",
@@ -85,6 +86,34 @@ class GuildBan:
8586
"""The banned user"""
8687

8788

89+
@attrs.define(eq=False, order=False, hash=False, kw_only=True)
90+
class BulkBanResponse(ClientObject):
91+
_banned_users: list[Snowflake_Type] = attrs.field(repr=False, converter=to_snowflake_list)
92+
"""List of user IDs that were successfully banned."""
93+
_failed_users: list[Snowflake_Type] = attrs.field(repr=False, converter=to_snowflake_list)
94+
"""List of user IDs that were not banned."""
95+
96+
@property
97+
def banned_users(self) -> List["models.User | None"]:
98+
"""List of users that were successfully banned."""
99+
return [self.client.cache.get_user(u_id) for u_id in self._banned_users]
100+
101+
@property
102+
def failed_users(self) -> List["models.User | None"]:
103+
"""List of users that were not banned."""
104+
return [self.client.cache.get_user(u_id) for u_id in self._failed_users]
105+
106+
@property
107+
def failed_user_ids(self) -> List[Snowflake_Type]:
108+
"""List of user IDs that were not banned."""
109+
return self._failed_users
110+
111+
@property
112+
def banned_user_ids(self) -> List[Snowflake_Type]:
113+
"""List of user IDs that were successfully banned."""
114+
return self._banned_users
115+
116+
88117
@attrs.define(eq=False, order=False, hash=False, kw_only=True)
89118
class BaseGuild(DiscordObject):
90119
name: str = attrs.field(repr=True)
@@ -1748,6 +1777,29 @@ async def ban(
17481777
delete_message_seconds = delete_message_days * 3600
17491778
await self._client.http.create_guild_ban(self.id, to_snowflake(user), delete_message_seconds, reason=reason)
17501779

1780+
async def bulk_ban(
1781+
self,
1782+
users: List[Union["models.User", "models.Member", Snowflake_Type]],
1783+
delete_message_seconds: int = 0,
1784+
reason: Optional[str] = None,
1785+
) -> BulkBanResponse:
1786+
"""
1787+
Bans a list of users from the guild.
1788+
1789+
!!! note
1790+
You must have the `ban members` permission
1791+
1792+
Args:
1793+
user: The users to ban
1794+
delete_message_seconds: How many seconds worth of messages to remove
1795+
reason: The reason for the ban
1796+
1797+
"""
1798+
result = await self.client.http.bulk_guild_ban(
1799+
self.id, [to_snowflake(user) for user in users], delete_message_seconds, reason=reason
1800+
)
1801+
return BulkBanResponse.from_dict(result, self.client)
1802+
17511803
async def fetch_ban(self, user: Union["models.User", "models.Member", Snowflake_Type]) -> Optional[GuildBan]:
17521804
"""
17531805
Fetches the ban information for the specified user in the guild. You must have the `ban members` permission.

0 commit comments

Comments
 (0)