Add toggleable notification of deleted messages count in Cleanup (#5218)

* Addition of cleanup notification.

* black

* subtract invoking message from count

* Update redbot/cogs/cleanup/cleanup.py

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* be explicity about kwarg

* address review

* style

* Fix type hints

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Jamie 2021-09-02 16:12:28 -07:00 committed by GitHub
parent 4e2e4bfe6a
commit c4f8f65d4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ from typing import Callable, List, Optional, Set, Union
import discord import discord
from redbot.cogs.mod.converters import RawUserIds from redbot.cogs.mod.converters import RawUserIds
from redbot.core import checks, commands from redbot.core import checks, commands, Config
from redbot.core.bot import Red from redbot.core.bot import Red
from redbot.core.i18n import Translator, cog_i18n from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import humanize_number from redbot.core.utils.chat_formatting import humanize_number
@ -33,6 +33,8 @@ class Cleanup(commands.Cog):
def __init__(self, bot: Red): def __init__(self, bot: Red):
super().__init__() super().__init__()
self.bot = bot self.bot = bot
self.config = Config.get_conf(self, 8927348724, force_registration=True)
self.config.register_guild(notify=True)
async def red_delete_data_for_user(self, **kwargs): async def red_delete_data_for_user(self, **kwargs):
""" Nothing to delete """ """ Nothing to delete """
@ -72,7 +74,7 @@ class Cleanup(commands.Cog):
@staticmethod @staticmethod
async def get_messages_for_deletion( async def get_messages_for_deletion(
*, *,
channel: discord.TextChannel, channel: Union[discord.TextChannel, discord.DMChannel],
number: Optional[PositiveInt] = None, number: Optional[PositiveInt] = None,
check: Callable[[discord.Message], bool] = lambda x: True, check: Callable[[discord.Message], bool] = lambda x: True,
limit: Optional[PositiveInt] = None, limit: Optional[PositiveInt] = None,
@ -123,6 +125,27 @@ class Cleanup(commands.Cog):
return collected return collected
async def send_optional_notification(
self,
num: int,
channel: Union[discord.TextChannel, discord.DMChannel],
*,
subtract_invoking: bool = False,
) -> None:
"""
Sends a notification to the channel that a certain number of messages have been deleted.
"""
if not hasattr(channel, "guild") or await self.config.guild(channel.guild).notify():
if subtract_invoking:
num -= 1
if num == 1:
await channel.send(_("1 message was deleted."), delete_after=5)
else:
await channel.send(
_("{num} messages were deleted.").format(num=humanize_number(num)),
delete_after=5,
)
@staticmethod @staticmethod
async def get_message_from_reference( async def get_message_from_reference(
channel: discord.TextChannel, reference: discord.MessageReference channel: discord.TextChannel, reference: discord.MessageReference
@ -199,6 +222,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@ -269,6 +293,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@ -321,6 +346,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel)
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@ -376,6 +402,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@ -428,6 +455,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@ -466,6 +494,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command(name="bot") @cleanup.command(name="bot")
@commands.guild_only() @commands.guild_only()
@ -552,6 +581,7 @@ class Cleanup(commands.Cog):
log.info(reason) log.info(reason)
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command(name="self") @cleanup.command(name="self")
@check_self_permissions() @check_self_permissions()
@ -640,6 +670,9 @@ class Cleanup(commands.Cog):
await mass_purge(to_delete, channel) await mass_purge(to_delete, channel)
else: else:
await slow_deletion(to_delete) await slow_deletion(to_delete)
await self.send_optional_notification(
len(to_delete), channel, subtract_invoking=can_mass_purge
)
@cleanup.command(name="duplicates", aliases=["spam"]) @cleanup.command(name="duplicates", aliases=["spam"])
@commands.guild_only() @commands.guild_only()
@ -690,3 +723,21 @@ class Cleanup(commands.Cog):
to_delete.append(ctx.message) to_delete.append(ctx.message)
await mass_purge(to_delete, ctx.channel) await mass_purge(to_delete, ctx.channel)
await self.send_optional_notification(len(to_delete), ctx.channel, subtract_invoking=True)
@commands.group()
@commands.admin_or_permissions(manage_messages=True)
async def cleanupset(self, ctx: commands.Context):
"""Manage the settings for the cleanup command."""
pass
@cleanupset.command(name="notify")
async def cleanupset_notify(self, ctx: commands.Context):
"""Toggle clean up notification settings."""
toggle = await self.config.guild(ctx.guild).notify()
if toggle:
await self.config.guild(ctx.guild).notify.set(False)
await ctx.send(_("I will no longer notify of message deletions."))
else:
await self.config.guild(ctx.guild).notify.set(True)
await ctx.send(_("I will now notify of message deletions."))