WIP: Refactor Aurora (3.0.0) #29

Draft
cswimr wants to merge 347 commits from aurora-pydantic into main
Showing only changes of commit a4e11fd828 - Show all commits

View file

@ -82,48 +82,53 @@ class Moderation(AuroraGuildModel):
def update(self):
from aurora.utilities.database import connect
from aurora.utilities.json import dumps
query = f"UPDATE moderation_{self.guild_id} SET timestamp = ?, moderation_type = ?, target_type = ?, moderator_id = ?, role_id = ?, duration = ?, end_timestamp = ?, reason = ?, resolved = ?, resolved_by = ?, resolve_reason = ?, expired = ?, changes = ?, metadata = ? WHERE moderation_id = ?;"
with connect() as database:
cursor = database.cursor()
cursor.execute(query, (
self.timestamp,
self.timestamp.timestamp(),
self.moderation_type,
self.target_type,
self.moderator_id,
self.role_id,
self.duration,
self.end_timestamp,
str(self.duration) if self.duration else None,
self.end_timestamp.timestamp() if self.end_timestamp else None,
self.reason,
self.resolved,
self.resolved_by,
self.resolve_reason,
self.expired,
self.changes,
self.metadata,
self.moderation_id
dumps(self.changes),
dumps(self.metadata),
self.moderation_id,
))
cursor.close()
logger.info("Updated moderation case %s in guild %s with the following data:\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
self.moderation_id,
self.guild_id,
self.timestamp,
self.timestamp.timestamp(),
self.moderation_type,
self.target_type,
self.moderator_id,
self.role_id,
self.duration,
self.end_timestamp,
str(self.duration) if self.duration else None,
self.end_timestamp.timestamp() if self.end_timestamp else None,
self.reason,
self.resolved,
self.resolved_by,
self.resolve_reason,
self.expired,
self.changes,
self.metadata,
dumps(self.changes),
dumps(self.metadata),
)
@classmethod
def from_dict(cls, bot: Red, data: dict) -> "Moderation":
return cls(bot=bot, **data)
@classmethod
def from_sql(cls, bot: Red, moderation_id: int, guild_id: int) -> Optional["Moderation"]:
from aurora.utilities.database import connect
@ -141,10 +146,6 @@ class Moderation(AuroraGuildModel):
return None
@classmethod
def from_dict(cls, bot: Red, data: dict) -> "Moderation":
return cls(bot=bot, **data)
@classmethod
def log(
cls,
@ -207,67 +208,54 @@ class Moderation(AuroraGuildModel):
moderation_id = get_next_case_number(guild_id=guild_id, cursor=cursor)
case = {
"guild_id": guild_id,
"moderation_id": moderation_id,
"timestamp": timestamp,
"timestamp": timestamp.timestamp(),
"moderation_type": moderation_type,
"target_type": target_type,
"target_id": target_id,
"moderator_id": moderator_id,
"role_id": role_id,
"duration": duration,
"end_timestamp": end_timestamp,
"duration": str(duration) if duration else None,
"end_timestamp": end_timestamp.timestamp() if end_timestamp else None,
"reason": reason,
"resolved": resolved,
"resolved_by": resolved_by,
"resolve_reason": resolved_reason,
"expired": expired,
"changes": changes,
"metadata": metadata
"changes": dumps(changes),
"metadata": dumps(metadata)
}
case_safe = case.copy()
case_safe.pop("guild_id")
case_safe["duration"] = str(case_safe["duration"]) if case_safe["duration"] else None
case_safe["timestamp"] = case_safe["timestamp"].timestamp()
case_safe["end_timestamp"] = case_safe["end_timestamp"].timestamp() if case_safe["end_timestamp"] else None
case_safe["changes"] = dumps(case_safe["changes"])
case_safe["metadata"] = dumps(case_safe["metadata"])
sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(sql, tuple(case_safe.values()))
cursor.execute(sql, tuple(case.values()))
cursor.close()
database.commit()
if close_db:
database.close()
case_safe.update({"guild_id": guild_id})
logger.debug(
"Row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
guild_id,
case_safe["moderation_id"],
case_safe["timestamp"],
case_safe["moderation_type"],
case_safe["target_type"],
case_safe["target_id"],
case_safe["moderator_id"],
case_safe["role_id"],
case_safe["duration"],
case_safe["end_timestamp"],
case_safe["reason"],
case_safe["resolved"],
case_safe["resolved_by"],
case_safe["resolve_reason"],
case_safe["expired"],
case_safe["changes"],
case_safe["metadata"],
case["moderation_id"],
case["timestamp"],
case["moderation_type"],
case["target_type"],
case["target_id"],
case["moderator_id"],
case["role_id"],
case["duration"],
case["end_timestamp"],
case["reason"],
case["resolved"],
case["resolved_by"],
case["resolve_reason"],
case["expired"],
case["changes"],
case["metadata"],
)
for change in case["changes"]:
change.update({"bot": bot})
return cls.from_dict(bot=bot, data=case)
return cls.from_sql(bot=bot, moderation_id=moderation_id, guild_id=guild_id)
class Change(AuroraBaseModel):
type: Literal["ORIGINAL", "RESOLVE", "EDIT"]