From 64e6044aba3ff6f828f15287ee88a9d4b2d156fd Mon Sep 17 00:00:00 2001 From: Leet <36166244+leetfin@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:02:37 -0400 Subject: [PATCH] [Cleanup] Pass reason for bulk message deletion to audit log (#5863) Co-authored-by: Jakub Kuczys --- redbot/cogs/cleanup/cleanup.py | 18 +++++++++--------- redbot/core/utils/mod.py | 8 ++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/redbot/cogs/cleanup/cleanup.py b/redbot/cogs/cleanup/cleanup.py index d3996e343..37b937341 100644 --- a/redbot/cogs/cleanup/cleanup.py +++ b/redbot/cogs/cleanup/cleanup.py @@ -227,7 +227,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command() @@ -298,7 +298,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command() @@ -351,7 +351,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel) @cleanup.command() @@ -407,7 +407,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command() @@ -460,7 +460,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command() @@ -499,7 +499,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command(name="bot") @@ -586,7 +586,7 @@ class Cleanup(commands.Cog): ) log.info(reason) - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True) @cleanup.command(name="self") @@ -673,7 +673,7 @@ class Cleanup(commands.Cog): log.info(reason) if can_mass_purge: - await mass_purge(to_delete, channel) + await mass_purge(to_delete, channel, reason=reason) else: await slow_deletion(to_delete) await self.send_optional_notification( @@ -733,7 +733,7 @@ class Cleanup(commands.Cog): ) to_delete.append(ctx.message) - await mass_purge(to_delete, ctx.channel) + await mass_purge(to_delete, ctx.channel, "Duplicate message cleanup") await self.send_optional_notification(len(to_delete), ctx.channel, subtract_invoking=True) @commands.group() diff --git a/redbot/core/utils/mod.py b/redbot/core/utils/mod.py index bef4069c7..09212f821 100644 --- a/redbot/core/utils/mod.py +++ b/redbot/core/utils/mod.py @@ -1,6 +1,6 @@ import asyncio from datetime import timedelta -from typing import List, Iterable, Union, TYPE_CHECKING, Dict +from typing import List, Iterable, Union, TYPE_CHECKING, Dict, Optional import discord @@ -12,6 +12,8 @@ if TYPE_CHECKING: async def mass_purge( messages: List[discord.Message], channel: Union[discord.TextChannel, discord.VoiceChannel, discord.Thread], + *, + reason: Optional[str] = None, ): """Bulk delete messages from a channel. @@ -29,6 +31,8 @@ async def mass_purge( The messages to bulk delete. channel : `discord.TextChannel`, `discord.VoiceChannel`, or `discord.Thread` The channel to delete messages from. + reason : `str`, optional + The reason for bulk deletion, which will appear in the audit log. Raises ------ @@ -43,7 +47,7 @@ async def mass_purge( # discord.NotFound can be raised when `len(messages) == 1` and the message does not exist. # As a result of this obscure behavior, this error needs to be caught just in case. try: - await channel.delete_messages(messages[:100]) + await channel.delete_messages(messages[:100], reason=reason) except discord.errors.HTTPException: pass messages = messages[100:]