From da7e24c838ecb99bf72ac5ef397873ed4e605dad Mon Sep 17 00:00:00 2001 From: mbaruh Date: Sat, 11 Jun 2022 19:02:05 +0300 Subject: Fix clean range not being exclusive --- bot/exts/moderation/clean.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index 39eff9757..123148e02 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -179,7 +179,7 @@ class Clean(Cog): def predicate_range(message: Message) -> bool: """Check if the message age is between the two limits.""" - return first_limit <= message.created_at <= second_limit + return first_limit < message.created_at < second_limit def predicate_after(message: Message) -> bool: """Check if the message is older than the first limit.""" @@ -471,7 +471,7 @@ class Clean(Cog): \u2003• `users`: A series of user mentions, ID's, or names. \u2003• `first_limit` and `second_limit`: A message, a duration delta, or an ISO datetime. - At least one limit is required. + At least one limit is required. The limits are *exclusive*. If a message is provided, cleaning will happen in that channel, and channels cannot be provided. If only one of them is provided, acts as `clean until`. If both are provided, acts as `clean between`. \u2003• `regex`: A regex pattern the message must contain to be deleted. @@ -565,6 +565,8 @@ class Clean(Cog): A limit can be either a message, and ISO date-time string, or a time delta. + The limit is *exclusive*. + If a message is specified the cleanup will be limited to the channel the message is in. If a timedelta or an ISO datetime is specified, `channels` can be specified to clean across multiple channels. @@ -590,6 +592,8 @@ class Clean(Cog): The range is specified through two limits. A limit can be either a message, and ISO date-time string, or a time delta. + The limits are *exclusive*. + If two messages are specified, they both must be in the same channel. The cleanup will be limited to the channel the messages are in. -- cgit v1.2.3 From 49c89472a08491b847c82d4cc27b1b54080daa93 Mon Sep 17 00:00:00 2001 From: mbaruh Date: Sat, 11 Jun 2022 19:33:13 +0300 Subject: Add user purging command The command cleans all public messages from a user. This is a private case that is common enough that it probably deserves a shortcut. Since the `purge` alias is never used for the clean group I repurposed it for this new command. --- bot/exts/moderation/clean.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index 123148e02..5a3481067 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -8,7 +8,7 @@ from itertools import takewhile from typing import Callable, Iterable, Literal, Optional, TYPE_CHECKING, Union from discord import Colour, Message, NotFound, TextChannel, Thread, User, errors -from discord.ext.commands import Cog, Context, Converter, Greedy, group, has_any_role +from discord.ext.commands import Cog, Context, Converter, Greedy, command, group, has_any_role from discord.ext.commands.converter import TextChannelConverter from discord.ext.commands.errors import BadArgument @@ -452,7 +452,7 @@ class Clean(Cog): # region: Commands - @group(invoke_without_command=True, name="clean", aliases=["clear", "purge"]) + @group(invoke_without_command=True, name="clean", aliases=("clear",)) async def clean_group( self, ctx: Context, @@ -619,6 +619,13 @@ class Clean(Cog): await self._send_expiring_message(ctx, message) await self._delete_invocation(ctx) + @command() + async def purge(self, ctx: Context, users: Greedy[User], age: Optional[Union[Age, ISODateTime]] = None) -> None: + """Clean messages of users from all public channels up to a certain message age (10 minutes by default).""" + if age is None: + age = await Age().convert(ctx, "10M") + await self._clean_messages(ctx, channels="*", users=users, first_limit=age) + # endregion async def cog_check(self, ctx: Context) -> bool: -- cgit v1.2.3 From 154cd0457aaa703a000da8d0d21642578496de78 Mon Sep 17 00:00:00 2001 From: mbaruh Date: Sun, 12 Jun 2022 19:53:04 +0300 Subject: Clarify age is exclusive in `purge` docstring --- bot/exts/moderation/clean.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index 5a3481067..4b65a1338 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -621,7 +621,11 @@ class Clean(Cog): @command() async def purge(self, ctx: Context, users: Greedy[User], age: Optional[Union[Age, ISODateTime]] = None) -> None: - """Clean messages of users from all public channels up to a certain message age (10 minutes by default).""" + """ + Clean messages of users from all public channels up to a certain message age (10 minutes by default). + + The age is *exclusive*, meaning that `10s` won't delete a message exactly 10 seconds old. + """ if age is None: age = await Age().convert(ctx, "10M") await self._clean_messages(ctx, channels="*", users=users, first_limit=age) -- cgit v1.2.3 From 0bbcde6af81405380bbae1eca3556338331b03fa Mon Sep 17 00:00:00 2001 From: mbaruh Date: Sun, 12 Jun 2022 21:24:11 +0300 Subject: Make `predicate_after` exclusive as well --- bot/exts/moderation/clean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index 4b65a1338..fb672f3dd 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -183,7 +183,7 @@ class Clean(Cog): def predicate_after(message: Message) -> bool: """Check if the message is older than the first limit.""" - return message.created_at >= first_limit + return message.created_at > first_limit predicates = [] # Set up the correct predicate -- cgit v1.2.3 From db65e49ee92656e85859745613d60c57fe4ae78e Mon Sep 17 00:00:00 2001 From: mbaruh Date: Mon, 13 Jun 2022 01:28:26 +0300 Subject: Correct _get_messages_from_channels type hints and add docstring --- bot/exts/moderation/clean.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index fb672f3dd..4e65c4396 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -241,9 +241,14 @@ class Clean(Cog): self, channels: Iterable[TextChannel], to_delete: Predicate, - before: datetime, - after: Optional[datetime] = None + after: datetime, + before: Optional[datetime] = None ) -> tuple[defaultdict[TextChannel, list], list]: + """ + Collect the messages for deletion by iterating over the histories of the appropriate channels. + + The clean cog enforces an upper limit on message age through `_validate_input`. + """ message_mappings = defaultdict(list) message_ids = [] @@ -419,8 +424,8 @@ class Clean(Cog): message_mappings, message_ids = await self._get_messages_from_channels( channels=deletion_channels, to_delete=predicate, - before=second_limit, - after=first_limit # Remember first is the earlier datetime. + after=first_limit, # Remember first is the earlier datetime (the "older" time). + before=second_limit ) if not self.cleaning: -- cgit v1.2.3 From d1dcf890bb1af5c5edf0be701b2e0275d315b522 Mon Sep 17 00:00:00 2001 From: Boris Muratov <8bee278@gmail.com> Date: Mon, 13 Jun 2022 01:29:55 +0300 Subject: Correct "older" to "younger" Co-authored-by: Mark <1515135+MarkKoz@users.noreply.github.com> --- bot/exts/moderation/clean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py index 4e65c4396..d7c2c7673 100644 --- a/bot/exts/moderation/clean.py +++ b/bot/exts/moderation/clean.py @@ -182,7 +182,7 @@ class Clean(Cog): return first_limit < message.created_at < second_limit def predicate_after(message: Message) -> bool: - """Check if the message is older than the first limit.""" + """Check if the message is younger than the first limit.""" return message.created_at > first_limit predicates = [] -- cgit v1.2.3