Browse Source

Add support for bulk banning members

pull/10109/head
Rapptz 1 year ago
committed by dolfies
parent
commit
a51939080b
  1. 58
      discord/guild.py
  2. 14
      discord/http.py
  3. 5
      discord/types/guild.py
  4. 20
      docs/api.rst

58
discord/guild.py

@ -35,6 +35,7 @@ from typing import (
Collection,
Coroutine,
Dict,
Iterable,
List,
Mapping,
NamedTuple,
@ -163,6 +164,11 @@ class BanEntry(NamedTuple):
user: User
class BulkBanResult(NamedTuple):
banned: List[Object]
failed: List[Object]
class _GuildLimit(NamedTuple):
emoji: int
stickers: int
@ -4337,6 +4343,58 @@ class Guild(Hashable):
"""
await self._state.http.unban(user.id, self.id, reason=reason)
async def bulk_ban(
self,
users: Iterable[Snowflake],
*,
reason: Optional[str] = None,
delete_message_seconds: int = 86400,
) -> BulkBanResult:
"""|coro|
Bans multiple users from the guild.
The users must meet the :class:`abc.Snowflake` abc.
You must have :attr:`~Permissions.ban_members` to do this.
.. versionadded:: 2.4
Parameters
-----------
users: :class:`abc.Snowflake`
The user to ban from their guild.
delete_message_seconds: :class:`int`
The number of seconds worth of messages to delete from the user
in the guild. The minimum is 0 and the maximum is 604800 (7 days).
Defaults to 1 day.
reason: Optional[:class:`str`]
The reason the users got banned.
Raises
-------
Forbidden
You do not have the proper permissions to ban.
HTTPException
Banning failed.
Returns
--------
:class:`BulkBanResult`
The result of the bulk ban operation.
"""
response = await self._state.http.bulk_ban(
self.id,
user_ids=[u.id for u in users],
delete_message_seconds=delete_message_seconds,
reason=reason,
)
return BulkBanResult(
banned=[Object(id=int(user_id), type=User) for user_id in response.get('banned_users', []) or []],
failed=[Object(id=int(user_id), type=User) for user_id in response.get('failed_users', []) or []],
)
@property
def vanity_url(self) -> Optional[str]:
"""Optional[:class:`str`]: The Discord vanity invite URL for this guild, if available.

14
discord/http.py

@ -1465,6 +1465,20 @@ class HTTPClient:
Route('DELETE', '/guilds/{guild_id}/bans/{user_id}', guild_id=guild_id, user_id=user_id), reason=reason
)
def bulk_ban(
self,
guild_id: Snowflake,
user_ids: List[Snowflake],
delete_message_seconds: int = 86400,
reason: Optional[str] = None,
) -> Response[guild.BulkBanUserResponse]:
r = Route('POST', '/guilds/{guild_id}/bulk-ban', guild_id=guild_id)
payload = {
'user_ids': user_ids,
'delete_message_seconds': delete_message_seconds,
}
return self.request(r, json=payload, reason=reason)
def guild_voice_state(
self,
user_id: Snowflake,

5
discord/types/guild.py

@ -196,3 +196,8 @@ class CommandScopeMigration(TypedDict):
class SupplementalGuild(UnavailableGuild):
embedded_activities: list
voice_states: List[VoiceState]
class BulkBanUserResponse(TypedDict):
banned_users: Optional[List[Snowflake]]
failed_users: Optional[List[Snowflake]]

20
docs/api.rst

@ -7419,6 +7419,26 @@ Guild
:type: :class:`User`
.. class:: BulkBanResult
A namedtuple which represents the result returned from :meth:`~Guild.bulk_ban`.
.. versionadded:: 2.1
.. attribute:: banned
The list of users that were banned. The type of the list is a :class:`Object`
representing the user.
:type: List[:class:`Object`]
.. attribute:: failed
The list of users that could not be banned. The type of the list is a :class:`Object`
representing the user.
:type: List[:class:`Object`]
Role
~~~~~

Loading…
Cancel
Save