[Mod] Allow admins to choose amount of repeats for "deleterepeats" (#2437)

* feat(mod): configurable amount of repeats for "deleterepeats"

resolves #2267

* fix(mod): check user input instead of changing it if it's invalid

* fix(mod): only purge cache when argument is valid

* perf(mod): fetch repeats from config only when guild not in cache
This commit is contained in:
jack1142
2019-04-23 16:01:20 +02:00
committed by Michael H
parent 47723cee33
commit 460b4bb3f2
4 changed files with 90 additions and 25 deletions

View File

@@ -1,3 +1,5 @@
from collections import defaultdict, deque
from redbot.core import commands, i18n, checks
from redbot.core.utils.chat_formatting import box
@@ -25,8 +27,10 @@ class ModSettings(MixinMeta):
delete_delay = await self.settings.guild(guild).delete_delay()
reinvite_on_unban = await self.settings.guild(guild).reinvite_on_unban()
msg = ""
msg += _("Delete repeats: {yes_or_no}\n").format(
yes_or_no=_("Yes") if delete_repeats else _("No")
msg += _("Delete repeats: {num_repeats}\n").format(
num_repeats=_("after {num} repeats").format(num=delete_repeats)
if delete_repeats != -1
else _("No")
)
msg += _("Ban mention spam: {num_mentions}\n").format(
num_mentions=_("{num} mentions").format(num=ban_mention_spam)
@@ -101,16 +105,45 @@ class ModSettings(MixinMeta):
@modset.command()
@commands.guild_only()
async def deleterepeats(self, ctx: commands.Context):
"""Enable auto-deletion of repeated messages."""
async def deleterepeats(self, ctx: commands.Context, repeats: int = None):
"""Enable auto-deletion of repeated messages.
Must be between 2 and 20.
Set to -1 to disable this feature.
"""
guild = ctx.guild
cur_setting = await self.settings.guild(guild).delete_repeats()
if not cur_setting:
await self.settings.guild(guild).delete_repeats.set(True)
await ctx.send(_("Messages repeated up to 3 times will be deleted."))
if repeats is not None:
if repeats == -1:
await self.settings.guild(guild).delete_repeats.set(repeats)
self.cache.pop(guild.id, None) # remove cache with old repeat limits
await ctx.send(_("Repeated messages will be ignored."))
elif 2 <= repeats <= 20:
await self.settings.guild(guild).delete_repeats.set(repeats)
# purge and update cache to new repeat limits
self.cache[guild.id] = defaultdict(lambda: deque(maxlen=repeats))
await ctx.send(
_("Messages repeated up to {num} times will be deleted.").format(num=repeats)
)
else:
await ctx.send(
_(
"Number of repeats must be between 2 and 20"
" or equal to -1 if you want to disable this feature!"
)
)
else:
await self.settings.guild(guild).delete_repeats.set(False)
await ctx.send(_("Repeated messages will be ignored."))
repeats = await self.settings.guild(guild).delete_repeats()
if repeats != -1:
await ctx.send(
_(
"Bot will delete repeated messages after"
" {num} repeats. Set this value to -1 to"
" ignore repeated messages"
).format(num=repeats)
)
else:
await ctx.send(_("Repeated messages will be ignored."))
@modset.command()
@commands.guild_only()