WIP: Refactor Aurora (3.0.0) #29

Draft
cswimr wants to merge 347 commits from aurora-pydantic into main
5 changed files with 124 additions and 4 deletions
Showing only changes of commit 9c345ed96b - Show all commits

View file

@ -15,6 +15,7 @@ from math import ceil
import discord
from dateutil.parser import ParserError, parse
from discord.ext import tasks
from menus.types import Types
from redbot.core import app_commands, commands, data_manager
from redbot.core.app_commands import Choice
from redbot.core.bot import Red
@ -31,7 +32,7 @@ from .models.change import Change
from .models.moderation import Moderation
from .models.type import 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
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
@ -44,7 +45,7 @@ class Aurora(commands.Cog):
This cog stores all of its data in an SQLite database."""
__author__ = ["SeaswimmerTheFsh"]
__version__ = "2.4.1"
__version__ = "2.4.2"
__documentation__ = "https://seacogs.coastalcommits.com/aurora/"
async def red_delete_data_for_user(self, *, requester, user_id: int):
@ -1115,6 +1116,21 @@ class Aurora(commands.Cog):
msg = await ctx.send(embed=await guild_embed(ctx))
await msg.edit(view=Guild(ctx, msg, 60))
@aurora_settings.command(name="type")
@commands.admin_or_permissions(manage_guild=True)
@commands.guild_only()
async def aurora_settings_type(self, ctx: commands.Context, moderation_type: str):
"""Manage configuration options for specific moderation types.
See [the documentation](https://seacogs.coastalcommits.com/Aurora/Types) for a list of built-in moderation types."""
registered_type = type_registry.get(moderation_type)
if not registered_type:
types = "`, `".join(type_registry.keys())
await ctx.send(error("`moderation_type` is not a valid moderation type.\nValid types are:\n" + types))
return
msg = await ctx.send(embed=await type_embed(ctx, registered_type))
await msg.edit(view=Types(ctx, msg, registered_type))
@aurora_settings.command(name="addrole", aliases=["removerole"])
@commands.admin_or_permissions(manage_guild=True)
@commands.guild_only()

58
aurora/menus/types.py Normal file
View file

@ -0,0 +1,58 @@
from discord import ButtonStyle, Interaction, Message, ui
from redbot.core import commands
from ..models.type import Type
from ..utilities.config import config
from ..utilities.factory import type_embed
class Types(ui.View):
def __init__(self, ctx: commands.Context, message: Message, moderation_type: Type, timeout: int | None = None):
super().__init__()
self.ctx = ctx
self.message = message
self.type = moderation_type
self.timeout = timeout
async def on_timeout(self):
await self.message.edit(view=None)
@ui.button(label="Show in History", style=ButtonStyle.green, row=0)
async def show_in_history(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.custom("type", interaction.guild.id, self.type.key).show_in_history()
await config.custom("type", interaction.guild.id, self.type.key).show_in_history.set(not current_setting)
await interaction.message.edit(embed=await type_embed(self.ctx, self.type))
@ui.button(label="Show Moderator", style=ButtonStyle.green, row=0)
async def show_moderator(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.custom("type", interaction.guild.id, self.type.key).show_moderator()
await config.custom("type", interaction.guild.id, self.type.key).show_moderator.set(not current_setting)
await interaction.message.edit(embed=await type_embed(self.ctx, self.type))
@ui.button(label="Use Discord Permissions", style=ButtonStyle.green, row=0)
async def use_discord_permissions(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.custom("type", interaction.guild.id, self.type.key).use_discord_permissions()
await config.custom("type", interaction.guild.id, self.type.key).use_discord_permissions.set(not current_setting)
await interaction.message.edit(embed=await type_embed(self.ctx, self.type))
@ui.button(label="DM Users", style=ButtonStyle.green, row=0)
async def dm_users(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must have the manage guild permission to change this setting.", ephemeral=True)
return
await interaction.response.defer()
current_setting = await config.custom("type", interaction.guild.id, self.type.key).dm_users()
await config.custom("type", interaction.guild.id, self.type.key).dm_users.set(not current_setting)
await interaction.message.edit(embed=await type_embed(self.ctx, self.type))

View file

@ -1,12 +1,12 @@
from abc import abstractmethod
from typing import Any, List, Tuple
from typing import Any, Dict, Tuple
from class_registry import AutoRegister, ClassRegistry
from discord import Interaction, Member, User
from discord.abc import Messageable
from redbot.core import commands
type_registry: List['Type'] = ClassRegistry(attr_name='key', unique=True)
type_registry: Dict['str', 'Type'] = ClassRegistry(attr_name='key', unique=True)
class Type(metaclass=AutoRegister(type_registry)):
"""This is a base class for moderation types.

View file

@ -27,3 +27,13 @@ def register_config(config_obj: Config):
history_inline_pagesize=None,
auto_evidenceformat=None,
)
moderation_type = {
"show_in_history": bool,
"show_moderator": bool,
"use_discord_permissions": bool,
"dm_users": bool,
}
config_obj.init_custom("type", 2)
config_obj.register_custom("type", **moderation_type)

View file

@ -544,3 +544,39 @@ async def immune_embed(ctx: commands.Context) -> Embed:
e.description += "\n\n" + immune_str
return e
async def type_embed(ctx: commands.Context, moderation_type = Type) -> Embed:
"""Generates a configuration menu field value for a guild's settings."""
type_settings = {
"show_in_history": await config.custom("type", ctx.guild.id, moderation_type.key).show_in_history(),
"show_moderator": await config.custom("type", ctx.guild.id, moderation_type.key).show_moderator(),
"use_discord_permissions": await config.custom("type", ctx.guild.id, moderation_type.key).use_discord_permissions(),
"dm_users": await config.custom("type", ctx.guild.id, moderation_type.key).dm_users(),
}
guild_str = [
"- "
+ bold("Show in History: ")
+ get_bool_emoji(type_settings["show_in_history"]),
"- "
+ bold("Show Moderator: ")
+ get_bool_emoji(type_settings["show_moderator"]),
"- "
+ bold("Use Discord Permissions: ")
+ get_bool_emoji(type_settings["use_discord_permissions"]),
"- "
+ bold("DM Users: ")
+ get_bool_emoji(type_settings["dm_users"]),
]
guild_str = "\n".join(guild_str)
e = await _config(ctx)
e.title += ": Server Configuration"
e.description = (
"""
Use the buttons below to manage Aurora's server configuration.\n
"""
+ guild_str
)
return e