WIP: Moderation type registry #26
1 changed files with 39 additions and 51 deletions
|
@ -82,48 +82,53 @@ class Moderation(AuroraGuildModel):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
from aurora.utilities.database import connect
|
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 = ?;"
|
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:
|
with connect() as database:
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
cursor.execute(query, (
|
cursor.execute(query, (
|
||||||
self.timestamp,
|
self.timestamp.timestamp(),
|
||||||
self.moderation_type,
|
self.moderation_type,
|
||||||
self.target_type,
|
self.target_type,
|
||||||
self.moderator_id,
|
self.moderator_id,
|
||||||
self.role_id,
|
self.role_id,
|
||||||
self.duration,
|
str(self.duration) if self.duration else None,
|
||||||
self.end_timestamp,
|
self.end_timestamp.timestamp() if self.end_timestamp else None,
|
||||||
self.reason,
|
self.reason,
|
||||||
self.resolved,
|
self.resolved,
|
||||||
self.resolved_by,
|
self.resolved_by,
|
||||||
self.resolve_reason,
|
self.resolve_reason,
|
||||||
self.expired,
|
self.expired,
|
||||||
self.changes,
|
dumps(self.changes),
|
||||||
self.metadata,
|
dumps(self.metadata),
|
||||||
self.moderation_id
|
self.moderation_id,
|
||||||
))
|
))
|
||||||
cursor.close()
|
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",
|
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.moderation_id,
|
||||||
self.guild_id,
|
self.guild_id,
|
||||||
self.timestamp,
|
self.timestamp.timestamp(),
|
||||||
self.moderation_type,
|
self.moderation_type,
|
||||||
self.target_type,
|
self.target_type,
|
||||||
self.moderator_id,
|
self.moderator_id,
|
||||||
self.role_id,
|
self.role_id,
|
||||||
self.duration,
|
str(self.duration) if self.duration else None,
|
||||||
self.end_timestamp,
|
self.end_timestamp.timestamp() if self.end_timestamp else None,
|
||||||
self.reason,
|
self.reason,
|
||||||
self.resolved,
|
self.resolved,
|
||||||
self.resolved_by,
|
self.resolved_by,
|
||||||
self.resolve_reason,
|
self.resolve_reason,
|
||||||
self.expired,
|
self.expired,
|
||||||
self.changes,
|
dumps(self.changes),
|
||||||
self.metadata,
|
dumps(self.metadata),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_dict(cls, bot: Red, data: dict) -> "Moderation":
|
||||||
|
return cls(bot=bot, **data)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_sql(cls, bot: Red, moderation_id: int, guild_id: int) -> Optional["Moderation"]:
|
def from_sql(cls, bot: Red, moderation_id: int, guild_id: int) -> Optional["Moderation"]:
|
||||||
from aurora.utilities.database import connect
|
from aurora.utilities.database import connect
|
||||||
|
@ -141,10 +146,6 @@ class Moderation(AuroraGuildModel):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, bot: Red, data: dict) -> "Moderation":
|
|
||||||
return cls(bot=bot, **data)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def log(
|
def log(
|
||||||
cls,
|
cls,
|
||||||
|
@ -207,67 +208,54 @@ class Moderation(AuroraGuildModel):
|
||||||
moderation_id = get_next_case_number(guild_id=guild_id, cursor=cursor)
|
moderation_id = get_next_case_number(guild_id=guild_id, cursor=cursor)
|
||||||
|
|
||||||
case = {
|
case = {
|
||||||
"guild_id": guild_id,
|
|
||||||
"moderation_id": moderation_id,
|
"moderation_id": moderation_id,
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp.timestamp(),
|
||||||
"moderation_type": moderation_type,
|
"moderation_type": moderation_type,
|
||||||
"target_type": target_type,
|
"target_type": target_type,
|
||||||
"target_id": target_id,
|
"target_id": target_id,
|
||||||
"moderator_id": moderator_id,
|
"moderator_id": moderator_id,
|
||||||
"role_id": role_id,
|
"role_id": role_id,
|
||||||
"duration": duration,
|
"duration": str(duration) if duration else None,
|
||||||
"end_timestamp": end_timestamp,
|
"end_timestamp": end_timestamp.timestamp() if end_timestamp else None,
|
||||||
"reason": reason,
|
"reason": reason,
|
||||||
"resolved": resolved,
|
"resolved": resolved,
|
||||||
"resolved_by": resolved_by,
|
"resolved_by": resolved_by,
|
||||||
"resolve_reason": resolved_reason,
|
"resolve_reason": resolved_reason,
|
||||||
"expired": expired,
|
"expired": expired,
|
||||||
"changes": changes,
|
"changes": dumps(changes),
|
||||||
"metadata": metadata
|
"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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
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()
|
cursor.close()
|
||||||
database.commit()
|
database.commit()
|
||||||
if close_db:
|
if close_db:
|
||||||
database.close()
|
database.close()
|
||||||
|
|
||||||
case_safe.update({"guild_id": guild_id})
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"Row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
"Row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
||||||
guild_id,
|
guild_id,
|
||||||
case_safe["moderation_id"],
|
case["moderation_id"],
|
||||||
case_safe["timestamp"],
|
case["timestamp"],
|
||||||
case_safe["moderation_type"],
|
case["moderation_type"],
|
||||||
case_safe["target_type"],
|
case["target_type"],
|
||||||
case_safe["target_id"],
|
case["target_id"],
|
||||||
case_safe["moderator_id"],
|
case["moderator_id"],
|
||||||
case_safe["role_id"],
|
case["role_id"],
|
||||||
case_safe["duration"],
|
case["duration"],
|
||||||
case_safe["end_timestamp"],
|
case["end_timestamp"],
|
||||||
case_safe["reason"],
|
case["reason"],
|
||||||
case_safe["resolved"],
|
case["resolved"],
|
||||||
case_safe["resolved_by"],
|
case["resolved_by"],
|
||||||
case_safe["resolve_reason"],
|
case["resolve_reason"],
|
||||||
case_safe["expired"],
|
case["expired"],
|
||||||
case_safe["changes"],
|
case["changes"],
|
||||||
case_safe["metadata"],
|
case["metadata"],
|
||||||
)
|
)
|
||||||
|
|
||||||
for change in case["changes"]:
|
return cls.from_sql(bot=bot, moderation_id=moderation_id, guild_id=guild_id)
|
||||||
change.update({"bot": bot})
|
|
||||||
|
|
||||||
return cls.from_dict(bot=bot, data=case)
|
|
||||||
|
|
||||||
class Change(AuroraBaseModel):
|
class Change(AuroraBaseModel):
|
||||||
type: Literal["ORIGINAL", "RESOLVE", "EDIT"]
|
type: Literal["ORIGINAL", "RESOLVE", "EDIT"]
|
||||||
|
|
Loading…
Reference in a new issue