Skip to content

Commit ded9c5d

Browse files
committed
Add support for bulk banning members
1 parent 82d13e7 commit ded9c5d

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

discord/guild.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
Collection,
3535
Coroutine,
3636
Dict,
37+
Iterable,
3738
List,
3839
Mapping,
3940
NamedTuple,
@@ -146,6 +147,11 @@ class BanEntry(NamedTuple):
146147
user: User
147148

148149

150+
class BulkBanResult(NamedTuple):
151+
banned: List[Object]
152+
failed: List[Object]
153+
154+
149155
class _GuildLimit(NamedTuple):
150156
emoji: int
151157
stickers: int
@@ -3789,6 +3795,58 @@ async def unban(self, user: Snowflake, *, reason: Optional[str] = None) -> None:
37893795
"""
37903796
await self._state.http.unban(user.id, self.id, reason=reason)
37913797

3798+
async def bulk_ban(
3799+
self,
3800+
users: Iterable[Snowflake],
3801+
*,
3802+
reason: Optional[str] = None,
3803+
delete_message_seconds: int = 86400,
3804+
) -> BulkBanResult:
3805+
"""|coro|
3806+
3807+
Bans multiple users from the guild.
3808+
3809+
The users must meet the :class:`abc.Snowflake` abc.
3810+
3811+
You must have :attr:`~Permissions.ban_members` to do this.
3812+
3813+
.. versionadded:: 2.4
3814+
3815+
Parameters
3816+
-----------
3817+
users: :class:`abc.Snowflake`
3818+
The user to ban from their guild.
3819+
delete_message_seconds: :class:`int`
3820+
The number of seconds worth of messages to delete from the user
3821+
in the guild. The minimum is 0 and the maximum is 604800 (7 days).
3822+
Defaults to 1 day.
3823+
reason: Optional[:class:`str`]
3824+
The reason the users got banned.
3825+
3826+
Raises
3827+
-------
3828+
Forbidden
3829+
You do not have the proper permissions to ban.
3830+
HTTPException
3831+
Banning failed.
3832+
3833+
Returns
3834+
--------
3835+
:class:`BulkBanResult`
3836+
The result of the bulk ban operation.
3837+
"""
3838+
3839+
response = await self._state.http.bulk_ban(
3840+
self.id,
3841+
user_ids=[u.id for u in users],
3842+
delete_message_seconds=delete_message_seconds,
3843+
reason=reason,
3844+
)
3845+
return BulkBanResult(
3846+
banned=[Object(id=int(user_id), type=User) for user_id in response.get('banned_users', []) or []],
3847+
failed=[Object(id=int(user_id), type=User) for user_id in response.get('failed_users', []) or []],
3848+
)
3849+
37923850
@property
37933851
def vanity_url(self) -> Optional[str]:
37943852
"""Optional[:class:`str`]: The Discord vanity invite URL for this guild, if available.

discord/http.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,20 @@ def unban(self, user_id: Snowflake, guild_id: Snowflake, *, reason: Optional[str
10551055
r = Route('DELETE', '/guilds/{guild_id}/bans/{user_id}', guild_id=guild_id, user_id=user_id)
10561056
return self.request(r, reason=reason)
10571057

1058+
def bulk_ban(
1059+
self,
1060+
guild_id: Snowflake,
1061+
user_ids: List[Snowflake],
1062+
delete_message_seconds: int = 86400,
1063+
reason: Optional[str] = None,
1064+
) -> Response[guild.BulkBanUserResponse]:
1065+
r = Route('POST', '/guilds/{guild_id}/bulk-ban', guild_id=guild_id)
1066+
payload = {
1067+
'user_ids': user_ids,
1068+
'delete_message_seconds': delete_message_seconds,
1069+
}
1070+
return self.request(r, json=payload, reason=reason)
1071+
10581072
def guild_voice_state(
10591073
self,
10601074
user_id: Snowflake,

discord/types/guild.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,8 @@ class _RolePositionRequired(TypedDict):
185185

186186
class RolePositionUpdate(_RolePositionRequired, total=False):
187187
position: Optional[Snowflake]
188+
189+
190+
class BulkBanUserResponse(TypedDict):
191+
banned_users: Optional[List[Snowflake]]
192+
failed_users: Optional[List[Snowflake]]

docs/api.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,25 @@ Guild
44914491

44924492
:type: :class:`User`
44934493

4494+
.. class:: BulkBanResult
4495+
4496+
A namedtuple which represents the result returned from :meth:`~Guild.bulk_ban`.
4497+
4498+
.. versionadded:: 2.4
4499+
4500+
.. attribute:: banned
4501+
4502+
The list of users that were banned. The type of the list is a :class:`Object`
4503+
representing the user.
4504+
4505+
:type: List[:class:`Object`]
4506+
.. attribute:: failed
4507+
4508+
The list of users that could not be banned. The type of the list is a :class:`Object`
4509+
representing the user.
4510+
4511+
:type: List[:class:`Object`]
4512+
44944513

44954514
ScheduledEvent
44964515
~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)