aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-05-31 18:37:56 +0200
committerGravatar Leon Sandøy <[email protected]>2020-05-31 18:37:56 +0200
commitf344dd8a72024e05577a5aeba25e2f98501417af (patch)
tree1089764d52fd71373727be8b0061a316e737af92
parentAdd a mod_log.ignore_all context manager. (diff)
Fix a bug with invocation deletion.
This command was written to support only a single channel, and with the move to multi-channel purges, we need to rethink the way the invocation deletion happens. We may be invoking this command from a completely different channel, so we can't necessarily look inside the channels we're targeting for the invocation. So, we're solving this by just deleting the invocation by using ctx.message. We do this before we start iterating message history, and then we only need to iterate the number of messages that was passed into the command. A much cleaner approach, which solves the bug reported and identified by @MarkKoz.
-rw-r--r--bot/cogs/clean.py35
1 files changed, 14 insertions, 21 deletions
diff --git a/bot/cogs/clean.py b/bot/cogs/clean.py
index 02216a4af..892c638b8 100644
--- a/bot/cogs/clean.py
+++ b/bot/cogs/clean.py
@@ -10,8 +10,7 @@ from discord.ext.commands import Cog, Context, group
from bot.bot import Bot
from bot.cogs.moderation import ModLog
from bot.constants import (
- Channels, CleanMessages, Colours, Event,
- Icons, MODERATION_ROLES, NEGATIVE_REPLIES
+ Channels, CleanMessages, Colours, Icons, MODERATION_ROLES, NEGATIVE_REPLIES
)
from bot.decorators import with_role
@@ -114,27 +113,23 @@ class Clean(Cog):
if not channels:
channels = [ctx.channel]
+ # Delete the invocation first
+ with self.mod_log.ignore_all():
+ await ctx.message.delete()
+
# Look through the history and retrieve message data
+ # This is only done so we can create a log to upload.
messages = []
message_ids = []
self.cleaning = True
- invocation_deleted = False
- # To account for the invocation message, we index `amount + 1` messages.
for channel in channels:
- async for message in channel.history(limit=amount + 1):
+ async for message in channel.history(limit=amount):
# If at any point the cancel command is invoked, we should stop.
if not self.cleaning:
return
- # Always start by deleting the invocation
- if not invocation_deleted:
- self.mod_log.ignore(Event.message_delete, message.id)
- await message.delete()
- invocation_deleted = True
- continue
-
# If the message passes predicate, let's save it.
if predicate is None or predicate(message):
message_ids.append(message.id)
@@ -142,15 +137,13 @@ class Clean(Cog):
self.cleaning = False
- # We should ignore the ID's we stored, so we don't get mod-log spam.
- self.mod_log.ignore(Event.message_delete, *message_ids)
-
- # Use bulk delete to actually do the cleaning. It's far faster.
- for channel in channels:
- await channel.purge(
- limit=amount,
- check=predicate
- )
+ # Now let's delete the actual messages with purge.
+ with self.mod_log.ignore_all():
+ for channel in channels:
+ await channel.purge(
+ limit=amount,
+ check=predicate
+ )
# Reverse the list to restore chronological order
if messages: