From b3850f6bb7a09f4a97cf91fd094c24fe4997fdde Mon Sep 17 00:00:00 2001 From: Neuro Assassin <42872277+NeuroAssassin@users.noreply.github.com> Date: Mon, 29 Apr 2019 15:55:23 -0400 Subject: [PATCH] [Cleanup] Add `[p]cleanup between` (#2617) * Add `[p]cleanup between` At the moment, only deletes the messages in between, does not delete the messages specified when running the command. * Create converter for message IDs; remove f-strings; use converters in a few extra commands * Sacrifice to the style gods --- redbot/cogs/cleanup/cleanup.py | 53 +++++++++++++++++++++++++++++-- redbot/cogs/cleanup/converters.py | 12 +++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 redbot/cogs/cleanup/converters.py diff --git a/redbot/cogs/cleanup/cleanup.py b/redbot/cogs/cleanup/cleanup.py index d76295b00..3b95c71aa 100644 --- a/redbot/cogs/cleanup/cleanup.py +++ b/redbot/cogs/cleanup/cleanup.py @@ -10,6 +10,7 @@ from redbot.core.i18n import Translator, cog_i18n from redbot.core.utils.mod import slow_deletion, mass_purge from redbot.cogs.mod.log import log from redbot.core.utils.predicates import MessagePredicate +from .converters import RawMessageIds _ = Translator("Cleanup", __file__) @@ -211,7 +212,9 @@ class Cleanup(commands.Cog): @cleanup.command() @commands.guild_only() @commands.bot_has_permissions(manage_messages=True) - async def after(self, ctx: commands.Context, message_id: int, delete_pinned: bool = False): + async def after( + self, ctx: commands.Context, message_id: RawMessageIds, delete_pinned: bool = False + ): """Delete all messages after a specified message. To get a message id, enable developer mode in Discord's @@ -242,7 +245,11 @@ class Cleanup(commands.Cog): @commands.guild_only() @commands.bot_has_permissions(manage_messages=True) async def before( - self, ctx: commands.Context, message_id: int, number: int, delete_pinned: bool = False + self, + ctx: commands.Context, + message_id: RawMessageIds, + number: int, + delete_pinned: bool = False, ): """Deletes X messages before specified message. @@ -271,6 +278,48 @@ class Cleanup(commands.Cog): await mass_purge(to_delete, channel) + @cleanup.command() + @commands.guild_only() + @commands.bot_has_permissions(manage_messages=True) + async def between( + self, + ctx: commands.Context, + one: RawMessageIds, + two: RawMessageIds, + delete_pinned: bool = False, + ): + """Delete the messages between Messsage One and Message Two, providing the messages IDs. + + The first message ID should be the older message and the second one the newer. + + Example: + `[p]cleanup between 123456789123456789 987654321987654321` + """ + channel = ctx.channel + author = ctx.author + try: + mone = await channel.fetch_message(one) + except discord.errors.Notfound: + return await ctx.send( + _("Could not find a message with the ID of {id}.".format(id=one)) + ) + try: + mtwo = await channel.fetch_message(two) + except discord.errors.Notfound: + return await ctx.send( + _("Could not find a message with the ID of {id}.".format(id=two)) + ) + to_delete = await self.get_messages_for_deletion( + channel=channel, before=mtwo, after=mone, delete_pinned=delete_pinned + ) + to_delete.append(ctx.message) + reason = "{}({}) deleted {} messages in channel {}.".format( + author.name, author.id, len(to_delete), channel.name + ) + log.info(reason) + + await mass_purge(to_delete, channel) + @cleanup.command() @commands.guild_only() @commands.bot_has_permissions(manage_messages=True) diff --git a/redbot/cogs/cleanup/converters.py b/redbot/cogs/cleanup/converters.py new file mode 100644 index 000000000..ad271b64c --- /dev/null +++ b/redbot/cogs/cleanup/converters.py @@ -0,0 +1,12 @@ +from redbot.core.commands import Converter, BadArgument +from redbot.core.i18n import Translator + +_ = Translator("Cleanup", __file__) + + +class RawMessageIds(Converter): + async def convert(self, ctx, argument): + if argument.isnumeric() and len(argument) >= 17: + return int(argument) + + raise BadArgument(_("{} doesn't look like a valid message ID.").format(argument))