Making DeleteRepeats having a custom value per channel #5607

Closed
YannPollet wants to merge 7 commits from V3/develop into V3/develop
YannPollet commented 2022-03-10 03:44:17 +01:00 (Migrated from github.com)

Resolves #4721

Description of the changes

I added the functionality to custom the number of allowed repeated messages before deleting it per channel.
The most important change of this feature is that the cache went from a map mapping the last messages from the author in the entire guild to a map mapping the messages of the authors per channel.
I also added in the utils file a method to create a new cache with taking into account the custom values per channel.

Have the changes in this PR been tested?

No, no tests have been added to the test files but the behaviour of the new implementation have been tested with scenario tests (and the tox tests still pass).

Resolves #4721 ### Description of the changes I added the functionality to custom the number of allowed repeated messages before deleting it per channel. The most important change of this feature is that the cache went from a map mapping the last messages from the author in the entire guild to a map mapping the messages of the authors per channel. I also added in the utils file a method to create a new cache with taking into account the custom values per channel. ### Have the changes in this PR been tested? <!-- Choose one (remove the line that doesn't apply): --> No, no tests have been added to the test files but the behaviour of the new implementation have been tested with scenario tests (and the tox tests still pass). <!-- If the question doesn't apply (for example, it's not a code change), choose Yes. Please respond to this question truthfully. We do not delay nor reject PRs based on the answer to this question but it allows to better review this PR. -->
Flame442 (Migrated from github.com) requested changes 2022-03-18 23:17:18 +01:00
Flame442 (Migrated from github.com) left a comment

This is not a complete review. The current implementation of the cache requires too many changes for the rest of the code to be effectively reviewed. I suggest you re-read Jack's comment on the original issue and ensure that you are following his advice in addition to reviewing the comments I have made.

This is not a complete review. The current implementation of the cache requires too many changes for the rest of the code to be effectively reviewed. I suggest you re-read Jack's comment on the original issue and ensure that you are following his advice in addition to reviewing the comments I have made.
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
Flame442 (Migrated from github.com) commented 2022-03-18 23:04:11 +01:00

The old cache only made config calls when it had not already been filled with data for a particular guild. Yours (inefficiently) does so on every message that is checked. You need to only call from config when needed.

The old cache only made config calls when it had not already been filled with data for a particular guild. Yours (inefficiently) does so on every message that is checked. You need to only call from config **when needed**.
Flame442 (Migrated from github.com) commented 2022-03-18 23:05:01 +01:00

You should not be leaving any print statements in the code. If there is something that should be displayed in console, you can use the logging module to do so like other parts of the code. This is not such a case however, so these lines should be removed.

You should not be leaving **any** `print` statements in the code. If there is something that should be displayed in console, you can use the `logging` module to do so like other parts of the code. This is not such a case however, so these lines should be removed.
Flame442 (Migrated from github.com) commented 2022-03-18 23:06:13 +01:00

This is not a fault of your PR, but while you are touching this function it is more efficient if this is the first thing checked instead of it being here.

This is not a fault of your PR, but while you are touching this function it is more efficient if this is the first thing checked instead of it being here.
Flame442 (Migrated from github.com) commented 2022-03-18 23:10:20 +01:00

I honestly cannot tell if it's safe to do this with all the different conditions for how guild_cache can be set up and how this line can be reached.

I honestly cannot tell if it's safe to do this with all the different conditions for how `guild_cache` can be set up and how this line can be reached.
Flame442 (Migrated from github.com) commented 2022-03-18 23:12:35 +01:00

This should be part of the channel scope, not a dict in the guild settings.

This should be part of the `channel` scope, not a dict in the `guild` settings.
Flame442 (Migrated from github.com) commented 2022-03-18 23:14:36 +01:00

The argument to specify the setting should only apply for a channel should be an optional discord.TextChannel (or perhaps a greedy one if we want to support passing multiple channels?). It should not be a string, and it should not lock the user into only picking the current channel.

