2024-07-06 11:30:37 -04:00
|
|
|
from typing import List, Union
|
|
|
|
|
|
|
|
import discord
|
|
|
|
from redbot.core import app_commands, commands
|
|
|
|
|
2024-07-12 15:22:24 -04:00
|
|
|
from ..models.type import Type, type_registry
|
2024-07-06 11:30:37 -04:00
|
|
|
from .config import config
|
|
|
|
from .utils import check_moddable
|
|
|
|
|
|
|
|
|
2024-07-06 13:03:59 -04:00
|
|
|
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:
|
2024-07-06 11:30:37 -04:00
|
|
|
"""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:
|
|
|
|
bot (Red): The bot instance.
|
|
|
|
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`.
|
2024-07-06 13:03:59 -04:00
|
|
|
target (discord.Member, discord.User, discord.abc.Messageable): The target user or channel to moderate.
|
2024-07-06 11:30:37 -04:00
|
|
|
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
|
2024-08-12 18:13:04 -04:00
|
|
|
if not await check_moddable(target=target, ctx=ctx, permissions=permissions, moderation_type=moderation_type):
|
2024-07-06 11:32:33 -04:00
|
|
|
return
|
|
|
|
if silent is None:
|
2024-08-12 18:13:04 -04:00
|
|
|
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
|
2024-07-06 11:30:37 -04:00
|
|
|
return await moderation_type.handler(
|
2024-08-12 18:13:04 -04:00
|
|
|
ctx=ctx,
|
|
|
|
target=target,
|
|
|
|
silent=silent,
|
2024-07-06 11:30:37 -04:00
|
|
|
**kwargs
|
|
|
|
)
|