WIP: Moderation type registry #26
2 changed files with 44 additions and 23 deletions
|
@ -1090,7 +1090,7 @@ class Aurora(commands.Cog):
|
||||||
+ f"moderation_{interaction.guild.id}.json"
|
+ 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:
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
dump(obj=cases, fp=f, indent=2)
|
dump(obj=cases, fp=f, indent=2)
|
||||||
|
|
|
@ -202,42 +202,63 @@ class Moderation(AuroraGuildModel):
|
||||||
return cls.from_dict(bot=bot, data=case)
|
return cls.from_dict(bot=bot, data=case)
|
||||||
|
|
||||||
@classmethod
|
@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
|
from ..utilities.database import connect
|
||||||
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
if not parameters:
|
||||||
|
parameters = ()
|
||||||
with connect() as database:
|
with connect() as database:
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
cursor.execute(query, (moderation_id,))
|
cursor.execute(query, parameters=parameters)
|
||||||
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)
|
|
||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
if results:
|
if results:
|
||||||
cases = []
|
cases = []
|
||||||
for result in results:
|
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:
|
if case.moderation_id != 0:
|
||||||
cases.append(case)
|
cases.append(case)
|
||||||
return cases
|
return cases
|
||||||
|
|
||||||
return []
|
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
|
@classmethod
|
||||||
def log(
|
def log(
|
||||||
cls,
|
cls,
|
||||||
|
|
Loading…
Reference in a new issue