2024-05-04 14:08:53 -04:00
|
|
|
import json
|
|
|
|
from datetime import datetime, timedelta
|
2024-05-04 13:41:11 -04:00
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2024-05-04 13:42:58 -04:00
|
|
|
|
2024-05-04 13:41:11 -04:00
|
|
|
|
|
|
|
class Moderation(BaseModel):
|
|
|
|
moderation_id: int
|
2024-05-04 14:08:53 -04:00
|
|
|
guild_id: int
|
|
|
|
timestamp: datetime
|
2024-05-04 13:41:11 -04:00
|
|
|
moderation_type: str
|
|
|
|
target_type: str
|
|
|
|
target_id: int
|
|
|
|
moderator_id: int
|
2024-05-04 14:08:53 -04:00
|
|
|
role_id: Optional[int]
|
|
|
|
duration: Optional[timedelta]
|
|
|
|
end_timestamp: Optional[datetime]
|
|
|
|
reason: Optional[str]
|
2024-05-04 13:41:11 -04:00
|
|
|
resolved: bool
|
2024-05-04 14:08:53 -04:00
|
|
|
resolved_by: Optional[int]
|
|
|
|
resolve_reason: Optional[str]
|
2024-05-04 13:41:11 -04:00
|
|
|
expired: bool
|
|
|
|
changes: List[Dict]
|
|
|
|
metadata: Dict
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}"
|
|
|
|
|
2024-05-04 13:47:07 -04:00
|
|
|
@classmethod
|
2024-05-04 14:08:53 -04:00
|
|
|
def from_sql(cls, moderation_id: int, guild_id: int):
|
|
|
|
from aurora.utilities.database import connect
|
|
|
|
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
2024-05-04 13:47:07 -04:00
|
|
|
|
2024-05-04 13:48:57 -04:00
|
|
|
with connect() as database:
|
|
|
|
cursor = database.cursor()
|
2024-05-04 13:47:07 -04:00
|
|
|
cursor.execute(query, (moderation_id,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
|
|
|
|
if result:
|
2024-05-04 14:51:22 -04:00
|
|
|
if result[7] is not None:
|
2024-05-04 14:08:53 -04:00
|
|
|
hours, minutes, seconds = map(int, result[7].split(':'))
|
|
|
|
duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
|
|
else:
|
|
|
|
duration = None
|
|
|
|
case = {
|
|
|
|
"moderation_id": int(result[0]),
|
|
|
|
"guild_id": int(guild_id),
|
|
|
|
"timestamp": datetime.fromtimestamp(result[1]),
|
|
|
|
"moderation_type": str(result[2]),
|
|
|
|
"target_type": str(result[3]),
|
|
|
|
"target_id": int(result[4]),
|
|
|
|
"moderator_id": int(result[5]),
|
2024-05-04 14:53:04 -04:00
|
|
|
"role_id": int(result[6]) if result[6] is not None else None,
|
2024-05-04 14:08:53 -04:00
|
|
|
"duration": duration,
|
2024-05-04 14:53:04 -04:00
|
|
|
"end_timestamp": datetime.fromtimestamp(result[8]) if result[8] is not None else None,
|
|
|
|
"reason": result[9],
|
2024-05-04 14:08:53 -04:00
|
|
|
"resolved": bool(result[10]),
|
2024-05-04 14:53:04 -04:00
|
|
|
"resolved_by": result[11],
|
|
|
|
"resolve_reason": result[12],
|
2024-05-04 14:08:53 -04:00
|
|
|
"expired": bool(result[13]),
|
2024-05-04 14:55:17 -04:00
|
|
|
"changes": result[14] if result[14] else [],
|
|
|
|
"metadata": result[15] if result[15] else {},
|
2024-05-04 14:08:53 -04:00
|
|
|
}
|
|
|
|
|
2024-05-04 13:48:57 -04:00
|
|
|
cursor.close()
|
2024-05-04 14:08:53 -04:00
|
|
|
|
|
|
|
return cls(**case)
|
2024-05-04 13:47:07 -04:00
|
|
|
|
|
|
|
return None
|
2024-05-04 14:18:19 -04:00
|
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
|
|
def default(self, o):
|
|
|
|
if isinstance(o, datetime):
|
2024-05-04 14:19:48 -04:00
|
|
|
return int(o.timestamp())
|
2024-05-04 14:18:19 -04:00
|
|
|
if isinstance(o, timedelta):
|
|
|
|
return str(o)
|
2024-05-04 14:49:07 -04:00
|
|
|
if isinstance(o, Moderation):
|
|
|
|
return o.model_dump()
|
|
|
|
if isinstance(o, None):
|
|
|
|
return "NULL"
|
2024-05-04 14:18:19 -04:00
|
|
|
return super().default(o)
|