SeaCogs/aurora/models.py

46 lines
1.2 KiB
Python
Raw Normal View History

2024-05-04 13:41:11 -04:00
from typing import Dict, List, Optional
from discord import Guild
from pydantic import BaseModel
2024-05-04 13:42:58 -04:00
from aurora.utilities.database import connect
2024-05-04 13:41:11 -04:00
class Moderation(BaseModel):
moderation_id: int
timestamp: int
moderation_type: str
target_type: str
target_id: int
moderator_id: int
resolved: bool
expired: bool
duration: str
end_timestamp: int
reason: str
changes: List[Dict]
metadata: Dict
resolved_by: Optional[int] = None
resolve_reason: Optional[str] = None
role_id: Optional[int] = None
def __str__(self):
return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}"
@classmethod
def from_sql(cls, moderation_id: int, guild: Guild):
2024-05-04 13:41:11 -04:00
query = f"SELECT * FROM moderation_{guild.id} WHERE moderation_id = ?;"
2024-05-04 13:48:57 -04:00
with connect() as database:
cursor = database.cursor()
cursor.execute(query, (moderation_id,))
result = cursor.fetchone()
if result:
moderation_data = dict(zip(cls.model_fields, result))
moderation = cls(**moderation_data)
2024-05-04 13:48:57 -04:00
cursor.close()
return moderation
return None