moderation(feat): added metadata json field to the db

This commit is contained in:
Seaswimmer 2023-12-17 00:02:07 -05:00
parent d2b1c27181
commit b82e1f3f84
Signed by untrusted user: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -221,7 +221,8 @@ class Moderation(commands.Cog):
resolved_by LONGTEXT, resolved_by LONGTEXT,
resolve_reason LONGTEXT, resolve_reason LONGTEXT,
expired BOOL NOT NULL, expired BOOL NOT NULL,
changes JSON NOT NULL changes JSON NOT NULL,
metadata JSON NOT NULL,
) )
""" """
cursor.execute(query) cursor.execute(query)
@ -235,10 +236,10 @@ class Moderation(commands.Cog):
insert_query = f""" insert_query = f"""
INSERT INTO `moderation_{guild.id}` INSERT INTO `moderation_{guild.id}`
(moderation_id, timestamp, moderation_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes) (moderation_id, timestamp, moderation_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""" """
insert_values = (0, 0, "NULL", 0, 0, 0, "NULL", 0, "NULL", 0, "NULL", "NULL", 0, json.dumps([])) insert_values = (0, 0, "NULL", 0, 0, 0, "NULL", 0, "NULL", 0, "NULL", "NULL", 0, json.dumps([]), json.dumps({}))
cursor.execute(insert_query, insert_values) cursor.execute(insert_query, insert_values)
database.commit() database.commit()
@ -313,7 +314,7 @@ class Moderation(commands.Cog):
return True return True
async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, role_id: int, duration, reason: str, database: mysql.connector.MySQLConnection = None, timestamp: int = None, resolved: bool = False, resolved_by: str = None, resolved_reason: str = None, expired: bool = None, changes: list = []): # pylint: disable=dangerous-default-argument async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, role_id: int, duration, reason: str, database: mysql.connector.MySQLConnection = None, timestamp: int = None, resolved: bool = False, resolved_by: str = None, resolved_reason: str = None, expired: bool = None, changes: list = [], metadata: dict = {}): # pylint: disable=dangerous-default-argument
if not timestamp: if not timestamp:
timestamp = int(time.time()) timestamp = int(time.time())
@ -344,8 +345,8 @@ class Moderation(commands.Cog):
moderation_id = await self.get_next_case_number(guild_id=guild_id, cursor=cursor) moderation_id = await self.get_next_case_number(guild_id=guild_id, cursor=cursor)
sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason, int(resolved), resolved_by, resolved_reason, expired, json.dumps(changes)) val = (moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason, int(resolved), resolved_by, resolved_reason, expired, json.dumps(changes), json.dumps(metadata))
cursor.execute(sql, val) cursor.execute(sql, val)
cursor.close() cursor.close()
@ -353,7 +354,7 @@ class Moderation(commands.Cog):
if close_db: if close_db:
database.close() database.close()
self.logger.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason, int(resolved), resolved_by, resolved_reason, expired, changes) self.logger.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason, int(resolved), resolved_by, resolved_reason, expired, changes, metadata)
return moderation_id return moderation_id
@ -380,7 +381,8 @@ class Moderation(commands.Cog):
"resolved_by": result[10], "resolved_by": result[10],
"resolve_reason": result[11], "resolve_reason": result[11],
"expired": result[12], "expired": result[12],
"changes": json.loads(result[13]) "changes": json.loads(result[13]),
"metadata": json.loads(result[14])
} }
return case return case
@ -497,6 +499,10 @@ class Moderation(commands.Cog):
embed.description += f"\n**Changes:** {len(case_dict['changes'])}" embed.description += f"\n**Changes:** {len(case_dict['changes'])}"
if case_dict['metadata']:
if case_dict['metadata']['imported_from']:
embed.description += f"\n**Imported From:** {case_dict['metadata']['imported_from']}"
embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False) embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False)
if case_dict['resolved'] == 1: if case_dict['resolved'] == 1:
@ -1899,6 +1905,9 @@ class Moderation(commands.Cog):
resolved_by=resolved_by, resolved_by=resolved_by,
resolved_reason=resolved_reason, resolved_reason=resolved_reason,
changes=changes, changes=changes,
metadata={
'imported_from': 'GalacticBot'
},
database=database database=database
) )