1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 22:23:13 +00:00
Lumi/modules/moderation/cases.py
2024-08-29 09:42:47 -04:00

173 lines
5.9 KiB
Python

import asyncio
import discord
from discord.ext import commands
from reactionmenu import ViewButton, ViewMenu
from lib.case_handler import edit_case_modlog
from lib.const import CONST
from lib.exceptions import LumiException
from lib.format import format_case_number
from services.case_service import CaseService
from ui.cases import (
create_case_embed,
create_case_list_embed,
)
from ui.embeds import Builder
case_service = CaseService()
def create_no_cases_embed(ctx: commands.Context[commands.Bot], author_text: str, description: str) -> discord.Embed:
return Builder.create_embed(
theme="info",
user_name=ctx.author.name,
author_text=author_text,
description=description,
)
def create_case_view_menu(ctx: commands.Context[commands.Bot]) -> ViewMenu:
menu = ViewMenu(ctx, menu_type=ViewMenu.TypeEmbed, all_can_click=True, delete_on_timeout=True)
menu.add_button(
ViewButton(style=discord.ButtonStyle.secondary, custom_id=ViewButton.ID_GO_TO_FIRST_PAGE, emoji="⏮️"),
)
menu.add_button(
ViewButton(style=discord.ButtonStyle.secondary, custom_id=ViewButton.ID_PREVIOUS_PAGE, emoji=""),
)
menu.add_button(ViewButton(style=discord.ButtonStyle.secondary, custom_id=ViewButton.ID_NEXT_PAGE, emoji=""))
menu.add_button(
ViewButton(style=discord.ButtonStyle.secondary, custom_id=ViewButton.ID_GO_TO_LAST_PAGE, emoji="⏭️"),
)
return menu
class Cases(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.hybrid_command(name="case", aliases=["c", "ca"], description="View a specific case by number")
@commands.has_permissions(manage_messages=True)
async def view_case_by_number(self, ctx: commands.Context[commands.Bot], case_number: int) -> None:
guild_id = ctx.guild.id if ctx.guild else 0
case = case_service.fetch_case_by_guild_and_number(guild_id, case_number)
if not case:
embed = create_no_cases_embed(
ctx,
CONST.STRINGS["error_no_case_found_author"],
CONST.STRINGS["error_no_case_found_description"],
)
await ctx.send(embed=embed)
return
target = await commands.UserConverter().convert(ctx, str(case["target_id"]))
embed: discord.Embed = create_case_embed(
ctx=ctx,
target=target,
case_number=case["case_number"],
action_type=case["action_type"],
reason=case["reason"],
timestamp=case["created_at"],
duration=case["duration"] or None,
)
await ctx.send(embed=embed)
@commands.hybrid_command(name="cases", description="View all cases in the guild")
@commands.has_permissions(manage_messages=True)
async def view_all_cases_in_guild(self, ctx: commands.Context[commands.Bot]) -> None:
if not ctx.guild:
raise LumiException(CONST.STRINGS["error_not_in_guild"])
guild_id = ctx.guild.id
cases = case_service.fetch_cases_by_guild(guild_id)
if not cases:
embed = create_no_cases_embed(
ctx,
CONST.STRINGS["case_guild_no_cases_author"],
CONST.STRINGS["case_guild_no_cases"],
)
await ctx.send(embed=embed)
return
menu = create_case_view_menu(ctx)
for i in range(0, len(cases), 10):
chunk = cases[i : i + 10]
embed = create_case_list_embed(
ctx,
chunk,
CONST.STRINGS["case_guild_cases_author"],
)
menu.add_page(embed)
await menu.start()
@commands.hybrid_command(name="modcases", aliases=["mc", "modc"], description="View all cases in the guild")
@commands.has_permissions(manage_messages=True)
async def view_all_cases_by_mod(self, ctx: commands.Context[commands.Bot], moderator: discord.Member) -> None:
if not ctx.guild:
raise LumiException(CONST.STRINGS["error_not_in_guild"])
guild_id = ctx.guild.id
cases = case_service.fetch_cases_by_moderator(guild_id, moderator.id)
if not cases:
embed = create_no_cases_embed(
ctx,
CONST.STRINGS["case_mod_no_cases_author"],
CONST.STRINGS["case_mod_no_cases"],
)
await ctx.send(embed=embed)
return
menu = create_case_view_menu(ctx)
for i in range(0, len(cases), 10):
chunk = cases[i : i + 10]
embed = create_case_list_embed(
ctx,
chunk,
CONST.STRINGS["case_mod_cases_author"].format(moderator.name),
)
menu.add_page(embed)
await menu.start()
@commands.hybrid_command(name="editcase", aliases=["ec"], description="Edit the reason for a case")
@commands.has_permissions(manage_messages=True)
async def edit_case_reason(self, ctx: commands.Context[commands.Bot], case_number: int, *, new_reason: str):
if not ctx.guild:
raise LumiException(CONST.STRINGS["error_not_in_guild"])
guild_id = ctx.guild.id
case_service.edit_case_reason(
guild_id,
case_number,
new_reason,
)
embed = Builder.create_embed(
theme="success",
user_name=ctx.author.name,
author_text=CONST.STRINGS["case_reason_update_author"],
description=CONST.STRINGS["case_reason_update_description"].format(
format_case_number(case_number),
),
)
async def update_tasks():
await asyncio.gather(
ctx.send(embed=embed),
edit_case_modlog(ctx, guild_id, case_number, new_reason),
)
await update_tasks()
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Cases(bot))