From 23a41c880ad1fbdcb86da9cd2492ff76a78fdd62 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 28 Aug 2022 16:06:57 +1000 Subject: [PATCH] Add support for delete_message_seconds ban argument --- discord/guild.py | 26 ++++++++++++++++++++++++-- discord/http.py | 4 ++-- discord/member.py | 10 ++++++++-- docs/ext/commands/commands.rst | 3 ++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index 533bff67c..f876b3783 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -46,6 +46,7 @@ from typing import ( Union, overload, ) +import warnings from . import utils, abc from .role import Role @@ -3338,7 +3339,8 @@ class Guild(Hashable): user: Snowflake, *, reason: Optional[str] = None, - delete_message_days: int = 1, + delete_message_days: int = MISSING, + delete_message_seconds: int = MISSING, ) -> None: """|coro| @@ -3356,6 +3358,13 @@ class Guild(Hashable): delete_message_days: :class:`int` The number of days worth of messages to delete from the user in the guild. The minimum is 0 and the maximum is 7. + + .. deprecated:: 2.0 + 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). + + .. versionadded:: 2.0 reason: Optional[:class:`str`] The reason the user got banned. @@ -3367,8 +3376,21 @@ class Guild(Hashable): You do not have the proper permissions to ban. HTTPException Banning failed. + TypeError + You specified both ``delete_message_days`` and ``delete_message_seconds``. """ - await self._state.http.ban(user.id, self.id, delete_message_days, reason=reason) + if delete_message_days is not MISSING and delete_message_seconds is not MISSING: + raise TypeError('Cannot mix delete_message_days and delete_message_seconds keyword arguments.') + + if delete_message_days is not MISSING: + msg = 'delete_message_days is deprecated, use delete_message_seconds instead' + warnings.warn(msg, DeprecationWarning, stacklevel=2) + delete_message_seconds = delete_message_days * 86400 # one day + + if delete_message_seconds is MISSING: + delete_message_seconds = 86400 # one day + + await self._state.http.ban(user.id, self.id, delete_message_seconds, reason=reason) async def unban(self, user: Snowflake, *, reason: Optional[str] = None) -> None: """|coro| diff --git a/discord/http.py b/discord/http.py index ebbc34f6a..64fcca495 100644 --- a/discord/http.py +++ b/discord/http.py @@ -1262,12 +1262,12 @@ class HTTPClient: self, user_id: Snowflake, guild_id: Snowflake, - delete_message_days: int = 1, + delete_message_seconds: int = 86400, # one day reason: Optional[str] = None, ) -> Response[None]: r = Route('PUT', '/guilds/{guild_id}/bans/{user_id}', guild_id=guild_id, user_id=user_id) payload = { - 'delete_message_days': str(delete_message_days), + 'delete_message_seconds': delete_message_seconds, } return self.request(r, json=payload, reason=reason) diff --git a/discord/member.py b/discord/member.py index a6b20e778..f6b3f6dbe 100644 --- a/discord/member.py +++ b/discord/member.py @@ -652,14 +652,20 @@ class Member(discord.abc.Messageable, discord.abc.Connectable, _UserTag): async def ban( self, *, - delete_message_days: int = 1, + delete_message_days: int = MISSING, + delete_message_seconds: int = MISSING, reason: Optional[str] = None, ) -> None: """|coro| Bans this member. Equivalent to :meth:`Guild.ban`. """ - await self.guild.ban(self, reason=reason, delete_message_days=delete_message_days) + await self.guild.ban( + self, + reason=reason, + delete_message_days=delete_message_days, + delete_message_seconds=delete_message_seconds, + ) async def unban(self, *, reason: Optional[str] = None) -> None: """|coro| diff --git a/docs/ext/commands/commands.rst b/docs/ext/commands/commands.rst index 238a4581f..275c408e8 100644 --- a/docs/ext/commands/commands.rst +++ b/docs/ext/commands/commands.rst @@ -606,8 +606,9 @@ When mixed with the :data:`typing.Optional` converter you can provide simple and delete_days: typing.Optional[int] = 0, *, reason: str): """Mass bans members with an optional delete_days parameter""" + delete_seconds = delete_days * 86400 # one day for member in members: - await member.ban(delete_message_days=delete_days, reason=reason) + await member.ban(delete_message_seconds=delete_seconds, reason=reason) This command can be invoked any of the following ways: