[Mod] Move deletedelay to core (#3638)

* Move deletedelay to core

* line lengths

* address review

* move settings change

* fine...
This commit is contained in:
TrustyJAID 2020-03-21 11:39:58 -06:00 committed by GitHub
parent d957e44e1e
commit b9f07e8684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 84 additions and 84 deletions

View File

@ -13,7 +13,6 @@ from redbot.core.utils._internal_utils import send_to_owners_with_prefix_replace
from .casetypes import CASETYPES
from .events import Events
from .kickban import KickBanMixin
from .movetocore import MoveToCore
from .mutes import MuteMixin
from .names import ModInfo
from .slowmode import Slowmode
@ -21,7 +20,7 @@ from .settings import ModSettings
_ = T_ = Translator("Mod", __file__)
__version__ = "1.1.0"
__version__ = "1.2.0"
class CompositeMetaClass(type(commands.Cog), type(ABC)):
@ -38,7 +37,6 @@ class Mod(
ModSettings,
Events,
KickBanMixin,
MoveToCore,
MuteMixin,
ModInfo,
Slowmode,
@ -111,8 +109,16 @@ class Mod(
"Please use `[p]moveignoredchannels` if "
"you were previously using these functions."
)
self.bot.loop.create_task(send_to_owners_with_prefix_replaced(self.bot, msg))
await self.settings.version.set(__version__)
self.bot.loop.create_task(self.bot.send_to_owners_with_prefix_replaced(msg))
await self.settings.version.set("1.1.0")
if await self.settings.version() < "1.2.0":
msg = _(
"Delete delay settings have been moved. "
"Please use `[p]movedeletedelay` if "
"you were previously using these functions."
)
self.bot.loop.create_task(self.bot.send_to_owners_with_prefix_replaced(msg))
await self.settings.version.set("1.2.0")
@commands.command()
@commands.is_owner()
@ -127,3 +133,17 @@ class Mod(
await self.bot._config.channel_from_id(channel_id).ignored.set(settings["ignored"])
await self.settings.channel_from_id(channel_id).clear()
await ctx.send(_("Ignored channels and guilds restored."))
@commands.command()
@commands.is_owner()
async def movedeletedelay(self, ctx: commands.Context) -> None:
"""
Move deletedelay settings to core
"""
all_guilds = await self.settings.all_guilds()
for guild_id, settings in all_guilds.items():
await self.bot._config.guild_from_id(guild_id).delete_delay.set(
settings["delete_delay"]
)
await self.settings.guild_from_id(guild_id).delete_delay.clear()
await ctx.send(_("Delete delay settings restored."))

View File

@ -1,49 +0,0 @@
import logging
import asyncio
import contextlib
import discord
from redbot.core import commands, checks, i18n
from redbot.core.utils.chat_formatting import box
from .abc import MixinMeta
log = logging.getLogger("red.mod")
_ = i18n.Translator("Mod", __file__)
# TODO: Empty this to core red.
class MoveToCore(MixinMeta):
"""
Mixin for things which should really not be in mod, but have not been moved out yet.
"""
@commands.Cog.listener()
async def on_command_completion(self, ctx: commands.Context):
await self._delete_delay(ctx)
@commands.Cog.listener()
async def on_command_error(self, ctx: commands.Context, error: Exception):
# Every message which isn't a command but which
# starts with a bot prefix is dispatched as a command error
if not isinstance(error, commands.CommandNotFound):
await self._delete_delay(ctx)
async def _delete_delay(self, ctx: commands.Context):
"""Currently used for:
* delete delay"""
guild = ctx.guild
if guild is None:
return
message = ctx.message
delay = await self.settings.guild(guild).delete_delay()
if delay == -1:
return
async def _delete_helper(m):
with contextlib.suppress(discord.HTTPException):
await m.delete()
log.debug("Deleted command msg {}".format(m.id))
await asyncio.sleep(delay)
await _delete_helper(message)

View File

@ -157,36 +157,6 @@ class ModSettings(MixinMeta):
else:
await ctx.send(_("Repeated messages will be ignored."))
@modset.command()
@commands.guild_only()
async def deletedelay(self, ctx: commands.Context, time: int = None):
"""Set the delay until the bot removes the command message.
Must be between -1 and 60.
Set to -1 to disable this feature.
"""
guild = ctx.guild
if time is not None:
time = min(max(time, -1), 60) # Enforces the time limits
await self.settings.guild(guild).delete_delay.set(time)
if time == -1:
await ctx.send(_("Command deleting disabled."))
else:
await ctx.send(_("Delete delay set to {num} seconds.").format(num=time))
else:
delay = await self.settings.guild(guild).delete_delay()
if delay != -1:
await ctx.send(
_(
"Bot will delete command messages after"
" {num} seconds. Set this value to -1 to"
" stop deleting messages"
).format(num=delay)
)
else:
await ctx.send(_("I will not delete command messages."))
@modset.command()
@commands.guild_only()
async def reinvite(self, ctx: commands.Context):

View File

@ -6,6 +6,7 @@ import platform
import re
import shutil
import sys
import contextlib
from collections import namedtuple
from datetime import datetime
from enum import IntEnum
@ -125,6 +126,7 @@ class RedBase(
fuzzy=False,
disabled_commands=[],
autoimmune_ids=[],
delete_delay=-1,
)
self._config.register_channel(embeds=None, ignored=False)
@ -1207,6 +1209,26 @@ class RedBase(
"""Wait until our post connection startup is done."""
await self._red_ready.wait()
async def _delete_delay(self, ctx: commands.Context):
"""Currently used for:
* delete delay"""
guild = ctx.guild
if guild is None:
return
message = ctx.message
delay = await self._config.guild(guild).delete_delay()
if delay == -1:
return
async def _delete_helper(m):
with contextlib.suppress(discord.HTTPException):
await m.delete()
log.debug("Deleted command msg {}".format(m.id))
await asyncio.sleep(delay)
await _delete_helper(message)
class Red(RedBase, discord.AutoShardedClient):
"""

View File

@ -912,6 +912,37 @@ class Core(commands.Cog, CoreLogic):
for page in pagify(settings):
await ctx.send(box(page))
@checks.guildowner_or_permissions(administrator=True)
@_set.command(name="deletedelay")
@commands.guild_only()
async def deletedelay(self, ctx: commands.Context, time: int = None):
"""Set the delay until the bot removes the command message.
Must be between -1 and 60.
Set to -1 to disable this feature.
"""
guild = ctx.guild
if time is not None:
time = min(max(time, -1), 60) # Enforces the time limits
await ctx.bot._config.guild(guild).delete_delay.set(time)
if time == -1:
await ctx.send(_("Command deleting disabled."))
else:
await ctx.send(_("Delete delay set to {num} seconds.").format(num=time))
else:
delay = await ctx.bot._config.guild(guild).delete_delay()
if delay != -1:
await ctx.send(
_(
"Bot will delete command messages after"
" {num} seconds. Set this value to -1 to"
" stop deleting messages"
).format(num=delay)
)
else:
await ctx.send(_("I will not delete command messages."))
@checks.is_owner()
@_set.command(name="description")
async def setdescription(self, ctx: commands.Context, *, description: str = ""):

View File

@ -134,6 +134,10 @@ def init_events(bot, cli_flags):
if outdated_red_message:
await bot.send_to_owners(outdated_red_message)
@bot.event
async def on_command_completion(ctx: commands.Context):
await bot._delete_delay(ctx)
@bot.event
async def on_command_error(ctx, error, unhandled_by_cog=False):
@ -144,6 +148,8 @@ def init_events(bot, cli_flags):
if ctx.cog:
if commands.Cog._get_overridden_method(ctx.cog.cog_command_error) is not None:
return
if not isinstance(error, commands.CommandNotFound):
await bot._delete_delay(ctx)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send_help()