1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:23:12 +00:00

refactor moderation commands

This commit is contained in:
wlinator 2024-07-18 12:14:26 -04:00
parent 47fc88cc86
commit 509304ae28
6 changed files with 167 additions and 97 deletions

View file

@ -4,8 +4,8 @@ import discord
from lib import formatter
from lib.constants import CONST
from lib.embed_builder import EmbedBuilder
from modules.moderation import functions
from modules.moderation.case_handler import create_case
from modules.moderation.utils import actionable
from modules.moderation.utils.case_handler import create_case
from typing import Optional
@ -18,7 +18,7 @@ async def ban_user(cog, ctx, target: discord.User, reason: Optional[str] = None)
# member -> user is in the guild, check role hierarchy
if member:
bot_member = await cog.client.get_or_fetch_member(ctx.guild, ctx.bot.user.id)
functions.actionable(member, ctx.author, bot_member)
actionable.actionable(member, ctx.author, bot_member)
try:
await member.send(

View file

@ -1,94 +0,0 @@
import discord
from loguru import logger
from services.moderation.case_service import CaseService
from services.moderation.modlog_service import ModLogService
from lib.embed_builder import EmbedBuilder
from lib.constants import CONST
from typing import Optional
from discord.ext.commands import TextChannelConverter
case_service = CaseService()
modlog_service = ModLogService()
async def create_case(
ctx,
target: discord.User,
action_type: str,
reason: Optional[str] = None,
duration: Optional[int] = None,
expires_at: Optional[str] = None,
):
guild_id = ctx.guild.id
moderator_id = ctx.author.id
target_id = target.id
# Create the case
case_number: int = case_service.create_case(
guild_id=guild_id,
target_id=target_id,
moderator_id=moderator_id,
action_type=action_type,
reason=reason,
duration=duration,
expires_at=expires_at,
modlog_message_id=None,
)
logger.info(f"Created case {case_number} for {target.name} in guild {guild_id}")
# Send the case to the modlog if configured
mod_log_channel_id = modlog_service.fetch_modlog_channel_id(guild_id)
if mod_log_channel_id:
mod_log_channel = await TextChannelConverter().convert(
ctx,
str(mod_log_channel_id),
)
if mod_log_channel:
embed = EmbedBuilder.create_warning_embed(
ctx,
author_text=CONST.STRINGS["case_new_case_author"],
thumbnail_url=target.display_avatar.url,
show_name=False,
)
embed.add_field(
name=CONST.STRINGS["case_case_field"],
value=CONST.STRINGS["case_case_field_value"].format(case_number),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_type_field"],
value=CONST.STRINGS["case_type_field_value"].format(
action_type.lower().capitalize(),
),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_moderator_field"],
value=CONST.STRINGS["case_moderator_field_value"].format(
ctx.author.name,
),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_target_field"],
value=CONST.STRINGS["case_target_field_value"].format(target.name),
inline=False,
)
embed.add_field(
name=CONST.STRINGS["case_reason_field"],
value=CONST.STRINGS["case_reason_field_value"].format(
reason or CONST.STRINGS["mod_no_reason"],
),
inline=False,
)
message = await mod_log_channel.send(embed=embed)
# Update the case with the modlog_message_id
case_service.edit_case(
guild_id=guild_id,
case_number=case_number,
changes={"modlog_message_id": message.id},
)

View file

@ -0,0 +1,54 @@
from discord.ext import pages
from services.moderation.case_service import CaseService
case_service = CaseService()
async def view_case_by_number(ctx, guild_id: int, case_number: int):
case = case_service.fetch_case_by_guild_and_number(guild_id, case_number)
if not case:
return await ctx.send("No case found with that ID.")
await ctx.send(
f"Case {case['case_number']}: {case['action_type']} - {case['reason']}",
)
async def view_all_cases_in_guild(ctx, guild_id: int):
cases = case_service.fetch_all_cases_in_guild(guild_id)
if not cases:
return await ctx.send("No cases found for this guild.")
pages_list = [
f"Case {case['case_number']}: {case['action_type']} - {case['reason']}"
for case in cases
]
paginator = pages.Paginator(pages=pages_list, loop_pages=True)
await paginator.send(ctx)
async def view_all_cases_by_mod(ctx, guild_id: int, mod_id: int):
cases = case_service.fetch_all_cases_by_mod(guild_id, mod_id)
if not cases:
return await ctx.send("No cases found for this moderator in this guild.")
pages_list = [
f"Case {case['case_number']}: {case['action_type']} - {case['reason']}"
for case in cases
]
paginator = pages.Paginator(pages=pages_list, loop_pages=True)
await paginator.send(ctx)
async def edit_case_reason(ctx, guild_id: int, case_number: int, new_reason: str):
changes = {"reason": new_reason}
case_service.edit_case(guild_id, case_number, changes)
await ctx.respond(f"Case {case_number} reason updated to: {new_reason}")
async def close_case(ctx, guild_id: int, case_number: int):
case_service.close_case(guild_id, case_number)
await ctx.respond(f"Case {case_number} has been closed.")

View file

@ -0,0 +1,51 @@
import discord
from lib.embed_builder import EmbedBuilder
from lib.constants import CONST
from typing import Optional
def create_case_embed(
ctx,
target: discord.User,
case_number: int,
action_type: str,
reason: Optional[str],
) -> discord.Embed:
embed = EmbedBuilder.create_warning_embed(
ctx,
author_text=CONST.STRINGS["case_new_case_author"],
thumbnail_url=target.display_avatar.url,
show_name=False,
)
embed.add_field(
name=CONST.STRINGS["case_case_field"],
value=CONST.STRINGS["case_case_field_value"].format(case_number),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_type_field"],
value=CONST.STRINGS["case_type_field_value"].format(
action_type.lower().capitalize(),
),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_moderator_field"],
value=CONST.STRINGS["case_moderator_field_value"].format(
ctx.author.name,
),
inline=True,
)
embed.add_field(
name=CONST.STRINGS["case_target_field"],
value=CONST.STRINGS["case_target_field_value"].format(target.name),
inline=False,
)
embed.add_field(
name=CONST.STRINGS["case_reason_field"],
value=CONST.STRINGS["case_reason_field_value"].format(
reason or CONST.STRINGS["mod_no_reason"],
),
inline=False,
)
return embed

View file

@ -0,0 +1,59 @@
import discord
from loguru import logger
from services.moderation.case_service import CaseService
from services.moderation.modlog_service import ModLogService
from modules.moderation.utils.case_embed import create_case_embed
from typing import Optional
from discord.ext.commands import TextChannelConverter
case_service = CaseService()
modlog_service = ModLogService()
async def create_case(
ctx,
target: discord.User,
action_type: str,
reason: Optional[str] = None,
duration: Optional[int] = None,
expires_at: Optional[str] = None,
):
guild_id = ctx.guild.id
moderator_id = ctx.author.id
target_id = target.id
# Create the case
case_number: int = case_service.create_case(
guild_id=guild_id,
target_id=target_id,
moderator_id=moderator_id,
action_type=action_type,
reason=reason,
duration=duration,
expires_at=expires_at,
modlog_message_id=None,
)
logger.info(f"Created case {case_number} for {target.name} in guild {guild_id}")
# Send the case to the modlog if configured
mod_log_channel_id = modlog_service.fetch_modlog_channel_id(guild_id)
if mod_log_channel_id:
try:
mod_log_channel = await TextChannelConverter().convert(
ctx,
str(mod_log_channel_id),
)
embed = create_case_embed(ctx, target, case_number, action_type, reason)
message = await mod_log_channel.send(embed=embed)
# Update the case with the modlog_message_id
case_service.edit_case(
guild_id=guild_id,
case_number=case_number,
changes={"modlog_message_id": message.id},
)
except Exception as e:
logger.error(f"Failed to send case to modlog channel: {e}")