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
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.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import humanize_number
@ -33,6 +33,8 @@ class Cleanup(commands.Cog):
def __init__(self, bot: Red):
super().__init__()
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):
""" Nothing to delete """
@ -72,7 +74,7 @@ class Cleanup(commands.Cog):
@staticmethod
async def get_messages_for_deletion(
*,
channel: discord.TextChannel,
channel: Union[discord.TextChannel, discord.DMChannel],
number: Optional[PositiveInt] = None,
check: Callable[[discord.Message], bool] = lambda x: True,
limit: Optional[PositiveInt] = None,
@ -123,6 +125,27 @@ class Cleanup(commands.Cog):
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
async def get_message_from_reference(
channel: discord.TextChannel, reference: discord.MessageReference
@ -199,6 +222,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command()
@commands.guild_only()
@ -269,6 +293,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command()
@commands.guild_only()
@ -321,6 +346,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel)
@cleanup.command()
@commands.guild_only()
@ -376,6 +402,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command()
@commands.guild_only()
@ -428,6 +455,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command()
@commands.guild_only()
@ -466,6 +494,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command(name="bot")
@commands.guild_only()
@ -552,6 +581,7 @@ class Cleanup(commands.Cog):
log.info(reason)
await mass_purge(to_delete, channel)
await self.send_optional_notification(len(to_delete), channel, subtract_invoking=True)
@cleanup.command(name="self")
@check_self_permissions()
@ -640,6 +670,9 @@ class Cleanup(commands.Cog):
await mass_purge(to_delete, channel)
else:
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"])
@commands.guild_only()
@ -690,3 +723,21 @@ class Cleanup(commands.Cog):
to_delete.append(ctx.message)
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."))