The argument to specify the setting should only apply for a channel should be an optional `discord.TextChannel` (or perhaps a greedy one if we want to support passing multiple channels?). It should not be a string, and it should not lock the user into only picking the current channel.
Flame442 (Migrated from github.com) commented 2022-03-18 23:14:49 +01:00

See above comment about print.

See above comment about `print`.
@ -11,3 +13,21 @@ async def is_allowed_by_hierarchy(
return True
Flame442 (Migrated from github.com) commented 2022-03-18 23:15:10 +01:00

See above comment about print.

See above comment about `print`.
YannPollet (Migrated from github.com) reviewed 2022-03-23 19:53:35 +01:00
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
YannPollet (Migrated from github.com) commented 2022-03-23 19:53:35 +01:00

Could I have some more informations about this? Because I didn't change much about this part of the code except for the dict because I needed a dictionary per channel.

Could I have some more informations about this? Because I didn't change much about this part of the code except for the dict because I needed a dictionary per channel.
Flame442 (Migrated from github.com) reviewed 2022-03-23 20:03:06 +01:00
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
Flame442 (Migrated from github.com) commented 2022-03-23 20:03:06 +01:00

[Your PR code]

github.com/Cog-Creators/Red-DiscordBot@fe92cd88a2/redbot/cogs/mod/utils.py (L20-L24)

guild_cache may be a defaultdict[defaultdict[int]], though it is not guaranteed to be one. Thus, I can't tell if blindly accessing guild_cache[str(channel.id)][author] is safe to do from reading the code. This kind of complexity is a bad code smell, so the way this var is created and used should be rewritten to be clearer.


[Current code]

github.com/Cog-Creators/Red-DiscordBot@be00a59ab3/redbot/cogs/mod/events.py (L24-L29)

Notice that current code clearly checks whether the guild cache exists, and if it doesn't it clearly creates it, guaranteeing that after the if is done guild_cache will be a defaultdict[int]. I do not have the same confidence reading the code in this PR.

**[Your PR code]** https://github.com/Cog-Creators/Red-DiscordBot/blob/fe92cd88a2d22fe7935b356e8e914d2182de003d/redbot/cogs/mod/utils.py#L20-L24 `guild_cache` *may* be a `defaultdict[defaultdict[int]]`, though it is not guaranteed to be one. Thus, I can't tell if blindly accessing `guild_cache[str(channel.id)][author]` is safe to do from reading the code. This kind of complexity is a bad code smell, so the way this var is created and used should be rewritten to be clearer. --- **[Current code]** https://github.com/Cog-Creators/Red-DiscordBot/blob/be00a59ab3253146eb82cdff2a8100d0b6e4dba6/redbot/cogs/mod/events.py#L24-L29 Notice that current code clearly checks whether the guild cache exists, and if it doesn't it clearly creates it, guaranteeing that after the `if` is done `guild_cache` **will** be a `defaultdict[int]`. I do not have the same confidence reading the code in this PR.
YannPollet (Migrated from github.com) reviewed 2022-03-23 20:11:41 +01:00
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
YannPollet (Migrated from github.com) commented 2022-03-23 20:11:41 +01:00

Ok I understand and I already made the changes. It is possible to create a deque with maxlen = 0. Thus these checks aren't useful anymore and have been deleted

Ok I understand and I already made the changes. It is possible to create a deque with maxlen = 0. Thus these checks aren't useful anymore and have been deleted
Flame442 (Migrated from github.com) reviewed 2022-03-23 20:24:31 +01:00
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
Flame442 (Migrated from github.com) commented 2022-03-23 20:24:31 +01:00

Letting you know just in case, you have not pushed any changes to this PR since my review. If you're still working, and you mean that you made the changes locally, that's fine. I just can't see any changes so I can only base what I say on what I can see.

Letting you know just in case, you have not pushed any changes to this PR since my review. If you're still working, and you mean that you made the changes locally, that's fine. I just can't see any changes so I can only base what I say on what I can see.
YannPollet (Migrated from github.com) reviewed 2022-03-23 20:29:31 +01:00
@ -19,21 +19,20 @@ class Events(MixinMeta):
"""
YannPollet (Migrated from github.com) commented 2022-03-23 20:29:31 +01:00

Sorry for the misunderstanding, yes I changed it locally I still have to update my code with these reviews but I will push it in a few days

Sorry for the misunderstanding, yes I changed it locally I still have to update my code with these reviews but I will push it in a few days
Flame442 (Migrated from github.com) requested changes 2022-11-28 23:43:42 +01:00
Flame442 (Migrated from github.com) left a comment

This PR is looking a lot better than the first time I reviewed it. Still has quite a few things that need to be revised, but I can see it coming together now.

This PR is looking a lot better than the first time I reviewed it. Still has quite a few things that need to be revised, but I can see it coming together now.
Flame442 (Migrated from github.com) commented 2022-11-28 22:58:36 +01:00

Since this string now has a non-determinant length due to this loop, the eventual send of this command should be modified to ensure the resulting message is under 2000 characters, and to properly pagify the message if it is not.

Since this string now has a non-determinant length due to this loop, the eventual `send` of this command should be modified to ensure the resulting message is under 2000 characters, and to properly pagify the message if it is not.
Flame442 (Migrated from github.com) commented 2022-11-28 22:56:29 +01:00
            data = channel_data.get(channel.id, None)

If you aren't setting multiple different variables, there is no reason to use this syntax.

```suggestion data = channel_data.get(channel.id, None) ``` If you aren't setting multiple different variables, there is no reason to use this syntax.
@ -305,0 +329,4 @@
else:
success.add(channel)
if len(fails) > 0:
Flame442 (Migrated from github.com) commented 2022-11-28 22:51:21 +01:00
    async def deleterepeats(self, ctx: commands.Context, repeats: int = None, *channels: discord.TextChannel):

As I suggested in my previous review, it is far easier to just type hint the channels argument so dpy automatically converts it than to attempt to process it after the fact.

```suggestion async def deleterepeats(self, ctx: commands.Context, repeats: int = None, *channels: discord.TextChannel): ``` As I suggested in my previous review, it is **far** easier to just type hint the `channels` argument so dpy automatically converts it than to attempt to process it after the fact.
@ -308,2 +340,2 @@
self.cache.pop(guild.id, None) # remove cache with old repeat limits
await ctx.send(_("Repeated messages will be ignored."))
if len(channels) != 0:
channels_string = ""
Flame442 (Migrated from github.com) commented 2022-11-28 22:55:31 +01:00
        Channel specific overrides can be created by using this command with a passed in value for `channels`.

        If one or more channels are provided, `repeats` can be set to 0 to make those channels use the server default.

I don't think this is perfect, but it's at least a bit more descriptive than what is currently there.

```suggestion Channel specific overrides can be created by using this command with a passed in value for `channels`. If one or more channels are provided, `repeats` can be set to 0 to make those channels use the server default. ``` I don't think this is perfect, but it's at least a bit more descriptive than what is currently there.
@ -310,0 +341,4 @@
channels_string = ""
for channel in success:
channels_string += _("{name}, ").format(name=channel.name)
await self.config.channel(channel).delete_repeats.set(repeats)
Flame442 (Migrated from github.com) commented 2022-11-28 22:55:57 +01:00

This line should not be here, I assume this is left over from your local debugging.

```suggestion ``` This line should not be here, I assume this is left over from your local debugging.
@ -322,0 +366,4 @@
else:
await self.config.guild(guild).delete_repeats.set(repeats)
await ctx.send(
Flame442 (Migrated from github.com) commented 2022-11-28 23:25:47 +01:00

This entire block is not needed if you use the converter,

This entire block is not needed if you use the converter,
@ -322,0 +381,4 @@
# reset the cache for this channel
self.cache[guild.id].pop(channel.id, None)
else:
await ctx.send(
Flame442 (Migrated from github.com) commented 2022-11-28 23:36:55 +01:00
                    channels_string = ", ".join(channel.name for channel in success)
                    for channel in success:
                        await self.config.channel(channel).delete_repeats.set(repeats)
                        self.cache[guild.id][channel.id] = defaultdict(lambda: deque(maxlen=0))

                    msg = _("Repeated messages will be ignored for the following channels: {channels_string}").format(channels_string=channels_string)
                    await ctx.send(msg)

You were only updating the cache for the last guild currently, since it was setting outside the loop.

Translation strings (_("Text to translate here")) should only be used on hardcoded strings or strings with .format applied outside of the _(), not on strings that are concatenated from multiple variables, and can thus be dynamic at runtime (IE _(msg + channels_string + ")") should not be a translated string).

Note that I kept success as the variable to loop over, this variable will need to be changed to channels in all places you're using it once you change the parameter to convert to channels automatically. I just don't want to add suggestions to every usage of it, since there's quite a few :P

```suggestion channels_string = ", ".join(channel.name for channel in success) for channel in success: await self.config.channel(channel).delete_repeats.set(repeats) self.cache[guild.id][channel.id] = defaultdict(lambda: deque(maxlen=0)) msg = _("Repeated messages will be ignored for the following channels: {channels_string}").format(channels_string=channels_string) await ctx.send(msg) ``` You were only updating the cache for the last guild currently, since it was setting outside the loop. Translation strings (`_("Text to translate here")`) should only be used on hardcoded strings or strings with `.format` applied **outside** of the `_()`, **not** on strings that are concatenated from multiple variables, and can thus be dynamic at runtime (IE `_(msg + channels_string + ")")` should not be a translated string). Note that I kept `success` as the variable to loop over, this variable will need to be changed to `channels` in all places you're using it once you change the parameter to convert to channels automatically. I just don't want to add suggestions to every usage of it, since there's quite a few :P
Flame442 (Migrated from github.com) commented 2022-11-28 23:42:40 +01:00
                    channels_string = ", ".join(channel.name for channel in success)
                    for channel in success:
                        await self.config.channel(channel).delete_repeats.set(repeats)
                        self.cache[guild.id][channel.id] = defaultdict(
                            lambda: deque(maxlen=repeats)
                        )
                    msg = _(
                        "Messages repeated up to {num} times will be deleted for the following channels:  {channels_string}"
                    ).format(num=repeats, channels_string=channels_string)
                    await ctx.send(msg)
```suggestion channels_string = ", ".join(channel.name for channel in success) for channel in success: await self.config.channel(channel).delete_repeats.set(repeats) self.cache[guild.id][channel.id] = defaultdict( lambda: deque(maxlen=repeats) ) msg = _( "Messages repeated up to {num} times will be deleted for the following channels: {channels_string}" ).format(num=repeats, channels_string=channels_string) await ctx.send(msg) ```
@ -360,3 +426,3 @@
)
@modset.group()
@modset.command()
Flame442 (Migrated from github.com) commented 2022-11-28 23:39:59 +01:00

You don't have any output in this branch.

You don't have any output in this branch.
Flame442 (Migrated from github.com) commented 2022-11-28 23:05:29 +01:00

I'm not certain, but this file might be part of the public API, and if so these functions should either not be in this file or be private.

I'm not certain, but this file *might* be part of the public API, and if so these functions should either not be in this file or be private.
Jackenmen commented 2026-05-19 02:07:55 +02:00 (Migrated from github.com)

It's been over 3 years since the last changes on this PR were made, and there are still unresolved review comments on this PR. It's time to just close this and let someone else pick it up.

It's been over 3 years since the last changes on this PR were made, and there are still unresolved review comments on this PR. It's time to just close this and let someone else pick it up.

Pull request closed

Sign in to join this conversation.
No reviewers
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!5607
No description provided.