[V3 Cleanup] Refactor get_messages_for_deletion #1980

Closed
mikeshardmind wants to merge 6 commits from patch-13 into V3/develop
mikeshardmind commented 2018-08-05 02:53:06 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

This begins a more long-term fix. Part of that is changing a function signature, and as such, steps are being taken to do this with a proper warning for 3rd party devs using this utility function.

This is targeting beta 20, but promises changes in beta 21, and beta 22
PRs will be opened and connected to this.

### Type - [X] Bugfix - [ ] Enhancement - [ ] New feature ### Description of the changes This begins a more long-term fix. Part of that is changing a function signature, and as such, steps are being taken to do this with a proper warning for 3rd party devs using this utility function. This is targeting beta 20, but promises changes in beta 21, and beta 22 PRs will be opened and connected to this.
palmtree5 (Migrated from github.com) reviewed 2018-08-05 02:53:06 +02:00
aikaterna (Migrated from github.com) reviewed 2018-08-05 02:53:06 +02:00
aikaterna commented 2018-08-09 19:10:04 +02:00 (Migrated from github.com)

I've tried this change on a couple bots with no change in the bot's response, cleanup after still returns 0 messages deleted.

I've tried this change on a couple bots with no change in the bot's response, cleanup after still returns 0 messages deleted.
mikeshardmind commented 2018-08-09 19:46:13 +02:00 (Migrated from github.com)

This has been further changed to not break early, and filter the resultant list:

From the discord.py documentation:

"""
...
    reverse: bool
        If set to true, return messages in oldest->newest order. Recommended
        when using with "after" queries with limit over 100, otherwise messages
        will be out of order.
...
"""

This also (potentially) explains the previously inconsistent behavior. We couldn't use reverse=True with the previous loop strategy and an early break, and reverse=False breaking on a message date isn't safe due to this either.

This has been further changed to not break early, and filter the resultant list: From the discord.py documentation: ```py """ ... reverse: bool If set to true, return messages in oldest->newest order. Recommended when using with "after" queries with limit over 100, otherwise messages will be out of order. ... """ ``` This also (potentially) explains the previously inconsistent behavior. We couldn't use `reverse=True` with the previous loop strategy and an early break, and `reverse=False` breaking on a message date isn't safe due to this either.
Tobotimus (Migrated from github.com) requested changes 2018-08-11 06:14:16 +02:00
Tobotimus (Migrated from github.com) left a comment

This fixes [p]cleanup after but breaks all other cleanup commands.

You've also refactored get_messages_for_deletion which changes a lot of the behaviour unexpectedly. For example, the idea is to delete number of messages, whereas limit is the amount of messages to be collected at a time (confusing but we're just fixing one command here, we're not rewriting a cog)

This fixes `[p]cleanup after` but breaks all other cleanup commands. You've also refactored `get_messages_for_deletion` which changes a lot of the behaviour unexpectedly. For example, the idea is to delete `number` of messages, whereas `limit` is the amount of messages to be collected at a time (confusing but we're just fixing one command here, we're not rewriting a cog)
Tobotimus commented 2018-08-11 06:16:25 +02:00 (Migrated from github.com)

Why not just not use get_messages_for_deletion in this command? Just get all messages after the given message (as long as the message is less than 14 days ago), remove pinned ones (if requested), and delete_messages. Otherwise this helper method is just getting too messy and complicated.

Given that this PR is fixing a currently obselete command, I will merge it into beta 19 post-cutoff (we already are post-cutoff) as long as it doesn't affect the behaviour of the rest of the cog.

Why not just not use `get_messages_for_deletion` in this command? Just get all messages after the given message (as long as the message is less than 14 days ago), remove pinned ones (if requested), and `delete_messages`. Otherwise this helper method is just getting too messy and complicated. Given that this PR is fixing a currently obselete command, I will merge it into beta 19 post-cutoff (we already are post-cutoff) as long as it doesn't affect the behaviour of the rest of the cog.
mikeshardmind commented 2018-08-11 15:38:26 +02:00 (Migrated from github.com)

This shouldn't break any other cleanup command and the fact that it did was a fault of me not handling a specific issue with it. This should be fixed in this method, as it was made a static method specifically so that other things can use it too, we don't need to make a special casing for this, just need to handle it correctly.

