misc(aurora): move moderate() into the Aurora cog class
Some checks failed
Actions / Build Documentation (MkDocs) (pull_request) Successful in 28s
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 41s

This commit is contained in:
Seaswimmer 2024-08-13 15:09:49 -04:00
parent 233e6ac908
commit 48259df18f
Signed by: cswimr
GPG key ID: 3813315477F26F82
2 changed files with 50 additions and 58 deletions

View file

@ -11,7 +11,7 @@ import os
import time import time
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from math import ceil from math import ceil
from typing import List from typing import List, Union
import discord import discord
from class_registry.registry import RegistryKeyError from class_registry.registry import RegistryKeyError
@ -32,13 +32,12 @@ from .menus.overrides import Overrides
from .menus.types import Types from .menus.types import Types
from .models.change import Change from .models.change import Change
from .models.moderation import Moderation 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.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.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.json import dump
from .utilities.logger import logger from .utilities.logger import logger
from .utilities.moderate import moderate from .utilities.utils import check_moddable, check_permissions, create_guild_table, log
from .utilities.utils import check_permissions, create_guild_table, log
class Aurora(commands.Cog): class Aurora(commands.Cog):
@ -47,7 +46,7 @@ class Aurora(commands.Cog):
This cog stores all of its data in an SQLite database.""" This cog stores all of its data in an SQLite database."""
__author__ = ["Seaswimmer"] __author__ = ["Seaswimmer"]
__version__ = "3.0.0-indev4" __version__ = "3.0.0-indev5"
__documentation__ = "https://seacogs.coastalcommits.com/aurora/" __documentation__ = "https://seacogs.coastalcommits.com/aurora/"
async def red_delete_data_for_user(self, *, requester, user_id: int): 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): async def cog_unload(self):
self.handle_expiry.cancel() 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") @commands.Cog.listener("on_guild_join")
async def db_generate_on_guild_join(self, guild: discord.Guild): async def db_generate_on_guild_join(self, guild: discord.Guild):
"""This method prepares the database schema whenever the bot joins a 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? Why are you noting this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -241,7 +275,7 @@ class Aurora(commands.Cog):
Why are you warning this user? Why are you warning this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -274,7 +308,7 @@ class Aurora(commands.Cog):
How long are you adding this role for? How long are you adding this role for?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -309,7 +343,7 @@ class Aurora(commands.Cog):
How long are you removing this role for? How long are you removing this role for?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -341,7 +375,7 @@ class Aurora(commands.Cog):
Why are you unbanning this user? Why are you unbanning this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -369,7 +403,7 @@ class Aurora(commands.Cog):
Why are you unmuting this user? Why are you unmuting this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -396,7 +430,7 @@ class Aurora(commands.Cog):
Why are you kicking this user? Why are you kicking this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -440,7 +474,7 @@ class Aurora(commands.Cog):
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
if duration: if duration:
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -451,7 +485,7 @@ class Aurora(commands.Cog):
delete_messages=delete_messages, delete_messages=delete_messages,
) )
else: else:
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -479,7 +513,7 @@ class Aurora(commands.Cog):
Why are you unbanning this user? Why are you unbanning this user?
silent: bool silent: bool
Should the user be messaged?""" Should the user be messaged?"""
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=target, target=target,
silent=silent, silent=silent,
@ -509,7 +543,7 @@ class Aurora(commands.Cog):
if channel is None: if channel is None:
channel = interaction.channel channel = interaction.channel
await moderate( await self.moderate(
ctx=interaction, ctx=interaction,
target=channel, target=channel,
silent=True, silent=True,

View file

@ -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
)