From 48259df18fa18b1d4a455749cd8cdcd90c8a38fa Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Tue, 13 Aug 2024 15:09:49 -0400 Subject: [PATCH] misc(aurora): move moderate() into the Aurora cog class --- aurora/aurora.py | 66 +++++++++++++++++++++++++++--------- aurora/utilities/moderate.py | 42 ----------------------- 2 files changed, 50 insertions(+), 58 deletions(-) delete mode 100644 aurora/utilities/moderate.py diff --git a/aurora/aurora.py b/aurora/aurora.py index 19a99c8..be455ca 100644 --- a/aurora/aurora.py +++ b/aurora/aurora.py @@ -11,7 +11,7 @@ import os import time from datetime import datetime, timedelta, timezone from math import ceil -from typing import List +from typing import List, Union import discord from class_registry.registry import RegistryKeyError @@ -32,13 +32,12 @@ from .menus.overrides import Overrides from .menus.types import Types from .models.change import Change from .models.moderation import Moderation -from .models.type import type_registry +from .models.type import Type, type_registry from .utilities.config import config, register_config from .utilities.factory import addrole_embed, case_factory, changes_factory, evidenceformat_factory, guild_embed, immune_embed, overrides_embed, type_embed from .utilities.json import dump from .utilities.logger import logger -from .utilities.moderate import moderate -from .utilities.utils import check_permissions, create_guild_table, log +from .utilities.utils import check_moddable, check_permissions, create_guild_table, log class Aurora(commands.Cog): @@ -47,7 +46,7 @@ class Aurora(commands.Cog): This cog stores all of its data in an SQLite database.""" __author__ = ["Seaswimmer"] - __version__ = "3.0.0-indev4" + __version__ = "3.0.0-indev5" __documentation__ = "https://seacogs.coastalcommits.com/aurora/" async def red_delete_data_for_user(self, *, requester, user_id: int): @@ -113,6 +112,41 @@ class Aurora(commands.Cog): async def cog_unload(self): self.handle_expiry.cancel() + @staticmethod + async def moderate(ctx: Union[commands.Context, discord.Interaction], target: discord.Member | discord.User | discord.abc.Messageable, silent: bool | None, permissions: List[str], moderation_type: Type | str, **kwargs) -> None | Type: + """This function is used to moderate users. + It checks if the target can be moderated, then calls the handler method of the moderation type specified. + + Args: + ctx (Union[commands.Context, discord.Interaction]): The context of the command. If this is a `discord.Interaction` object, it will be converted to a `commands.Context` object. Additionally, if the interaction orignated from a context menu, the `ctx.author` attribute will be overriden to `interaction.user`. + target (discord.Member, discord.User, discord.abc.Messageable): The target user or channel to moderate. + silent (bool | None): Whether to send the moderation action to the target. + permissions (List[str]): The permissions required to moderate the target. + moderation_type (Type): The moderation type (handler) to use. See `aurora.models.moderation_types` for some examples. + **kwargs: The keyword arguments to pass to the handler method. + """ + if isinstance(moderation_type, str): + moderation_type = type_registry[str.lower(moderation_type)] + if isinstance(ctx, discord.Interaction): + interaction = ctx + ctx = await commands.Context.from_interaction(interaction) + if isinstance(interaction.command, app_commands.ContextMenu): + ctx.author = interaction.user + if not await check_moddable(target=target, ctx=ctx, permissions=permissions, moderation_type=moderation_type): + return + if silent is None: + dm_users = await config.custom("types", ctx.guild.id, moderation_type.key).dm_users() + if dm_users is None: + dm_users = await config.guild(ctx.guild).dm_users() + silent = not dm_users + return await moderation_type.handler( + ctx=ctx, + target=target, + silent=silent, + **kwargs + ) + + @commands.Cog.listener("on_guild_join") async def db_generate_on_guild_join(self, guild: discord.Guild): """This method prepares the database schema whenever the bot joins a guild.""" @@ -214,7 +248,7 @@ class Aurora(commands.Cog): Why are you noting this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -241,7 +275,7 @@ class Aurora(commands.Cog): Why are you warning this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -274,7 +308,7 @@ class Aurora(commands.Cog): How long are you adding this role for? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -309,7 +343,7 @@ class Aurora(commands.Cog): How long are you removing this role for? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -341,7 +375,7 @@ class Aurora(commands.Cog): Why are you unbanning this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -369,7 +403,7 @@ class Aurora(commands.Cog): Why are you unmuting this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -396,7 +430,7 @@ class Aurora(commands.Cog): Why are you kicking this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -440,7 +474,7 @@ class Aurora(commands.Cog): silent: bool Should the user be messaged?""" if duration: - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -451,7 +485,7 @@ class Aurora(commands.Cog): delete_messages=delete_messages, ) else: - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -479,7 +513,7 @@ class Aurora(commands.Cog): Why are you unbanning this user? silent: bool Should the user be messaged?""" - await moderate( + await self.moderate( ctx=interaction, target=target, silent=silent, @@ -509,7 +543,7 @@ class Aurora(commands.Cog): if channel is None: channel = interaction.channel - await moderate( + await self.moderate( ctx=interaction, target=channel, silent=True, diff --git a/aurora/utilities/moderate.py b/aurora/utilities/moderate.py deleted file mode 100644 index bd4f793..0000000 --- a/aurora/utilities/moderate.py +++ /dev/null @@ -1,42 +0,0 @@ -from typing import List, Union - -import discord -from redbot.core import app_commands, commands - -from ..models.type import Type, type_registry -from .config import config -from .utils import check_moddable - - -async def moderate(ctx: Union[commands.Context, discord.Interaction], target: discord.Member | discord.User | discord.abc.Messageable, silent: bool | None, permissions: List[str], moderation_type: Type | str, **kwargs) -> None | Type: - """This function is used to moderate users. - It checks if the target can be moderated, then calls the handler method of the moderation type specified. - - Args: - ctx (Union[commands.Context, discord.Interaction]): The context of the command. If this is a `discord.Interaction` object, it will be converted to a `commands.Context` object. Additionally, if the interaction orignated from a context menu, the `ctx.author` attribute will be overriden to `interaction.user`. - target (discord.Member, discord.User, discord.abc.Messageable): The target user or channel to moderate. - silent (bool | None): Whether to send the moderation action to the target. - permissions (List[str]): The permissions required to moderate the target. - moderation_type (Type): The moderation type (handler) to use. See `aurora.models.moderation_types` for some examples. - **kwargs: The keyword arguments to pass to the handler method. - """ - if isinstance(moderation_type, str): - moderation_type = type_registry[str.lower(moderation_type)] - if isinstance(ctx, discord.Interaction): - interaction = ctx - ctx = await commands.Context.from_interaction(interaction) - if isinstance(interaction.command, app_commands.ContextMenu): - ctx.author = interaction.user - if not await check_moddable(target=target, ctx=ctx, permissions=permissions, moderation_type=moderation_type): - return - if silent is None: - dm_users = await config.custom("types", ctx.guild.id, moderation_type.key).dm_users() - if dm_users is None: - dm_users = await config.guild(ctx.guild).dm_users() - silent = not dm_users - return await moderation_type.handler( - ctx=ctx, - target=target, - silent=silent, - **kwargs - )