This shouldn't break any other cleanup command and the fact that it did was a fault of me not handling a specific issue with it. This should be fixed in this method, as it was made a static method specifically so that other things can use it too, we don't need to make a special casing for this, just need to handle it correctly.
Tobotimus (Migrated from github.com) reviewed 2018-08-13 08:16:05 +02:00
@ -68,35 +72,55 @@ class Cleanup:
- The message is less than 14 days old
Tobotimus (Migrated from github.com) commented 2018-08-13 02:52:23 +02:00

I'd rather use ctx.message.created_at and do slightly less than 14 days (like 13 days, 23 hours and 55 minutes), since the system time could be out of sync with discord's, created_at should be correct since it uses the snowflake to calculate the time.

I'd rather use `ctx.message.created_at` and do slightly less than 14 days (like 13 days, 23 hours and 55 minutes), since the system time could be out of sync with discord's, `created_at` should be correct since it uses the snowflake to calculate the time.
mikeshardmind commented 2018-08-13 12:11:47 +02:00 (Migrated from github.com)

Because it's a timedelta, being out of sync on the base time isnt a
concern, and I'd prefer removing ctx as we don't need it, so it opens up
for event based usage without mocking an object.

I agree making it slightly under 14 days makes sense.

On Mon, Aug 13, 2018, 2:16 AM Toby Harradine notifications@github.com
wrote:

@Tobotimus commented on this pull request.

In redbot/cogs/cleanup/cleanup.py
https://github.com/Cog-Creators/Red-DiscordBot/pull/1980#discussion_r209474725
:

  •    while not too_old and len(to_delete) < number:
    
  •        message = None
    
  •        async for message in channel.history(limit=limit, before=before, after=after):
    
  •            if (
    
  •                (not number or len(to_delete) < number)
    
  •                and check(message)
    
  •                and (ctx.message.created_at - message.created_at).days < 14
    
  •                and (delete_pinned or not message.pinned)
    
  •            ):
    
  •                to_delete.append(message)
    
  •            elif (ctx.message.created_at - message.created_at).days >= 14:
    
  •                too_old = True
    
  •                break
    
  •            elif number and len(to_delete) >= number:
    
  •    two_weeks_ago = datetime.utcnow() - timedelta(days=14)
    

I'd rather use ctx.message.created_at and do slightly less than 14 days
(like 13 days, 23 hours and 55 minutes), since the system time could be out
of sync with discord's, created_at should be correct since it uses the
snowflake to calculate the time.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/Cog-Creators/Red-DiscordBot/pull/1980#pullrequestreview-145494377,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AVA9pLoW7Fpi1u4_Tk0SMepYg8_qkhA0ks5uQRmogaJpZM4VvMOA
.

Because it's a timedelta, being out of sync on the base time isnt a concern, and I'd prefer removing ctx as we don't need it, so it opens up for event based usage without mocking an object. I agree making it slightly under 14 days makes sense. On Mon, Aug 13, 2018, 2:16 AM Toby Harradine <notifications@github.com> wrote: > *@Tobotimus* commented on this pull request. > ------------------------------ > > In redbot/cogs/cleanup/cleanup.py > <https://github.com/Cog-Creators/Red-DiscordBot/pull/1980#discussion_r209474725> > : > > > - > - while not too_old and len(to_delete) < number: > - message = None > - async for message in channel.history(limit=limit, before=before, after=after): > - if ( > - (not number or len(to_delete) < number) > - and check(message) > - and (ctx.message.created_at - message.created_at).days < 14 > - and (delete_pinned or not message.pinned) > - ): > - to_delete.append(message) > - elif (ctx.message.created_at - message.created_at).days >= 14: > - too_old = True > - break > - elif number and len(to_delete) >= number: > + two_weeks_ago = datetime.utcnow() - timedelta(days=14) > > I'd rather use ctx.message.created_at and do slightly less than 14 days > (like 13 days, 23 hours and 55 minutes), since the system time could be out > of sync with discord's, created_at should be correct since it uses the > snowflake to calculate the time. > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/Cog-Creators/Red-DiscordBot/pull/1980#pullrequestreview-145494377>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AVA9pLoW7Fpi1u4_Tk0SMepYg8_qkhA0ks5uQRmogaJpZM4VvMOA> > . >
mikeshardmind commented 2018-08-13 13:35:25 +02:00 (Migrated from github.com)

Closing favoring #2013

Closing favoring #2013

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!1980
No description provided.