From d375716fbf24f596bb44bfa40822c728cf7341dc Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 6 May 2024 15:29:48 -0400 Subject: [PATCH] feat(aurora): added a resolve function to the Moderation model --- aurora/models.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/aurora/models.py b/aurora/models.py index 396671e..2ad8de2 100644 --- a/aurora/models.py +++ b/aurora/models.py @@ -4,6 +4,7 @@ from datetime import datetime, timedelta from time import time from typing import Any, Dict, List, Literal, Optional, Union +import discord from discord import Forbidden, HTTPException, InvalidData, NotFound from pydantic import BaseModel, ConfigDict from redbot.core.bot import Red @@ -81,6 +82,50 @@ class Moderation(AuroraGuildModel): def __str__(self): return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}" + async def resolve(self, resolved_by: int, reason: str): + if self.resolved: + raise ValueError("Case is already resolved!") + + self.resolved = True + self.resolved_by = resolved_by + self.resolve_reason = reason + + if self.type == "MUTE": + try: + guild: discord.Guild = await self.bot.fetch_guild(self.guild_id) + member = await guild.fetch_member(self.target_id) + + await member.timeout( + None, reason=f"Case {self.moderation_id} resolved by {resolved_by}{" for" + reason if reason else ""}" + ) + except NotFound: + pass + + if self.type in ["BAN", "TEMPBAN"]: + try: + guild: discord.Guild = await self.bot.fetch_guild(self.guild_id) + await guild.unban(await self.get_target(), reason=f"Case {self.moderation_id} resolved by {resolved_by}{" for" + reason if reason else ""}") + except NotFound: + pass + + if not self.changes: + self.changes.append(Change.from_dict(self.bot, { + "type": "ORIGINAL", + "timestamp": self.timestamp, + "reason": self.reason, + "user_id": self.moderator_id, + "duration": self.duration, + "end_timestamp": self.end_timestamp, + })) + self.changes.append(Change.from_dict(self.bot, { + "type": "RESOLVE", + "timestamp": datetime.now(), + "reason": reason, + "user_id": resolved_by, + })) + + self.update() + def update(self): from aurora.utilities.database import connect from aurora.utilities.json import dumps