feat(moderation): added logging for right click moderations
Some checks failed
Pylint / Pylint (push) Failing after 1m12s

This commit is contained in:
Seaswimmer 2023-10-04 11:31:21 -04:00
parent 437d504de9
commit 3f06ce1be4
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -56,6 +56,27 @@ class Moderation(commands.Cog):
except ConnectionRefusedError:
return
@commands.cog.listener('on_audit_log_entry_create')
async def autologger(self, entry: discord.AuditLogEntry):
"""This method automatically logs moderations done by users manually ("right clicks")."""
if entry.user.bot or entry.target.bot:
return
duration = "NULL"
reason = entry.reason + " (Audit Log)"
if entry.action == discord.AuditLogAction.kick:
moderation_type = 'KICK'
elif entry.action == discord.AuditLogAction.ban:
moderation_type = 'BAN'
elif entry.action == discord.AuditLogAction.unban:
moderation_type = 'UNBAN'
elif entry.action == discord.AuditLogAction.kick:
moderation_type = 'KICK'
elif entry.action == discord.AuditLogAction.member_update:
moderation_type = 'TIMEOUT'
else:
return
await self.mysql_log(entry.guild.id, entry.user.id, moderation_type, entry.target.id, duration, reason)
async def connect(self):
"""Connects to the MySQL database, and returns a connection object."""
conf = await self.check_conf([
@ -123,7 +144,7 @@ class Moderation(commands.Cog):
not_found_list.append(item)
return not_found_list
async def mysql_log(self, ctx: commands.Context, moderation_type: str, target_id: int, duration, reason:str ):
async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, duration, reason: str):
timestamp = int(time.time())
if duration != "NULL":
end_timedelta = datetime.fromtimestamp(timestamp) + duration
@ -132,14 +153,14 @@ class Moderation(commands.Cog):
end_timestamp = 0
database = await self.connect()
cursor = database.cursor()
cursor.execute(f"SELECT moderation_id FROM `{ctx.guild.id}_moderation` ORDER BY moderation_id DESC LIMIT 1")
cursor.execute(f"SELECT moderation_id FROM `{guild_id}_moderation` ORDER BY moderation_id DESC LIMIT 1")
moderation_id = cursor.fetchone()[0] + 1
sql = f"INSERT INTO `{ctx.guild.id}_moderation` (moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (moderation_id, timestamp, moderation_type, target_id, ctx.author.id, duration, end_timestamp, f"{reason}", 0, "NULL", 0)
sql = f"INSERT INTO `{guild_id}_moderation` (moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, f"{reason}", 0, "NULL", 0)
cursor.execute(sql, val)
database.commit()
database.close()
logging.debug("MySQL row inserted into %s_moderation!\n%s, %s, %s, %s, %s, %s, %s, %s, 0, NULL", ctx.guild.id, moderation_id, timestamp, moderation_type, target_id, ctx.author.id, duration, end_timestamp, reason)
logging.debug("MySQL row inserted into %s_moderation!\n%s, %s, %s, %s, %s, %s, %s, %s, 0, NULL", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, reason)
@commands.group(autohelp=True)
@checks.admin()