WIP: Refactor Aurora (3.0.0) #29
2 changed files with 44 additions and 23 deletions
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue