feat(aurora): added a bunch of functionality to the Moderation model
Some checks failed
Actions / Build Documentation (MkDocs) (pull_request) Successful in 44s
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 1m11s

This commit is contained in:
Seaswimmer 2024-06-03 00:47:56 -04:00
parent 76f176d4cc
commit d646754466
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
2 changed files with 44 additions and 23 deletions

View file

@ -1090,7 +1090,7 @@ class Aurora(commands.Cog):
+ f"moderation_{interaction.guild.id}.json"
)
cases = Moderation.from_sql_all(interaction.client, interaction.guild.id)
cases = Moderation.get_all_cases(bot=interaction.client, guild_id=interaction.guild.id)
with open(filename, "w", encoding="utf-8") as f:
dump(obj=cases, fp=f, indent=2)

View file

@ -202,42 +202,63 @@ class Moderation(AuroraGuildModel):
return cls.from_dict(bot=bot, data=case)
@classmethod
def from_sql(cls, bot: Red, moderation_id: int, guild_id: int) -> "Moderation":
def execute(cls, bot: Red, query: str, parameters: tuple | None = None) -> List["Moderation"]:
from ..utilities.database import connect
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
if not parameters:
parameters = ()
with connect() as database:
cursor = database.cursor()
cursor.execute(query, (moderation_id,))
result = cursor.fetchone()
cursor.close()
if result and not moderation_id == 0:
return cls.from_result(bot, result, guild_id)
raise ValueError(f"Case {moderation_id} not found in moderation_{guild_id}!")
@classmethod
def from_sql_all(cls, bot: Red, guild_id: int) -> List["Moderation"]:
from ..utilities.database import connect
query = f"SELECT * FROM moderation_{guild_id};"
with connect() as database:
cursor = database.cursor()
cursor.execute(query)
cursor.execute(query, parameters=parameters)
results = cursor.fetchall()
cursor.close()
if results:
cases = []
for result in results:
case = cls.from_result(bot, result, guild_id)
case = cls.from_result(bot=bot, result=result)
if case.moderation_id != 0:
cases.append(case)
return cases
return []
@classmethod
def from_sql(cls, bot: Red, moderation_id: int, guild_id: int) -> "Moderation":
return cls.find_by_id(bot=bot, moderation_id=moderation_id, guild_id=guild_id)
@classmethod
def find_by_id(cls, bot: Red, moderation_id: int, guild_id: int) -> "Moderation":
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
case = cls.execute(bot=bot, query=query, parameters=(moderation_id,))
if case:
return case[0]
raise ValueError(f"Case {moderation_id} not found in moderation_{guild_id}!")
@classmethod
def find_by_target(cls, bot: Red, guild_id: int, target: int, types: list | None = None) -> List["Moderation"]:
query = f"SELECT * FROM moderation_{guild_id} WHERE target_id = ?"
if types:
query += f" AND moderation_type IN ({', '.join(['?' for _ in types])})"
query += " ORDER BY moderation_id DESC;"
return cls.execute(bot=bot, query=query, parameters=(target, *types) if types else (target,))
@classmethod
def find_by_moderator(cls, bot: Red, guild_id: int, moderator: int, types: list | None = None) -> List["Moderation"]:
query = f"SELECT * FROM moderation_{guild_id} WHERE moderator_id = ?"
if types:
query += f" AND moderation_type IN ({', '.join(['?' for _ in types])})"
query += " ORDER BY moderation_id DESC;"
return cls.execute(bot=bot, query=query, parameters=(moderator, *types) if types else (moderator,))
@classmethod
def get_all_cases(cls, bot: Red, guild_id: int, types: list | None = None) -> List["Moderation"]:
query = f"SELECT * FROM moderation_{guild_id}"
if types:
query += f" WHERE moderation_type IN ({', '.join(['?' for _ in types])})"
query += " ORDER BY moderation_id DESC;"
return cls.execute(bot=bot, query=query, parameters=tuple(iterable=types) if types else None)
@classmethod
def log(
cls,