From d6467544666a0635da7d3eed22b2515dd7a2076f Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Mon, 3 Jun 2024 00:47:56 -0400 Subject: [PATCH] feat(aurora): added a bunch of functionality to the Moderation model --- aurora/aurora.py | 2 +- aurora/models/moderation.py | 65 ++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/aurora/aurora.py b/aurora/aurora.py index e4f9c2b..4497d40 100644 --- a/aurora/aurora.py +++ b/aurora/aurora.py @@ -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) diff --git a/aurora/models/moderation.py b/aurora/models/moderation.py index e5c852d..db9e681 100644 --- a/aurora/models/moderation.py +++ b/aurora/models/moderation.py @@ -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,