misc(moderation): improved formatting
Some checks failed
Pylint / Pylint (3.10) (push) Failing after 59s

This commit is contained in:
Seaswimmer 2023-12-14 16:05:47 -05:00
parent 15ef2216ce
commit fc0dc8aefe
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -12,7 +12,6 @@ from pytimeparse2 import disable_dateutil, parse
from redbot.core import app_commands, checks, Config, commands, data_manager from redbot.core import app_commands, checks, Config, commands, data_manager
from redbot.core.app_commands import Choice from redbot.core.app_commands import Choice
class Moderation(commands.Cog): class Moderation(commands.Cog):
"""Custom moderation cog. """Custom moderation cog.
Developed by SeaswimmerTheFsh.""" Developed by SeaswimmerTheFsh."""
@ -43,14 +42,18 @@ class Moderation(commands.Cog):
'mysql_username', 'mysql_username',
'mysql_password' 'mysql_password'
]) ])
if conf: if conf:
self.logger.fatal("Failed to create tables, due to MySQL connection configuration being unset.") self.logger.fatal("Failed to create tables, due to MySQL connection configuration being unset.")
return return
guilds: list[discord.Guild] = self.bot.guilds guilds: list[discord.Guild] = self.bot.guilds
try: try:
for guild in guilds: for guild in guilds:
if not await self.bot.cog_disabled_in_guild(self, guild): if not await self.bot.cog_disabled_in_guild(self, guild):
await self.create_guild_table(guild) await self.create_guild_table(guild)
except ConnectionRefusedError: except ConnectionRefusedError:
return return
@ -66,12 +69,15 @@ class Moderation(commands.Cog):
'mysql_database', 'mysql_database',
'mysql_username', 'mysql_username',
'mysql_password' 'mysql_password'
]) ])
if conf: if conf:
self.logger.error("Failed to create a table for %s, due to MySQL connection configuration being unset.", guild.id) self.logger.error("Failed to create a table for %s, due to MySQL connection configuration being unset.", guild.id)
return return
try: try:
await self.create_guild_table(guild) await self.create_guild_table(guild)
except ConnectionRefusedError: except ConnectionRefusedError:
return return
@ -85,17 +91,24 @@ class Moderation(commands.Cog):
else: else:
if entry.user.id == self.bot.user.id: if entry.user.id == self.bot.user.id:
return return
duration = "NULL" duration = "NULL"
if entry.reason: if entry.reason:
reason = entry.reason + " (This action was performed without the bot.)" reason = entry.reason + " (This action was performed without the bot.)"
else: else:
reason = "This action was performed without the bot." reason = "This action was performed without the bot."
if entry.action == discord.AuditLogAction.kick: if entry.action == discord.AuditLogAction.kick:
moderation_type = 'KICK' moderation_type = 'KICK'
elif entry.action == discord.AuditLogAction.ban: elif entry.action == discord.AuditLogAction.ban:
moderation_type = 'BAN' moderation_type = 'BAN'
elif entry.action == discord.AuditLogAction.unban: elif entry.action == discord.AuditLogAction.unban:
moderation_type = 'UNBAN' moderation_type = 'UNBAN'
elif entry.action == discord.AuditLogAction.member_update: elif entry.action == discord.AuditLogAction.member_update:
if entry.after.timed_out_until is not None: if entry.after.timed_out_until is not None:
timed_out_until_aware = entry.after.timed_out_until.replace(tzinfo=timezone.utc) timed_out_until_aware = entry.after.timed_out_until.replace(tzinfo=timezone.utc)
@ -107,6 +120,7 @@ class Moderation(commands.Cog):
moderation_type = 'UNMUTE' moderation_type = 'UNMUTE'
else: else:
return return
await self.mysql_log(entry.guild.id, entry.user.id, moderation_type, entry.target.id, 0, duration, reason) await self.mysql_log(entry.guild.id, entry.user.id, moderation_type, entry.target.id, 0, duration, reason)
async def connect(self): async def connect(self):
@ -117,8 +131,10 @@ class Moderation(commands.Cog):
'mysql_username', 'mysql_username',
'mysql_password' 'mysql_password'
]) ])
if conf: if conf:
raise LookupError("MySQL connection details not set properly!") raise LookupError("MySQL connection details not set properly!")
try: try:
connection = mysql.connector.connect( connection = mysql.connector.connect(
host=await self.config.mysql_address(), host=await self.config.mysql_address(),
@ -126,7 +142,9 @@ class Moderation(commands.Cog):
password=await self.config.mysql_password(), password=await self.config.mysql_password(),
database=await self.config.mysql_database() database=await self.config.mysql_database()
) )
return connection return connection
except mysql.connector.ProgrammingError as e: except mysql.connector.ProgrammingError as e:
self.logger.fatal("Unable to access the MySQL database!\nError:\n%s", e.msg) self.logger.fatal("Unable to access the MySQL database!\nError:\n%s", e.msg)
raise ConnectionRefusedError(f"Unable to access the MySQL Database!\n{e.msg}") from e raise ConnectionRefusedError(f"Unable to access the MySQL Database!\n{e.msg}") from e
@ -134,9 +152,11 @@ class Moderation(commands.Cog):
async def create_guild_table(self, guild: discord.Guild): async def create_guild_table(self, guild: discord.Guild):
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
try: try:
cursor.execute(f"SELECT * FROM `moderation_{guild.id}`") cursor.execute(f"SELECT * FROM `moderation_{guild.id}`")
self.logger.info("MySQL Table exists for server %s (%s)", guild.name, guild.id) self.logger.info("MySQL Table exists for server %s (%s)", guild.name, guild.id)
except mysql.connector.errors.ProgrammingError: except mysql.connector.errors.ProgrammingError:
query = f""" query = f"""
CREATE TABLE `moderation_{guild.id}` ( CREATE TABLE `moderation_{guild.id}` (
@ -157,12 +177,14 @@ class Moderation(commands.Cog):
) )
""" """
cursor.execute(query) cursor.execute(query)
index_query_1 = "CREATE INDEX idx_target_id ON moderation_%s(target_id(25));" index_query_1 = "CREATE INDEX idx_target_id ON moderation_%s(target_id(25));"
cursor.execute(index_query_1, (guild.id,)) cursor.execute(index_query_1, (guild.id,))
index_query_2 = "CREATE INDEX idx_moderator_id ON moderation_%s(moderator_id(25));" index_query_2 = "CREATE INDEX idx_moderator_id ON moderation_%s(moderator_id(25));"
cursor.execute(index_query_2, (guild.id,)) cursor.execute(index_query_2, (guild.id,))
index_query_3 = "CREATE INDEX idx_moderation_id ON moderation_%s(moderation_id);" index_query_3 = "CREATE INDEX idx_moderation_id ON moderation_%s(moderation_id);"
cursor.execute(index_query_3, (guild.id,)) cursor.execute(index_query_3, (guild.id,))
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)
@ -170,16 +192,21 @@ class Moderation(commands.Cog):
""" """
insert_values = (0, 0, "NULL", 0, 0, 0, "NULL", 0, "NULL", 0, "NULL", "NULL", 0, []) insert_values = (0, 0, "NULL", 0, 0, 0, "NULL", 0, "NULL", 0, "NULL", "NULL", 0, [])
cursor.execute(insert_query, insert_values) cursor.execute(insert_query, insert_values)
database.commit() database.commit()
self.logger.info("MySQL Table (moderation_%s) created for %s (%s)", guild.id, guild.name, guild.id) self.logger.info("MySQL Table (moderation_%s) created for %s (%s)", guild.id, guild.name, guild.id)
database.close() database.close()
async def check_conf(self, config: list): async def check_conf(self, config: list):
"""Checks if any required config options are not set.""" """Checks if any required config options are not set."""
not_found_list = [] not_found_list = []
for item in config: for item in config:
if await self.config.item() == " ": if await self.config.item() == " ":
not_found_list.append(item) not_found_list.append(item)
return not_found_list return not_found_list
def check_permissions(self, user: discord.User, permissions: list, ctx: Union[commands.Context, discord.Interaction] = None, guild: discord.Guild = None): def check_permissions(self, user: discord.User, permissions: list, ctx: Union[commands.Context, discord.Interaction] = None, guild: discord.Guild = None):
@ -187,32 +214,43 @@ class Moderation(commands.Cog):
if ctx: if ctx:
member = ctx.guild.get_member(user.id) member = ctx.guild.get_member(user.id)
resolved_permissions = ctx.channel.permissions_for(member) resolved_permissions = ctx.channel.permissions_for(member)
elif guild: elif guild:
member = guild.get_member(user.id) member = guild.get_member(user.id)
resolved_permissions = member.guild_permissions resolved_permissions = member.guild_permissions
else: else:
raise(KeyError) raise(KeyError)
for permission in permissions: for permission in permissions:
if not getattr(resolved_permissions, permission, False) and not resolved_permissions.administrator is True: if not getattr(resolved_permissions, permission, False) and not resolved_permissions.administrator is True:
return permission return permission
return False return False
async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, role_id: int, duration, reason: str): async def mysql_log(self, guild_id: str, author_id: str, moderation_type: str, target_id: int, role_id: int, duration, reason: str):
timestamp = int(time.time()) timestamp = int(time.time())
if duration != "NULL": if duration != "NULL":
end_timedelta = datetime.fromtimestamp(timestamp) + duration end_timedelta = datetime.fromtimestamp(timestamp) + duration
end_timestamp = int(end_timedelta.timestamp()) end_timestamp = int(end_timedelta.timestamp())
else: else:
end_timestamp = 0 end_timestamp = 0
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
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) VALUES (%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, f"{reason}", 0, "NULL", "NULL", 0, []) val = (moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, f"{reason}", 0, "NULL", "NULL", 0, [])
cursor.execute(sql, val) cursor.execute(sql, val)
database.commit() database.commit()
database.close() database.close()
self.logger.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, 0, NULL, NULL, 0, []", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason) self.logger.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, 0, NULL, NULL, 0, []", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, role_id, duration, end_timestamp, reason)
return moderation_id return moderation_id
async def get_next_case_number(self, guild_id: str, cursor = None): async def get_next_case_number(self, guild_id: str, cursor = None):
@ -248,32 +286,38 @@ class Moderation(commands.Cog):
user = interaction.client.get_user(user_id) user = interaction.client.get_user(user_id)
if user is None: if user is None:
user = await interaction.client.fetch_user(user_id) user = await interaction.client.fetch_user(user_id)
user_dict = { user_dict = {
'id': user.id, 'id': user.id,
'name': user.name, 'name': user.name,
'discriminator': user.discriminator 'discriminator': user.discriminator
} }
except discord.errors.NotFound: except discord.errors.NotFound:
user_dict = { user_dict = {
'id': user_id, 'id': user_id,
'name': 'Deleted User', 'name': 'Deleted User',
'discriminator': '0' 'discriminator': '0'
} }
return user_dict return user_dict
async def fetch_role_dict(self, interaction: discord.Interaction, role_id: str): async def fetch_role_dict(self, interaction: discord.Interaction, role_id: str):
"""This method returns a dictionary containing either role information or a standard deleted role template.""" """This method returns a dictionary containing either role information or a standard deleted role template."""
try: try:
role = interaction.guild.get_role(role_id) role = interaction.guild.get_role(role_id)
role_dict = { role_dict = {
'id': role.id, 'id': role.id,
'name': role.name 'name': role.name
} }
except discord.errors.NotFound: except discord.errors.NotFound:
role_dict = { role_dict = {
'id': role_id, 'id': role_id,
'name': 'Deleted Role' 'name': 'Deleted Role'
} }
return role_dict return role_dict
async def embed_factory(self, embed_type: str, /, interaction: discord.Interaction = None, case_dict: dict = None, guild: discord.Guild = None, reason: str = None, moderation_type: str = None, response: discord.InteractionMessage = None, duration: timedelta = None, resolved: bool = False): async def embed_factory(self, embed_type: str, /, interaction: discord.Interaction = None, case_dict: dict = None, guild: discord.Guild = None, reason: str = None, moderation_type: str = None, response: discord.InteractionMessage = None, duration: timedelta = None, resolved: bool = False):
@ -300,68 +344,89 @@ class Moderation(commands.Cog):
- interaction - interaction
- case_dict""" - case_dict"""
if embed_type == 'message': if embed_type == 'message':
if moderation_type in ["kicked", "banned", "tempbanned", "unbanned"]: if moderation_type in ["kicked", "banned", "tempbanned", "unbanned"]:
guild_name = guild.name guild_name = guild.name
else: else:
guild_name = f"[{guild.name}]({response.jump_url})" guild_name = f"[{guild.name}]({response.jump_url})"
if moderation_type in ["tempbanned", "muted"] and duration: if moderation_type in ["tempbanned", "muted"] and duration:
embed_duration = f" for {humanize.precisedelta(duration)}" embed_duration = f" for {humanize.precisedelta(duration)}"
else: else:
embed_duration = "" embed_duration = ""
if moderation_type == "note": if moderation_type == "note":
embed_desc = "recieved a" embed_desc = "received a"
else: else:
embed_desc = "been" embed_desc = "been"
embed = discord.Embed(title=str.title(moderation_type), description=f"You have {embed_desc} {moderation_type}{embed_duration} in {guild_name}.", color=await self.bot.get_embed_color(None), timestamp=datetime.now()) embed = discord.Embed(title=str.title(moderation_type), description=f"You have {embed_desc} {moderation_type}{embed_duration} in {guild_name}.", color=await self.bot.get_embed_color(None), timestamp=datetime.now())
embed.add_field(name='Reason', value=f"`{reason}`") embed.add_field(name='Reason', value=f"`{reason}`")
embed.set_author(name=guild.name, icon_url=guild.icon.url) embed.set_author(name=guild.name, icon_url=guild.icon.url)
embed.set_footer(text=f"Case #{await self.get_next_case_number(guild.id)}", icon_url="https://cdn.discordapp.com/attachments/1070822161389994054/1159469476773904414/arrow-right-circle-icon-512x512-2p1e2aaw.png?ex=65312319&is=651eae19&hm=3cebdd28e805c13a79ec48ef87c32ca532ffa6b9ede2e48d0cf8e5e81f3a6818&") embed.set_footer(text=f"Case #{await self.get_next_case_number(guild.id)}", icon_url="https://cdn.discordapp.com/attachments/1070822161389994054/1159469476773904414/arrow-right-circle-icon-512x512-2p1e2aaw.png?ex=65312319&is=651eae19&hm=3cebdd28e805c13a79ec48ef87c32ca532ffa6b9ede2e48d0cf8e5e81f3a6818&")
return embed return embed
if embed_type == 'case': if embed_type == 'case':
target_user = await self.fetch_user_dict(interaction, case_dict['target_id']) target_user = await self.fetch_user_dict(interaction, case_dict['target_id'])
moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id']) moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id'])
target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`" target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`"
moderator_name = f"`{moderator_user['name']}`" if moderator_user['discriminator'] == "0" else f"`{moderator_user['name']}#{moderator_user['discriminator']}`" moderator_name = f"`{moderator_user['name']}`" if moderator_user['discriminator'] == "0" else f"`{moderator_user['name']}#{moderator_user['discriminator']}`"
embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']}", color=await self.bot.get_embed_color(None)) embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']}", color=await self.bot.get_embed_color(None))
embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Resolved:** {bool(case_dict['resolved'])}\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>" embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Resolved:** {bool(case_dict['resolved'])}\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>"
if case_dict['duration'] != 'NULL': if case_dict['duration'] != 'NULL':
td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))}) td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))})
duration_embed = f"{humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>" if bool(case_dict['expired']) is False else str(humanize.precisedelta(td)) duration_embed = f"{humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>" if bool(case_dict['expired']) is False else str(humanize.precisedelta(td))
embed.description = embed.description + f"\n**Duration:** {duration_embed}\n**Expired:** {bool(case_dict['expired'])}" embed.description = embed.description + f"\n**Duration:** {duration_embed}\n**Expired:** {bool(case_dict['expired'])}"
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:
resolved_user = await self.fetch_user_dict(interaction, case_dict['resolved_by']) resolved_user = await self.fetch_user_dict(interaction, case_dict['resolved_by'])
resolved_name = f"`{resolved_user['name']}`" if resolved_user['discriminator'] == "0" else f"`{resolved_user['name']}#{resolved_user['discriminator']}`" resolved_name = f"`{resolved_user['name']}`" if resolved_user['discriminator'] == "0" else f"`{resolved_user['name']}#{resolved_user['discriminator']}`"
embed.add_field(name='Resolve Reason', value=f"Resolved by {resolved_name} ({resolved_user['id']}) for:\n```{case_dict['resolve_reason']}```", inline=False) embed.add_field(name='Resolve Reason', value=f"Resolved by {resolved_name} ({resolved_user['id']}) for:\n```{case_dict['resolve_reason']}```", inline=False)
return embed return embed
if embed_type == 'log': if embed_type == 'log':
if resolved: if resolved:
target_user = await self.fetch_user_dict(interaction, case_dict['target_id']) target_user = await self.fetch_user_dict(interaction, case_dict['target_id'])
moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id']) moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id'])
target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`" target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`"
moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}" moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}"
embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']} Resolved", color=await self.bot.get_embed_color(None)) embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']} Resolved", color=await self.bot.get_embed_color(None))
embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>" embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>"
if case_dict['duration'] != 'NULL': if case_dict['duration'] != 'NULL':
td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))}) td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))})
duration_embed = f"{humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>" if case_dict["expired"] == '0' else str(humanize.precisedelta(td)) duration_embed = f"{humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>" if case_dict["expired"] == '0' else str(humanize.precisedelta(td))
embed.description = embed.description + f"\n**Duration:** {duration_embed}\n**Expired:** {bool(case_dict['expired'])}" embed.description = embed.description + f"\n**Duration:** {duration_embed}\n**Expired:** {bool(case_dict['expired'])}"
embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False) embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False)
resolved_user = await self.fetch_user_dict(interaction, case_dict['resolved_by']) resolved_user = await self.fetch_user_dict(interaction, case_dict['resolved_by'])
resolved_name = resolved_user['name'] if resolved_user['discriminator'] == "0" else f"{resolved_user['name']}#{resolved_user['discriminator']}" resolved_name = resolved_user['name'] if resolved_user['discriminator'] == "0" else f"{resolved_user['name']}#{resolved_user['discriminator']}"
embed.add_field(name='Resolve Reason', value=f"Resolved by {resolved_name} ({resolved_user['id']}) for:\n```{case_dict['resolve_reason']}```", inline=False) embed.add_field(name='Resolve Reason', value=f"Resolved by {resolved_name} ({resolved_user['id']}) for:\n```{case_dict['resolve_reason']}```", inline=False)
else: else:
target_user = await self.fetch_user_dict(interaction, case_dict['target_id']) target_user = await self.fetch_user_dict(interaction, case_dict['target_id'])
moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id']) moderator_user = await self.fetch_user_dict(interaction, case_dict['moderator_id'])
target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`" target_name = f"`{target_user['name']}`" if target_user['discriminator'] == "0" else f"`{target_user['name']}#{target_user['discriminator']}`"
moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}" moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}"
embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']}", color=await self.bot.get_embed_color(None)) embed = discord.Embed(title=f"📕 Case #{case_dict['moderation_id']}", color=await self.bot.get_embed_color(None))
embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>" embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Timestamp:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>"
if case_dict['duration'] != 'NULL': if case_dict['duration'] != 'NULL':
td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))}) td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))})
embed.description = embed.description + f"\n**Duration:** {humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>" embed.description = embed.description + f"\n**Duration:** {humanize.precisedelta(td)} | <t:{case_dict['end_timestamp']}:R>"
embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False) embed.add_field(name='Reason', value=f"```{case_dict['reason']}```", inline=False)
return embed return embed
@ -371,11 +436,14 @@ class Moderation(commands.Cog):
"""This method fetches a case from the database and returns the case's dictionary.""" """This method fetches a case from the database and returns the case's dictionary."""
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;" query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
cursor.execute(query, (guild_id, moderation_id)) cursor.execute(query, (guild_id, moderation_id))
result = cursor.fetchone() result = cursor.fetchone()
cursor.close() cursor.close()
database.close() database.close()
return self.generate_dict(result) return self.generate_dict(result)
async def log(self, interaction: discord.Interaction, moderation_id: int, resolved: bool = False): async def log(self, interaction: discord.Interaction, moderation_id: int, resolved: bool = False):
@ -383,6 +451,7 @@ class Moderation(commands.Cog):
logging_channel_id = await self.config.guild(interaction.guild).log_channel() logging_channel_id = await self.config.guild(interaction.guild).log_channel()
if logging_channel_id != " ": if logging_channel_id != " ":
logging_channel = interaction.guild.get_channel(logging_channel_id) logging_channel = interaction.guild.get_channel(logging_channel_id)
case = await self.fetch_case(moderation_id, interaction.guild.id) case = await self.fetch_case(moderation_id, interaction.guild.id)
if case: if case:
embed = await self.embed_factory('log', interaction=interaction, case_dict=case, resolved=resolved) embed = await self.embed_factory('log', interaction=interaction, case_dict=case, resolved=resolved)
@ -415,7 +484,9 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
await interaction.response.send_message(content=f"{target.mention} has recieved a note!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has recieved a note!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -424,6 +495,7 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'NOTE', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'NOTE', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has received a note! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has received a note! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -448,7 +520,9 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
await interaction.response.send_message(content=f"{target.mention} has been warned!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been warned!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -457,6 +531,7 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'WARN', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'WARN', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has been warned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been warned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -483,6 +558,7 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction) permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction)
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
@ -490,16 +566,21 @@ class Moderation(commands.Cog):
if target.is_timed_out() is True: if target.is_timed_out() is True:
await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True)
return return
try: try:
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True) parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
except ValueError: except ValueError:
await interaction.response.send_message("Please provide a valid duration!", ephemeral=True) await interaction.response.send_message("Please provide a valid duration!", ephemeral=True)
return return
if parsed_time.total_seconds() / 1000 > 2419200000: if parsed_time.total_seconds() / 1000 > 2419200000:
await interaction.response.send_message("Please provide a duration that is less than 28 days.") await interaction.response.send_message("Please provide a duration that is less than 28 days.")
return return
await target.timeout(parsed_time, reason=f"Muted by {interaction.user.id} for: {reason}") await target.timeout(parsed_time, reason=f"Muted by {interaction.user.id} for: {reason}")
await interaction.response.send_message(content=f"{target.mention} has been muted for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been muted for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -508,6 +589,7 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'MUTE', target.id, 0, parsed_time, reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'MUTE', target.id, 0, parsed_time, reason)
await interaction.edit_original_response(content=f"{target.mention} has been muted for {humanize.precisedelta(parsed_time)}! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been muted for {humanize.precisedelta(parsed_time)}! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -532,6 +614,7 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction) permissions = self.check_permissions(interaction.client.user, ['moderate_members'], interaction)
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
@ -544,7 +627,9 @@ class Moderation(commands.Cog):
else: else:
await target.timeout(None, reason=f"Unbanned by {interaction.user.id}") await target.timeout(None, reason=f"Unbanned by {interaction.user.id}")
reason = "No reason given." reason = "No reason given."
await interaction.response.send_message(content=f"{target.mention} has been unmuted!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been unmuted!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -553,6 +638,7 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNMUTE', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNMUTE', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has been unmuted! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been unmuted! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -577,11 +663,14 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
permissions = self.check_permissions(interaction.client.user, ['kick_members'], interaction) permissions = self.check_permissions(interaction.client.user, ['kick_members'], interaction)
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return return
await interaction.response.send_message(content=f"{target.mention} has been kicked!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been kicked!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -590,7 +679,9 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
await target.kick(f"Kicked by {interaction.user.id} for: {reason}") await target.kick(f"Kicked by {interaction.user.id} for: {reason}")
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'KICK', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'KICK', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has been kicked! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been kicked! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -627,7 +718,9 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction) permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction)
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return return
@ -637,24 +730,30 @@ class Moderation(commands.Cog):
return return
except discord.errors.NotFound: except discord.errors.NotFound:
pass pass
if duration: if duration:
try: try:
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True) parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
except ValueError: except ValueError:
await interaction.response.send_message("Please provide a valid duration!", ephemeral=True) await interaction.response.send_message("Please provide a valid duration!", ephemeral=True)
return return
await interaction.response.send_message(content=f"{target.mention} has been banned for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been banned for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`")
try: try:
embed = await self.embed_factory('message', guild=interaction.guild, reason=reason, moderation_type='tempbanned', response=await interaction.original_response(), duration=parsed_time) embed = await self.embed_factory('message', guild=interaction.guild, reason=reason, moderation_type='tempbanned', response=await interaction.original_response(), duration=parsed_time)
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
await interaction.guild.ban(target, reason=f"Tempbanned by {interaction.user.id} for: {reason} (Duration: {parsed_time})", delete_message_seconds=delete_messages) await interaction.guild.ban(target, reason=f"Tempbanned by {interaction.user.id} for: {reason} (Duration: {parsed_time})", delete_message_seconds=delete_messages)
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'TEMPBAN', target.id, 0, parsed_time, reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'TEMPBAN', target.id, 0, parsed_time, reason)
await interaction.edit_original_response(content=f"{target.mention} has been banned for {humanize.precisedelta(parsed_time)}! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been banned for {humanize.precisedelta(parsed_time)}! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
else: else:
await interaction.response.send_message(content=f"{target.mention} has been banned!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been banned!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -663,7 +762,9 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
await interaction.guild.ban(target, reason=f"Banned by {interaction.user.id} for: {reason}", delete_message_seconds=delete_messages) await interaction.guild.ban(target, reason=f"Banned by {interaction.user.id} for: {reason}", delete_message_seconds=delete_messages)
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'BAN', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'BAN', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has been banned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been banned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -688,6 +789,7 @@ class Moderation(commands.Cog):
if interaction.user.top_role <= target_member.top_role: if interaction.user.top_role <= target_member.top_role:
await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True) await interaction.response.send_message(content="You cannot moderate members with a higher role than you!", ephemeral=True)
return return
permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction) permissions = self.check_permissions(interaction.client.user, ['ban_members'], interaction)
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
@ -697,12 +799,15 @@ class Moderation(commands.Cog):
except discord.errors.NotFound: except discord.errors.NotFound:
await interaction.response.send_message(content=f"{target.mention} is not banned!", ephemeral=True) await interaction.response.send_message(content=f"{target.mention} is not banned!", ephemeral=True)
return return
if reason: if reason:
await interaction.guild.unban(target, reason=f"Unbanned by {interaction.user.id} for: {reason}") await interaction.guild.unban(target, reason=f"Unbanned by {interaction.user.id} for: {reason}")
else: else:
await interaction.guild.unban(target, reason=f"Unbanned by {interaction.user.id}") await interaction.guild.unban(target, reason=f"Unbanned by {interaction.user.id}")
reason = "No reason given." reason = "No reason given."
await interaction.response.send_message(content=f"{target.mention} has been unbanned!\n**Reason** - `{reason}`") await interaction.response.send_message(content=f"{target.mention} has been unbanned!\n**Reason** - `{reason}`")
if silent is None: if silent is None:
silent = not await self.config.guild(interaction.guild).dm_users() silent = not await self.config.guild(interaction.guild).dm_users()
if silent is False: if silent is False:
@ -711,6 +816,7 @@ class Moderation(commands.Cog):
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNBAN', target.id, 0, 'NULL', reason) moderation_id = await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNBAN', target.id, 0, 'NULL', reason)
await interaction.edit_original_response(content=f"{target.mention} has been unbanned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`") await interaction.edit_original_response(content=f"{target.mention} has been unbanned! (Case `#{moderation_id}`)\n**Reason** - `{reason}`")
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@ -734,12 +840,15 @@ class Moderation(commands.Cog):
export: bool export: bool
Exports the server's entire moderation history to a JSON file""" Exports the server's entire moderation history to a JSON file"""
await interaction.response.defer(ephemeral=ephemeral) await interaction.response.defer(ephemeral=ephemeral)
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction) permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions: if permissions:
await interaction.followup.send(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.followup.send(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return return
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
if target: if target:
query = """SELECT * query = """SELECT *
FROM moderation_%s FROM moderation_%s
@ -757,54 +866,72 @@ class Moderation(commands.Cog):
FROM moderation_%s FROM moderation_%s
ORDER BY moderation_id DESC;""" ORDER BY moderation_id DESC;"""
cursor.execute(query, (interaction.guild.id,)) cursor.execute(query, (interaction.guild.id,))
results = cursor.fetchall() results = cursor.fetchall()
result_dict_list = [] result_dict_list = []
for result in results: for result in results:
case_dict = self.generate_dict(result) case_dict = self.generate_dict(result)
if case_dict['moderation_id'] == 0: if case_dict['moderation_id'] == 0:
continue continue
result_dict_list.append(case_dict) result_dict_list.append(case_dict)
if export: if export:
try: try:
filename = str(data_manager.cog_data_path(cog_instance=self)) + str(os.sep) + f"moderation_{interaction.guild.id}.json" filename = str(data_manager.cog_data_path(cog_instance=self)) + str(os.sep) + f"moderation_{interaction.guild.id}.json"
with open(filename, "w", encoding="utf-8") as f: with open(filename, "w", encoding="utf-8") as f:
json.dump(result_dict_list, f, indent=2) json.dump(result_dict_list, f, indent=2)
await interaction.followup.send(file=discord.File(filename, f"moderation_{interaction.guild.id}.json"), ephemeral=ephemeral) await interaction.followup.send(file=discord.File(filename, f"moderation_{interaction.guild.id}.json"), ephemeral=ephemeral)
os.remove(filename) os.remove(filename)
return return
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
await interaction.followup.send(content=f"An error occured while exporting the moderation history.\nError:\n```{e}```", ephemeral=ephemeral) await interaction.followup.send(content=f"An error occured while exporting the moderation history.\nError:\n```{e}```", ephemeral=ephemeral)
return return
case_quantity = len(result_dict_list) case_quantity = len(result_dict_list)
page_quantity = round(case_quantity / pagesize) page_quantity = round(case_quantity / pagesize)
start_index = (page - 1) * pagesize start_index = (page - 1) * pagesize
end_index = page * pagesize end_index = page * pagesize
embed = discord.Embed(color=await self.bot.get_embed_color(None)) embed = discord.Embed(color=await self.bot.get_embed_color(None))
embed.set_author(icon_url=interaction.guild.icon.url, name='Infraction History') embed.set_author(icon_url=interaction.guild.icon.url, name='Infraction History')
embed.set_footer(text=f"Page {page}/{page_quantity} | {case_quantity} Results") embed.set_footer(text=f"Page {page}/{page_quantity} | {case_quantity} Results")
memory_dict = {} memory_dict = {}
for case in result_dict_list[start_index:end_index]: for case in result_dict_list[start_index:end_index]:
if case['target_id'] not in memory_dict: if case['target_id'] not in memory_dict:
memory_dict[str(case['target_id'])] = await self.fetch_user_dict(interaction, case['target_id']) memory_dict[str(case['target_id'])] = await self.fetch_user_dict(interaction, case['target_id'])
target_user = memory_dict[str(case['target_id'])] target_user = memory_dict[str(case['target_id'])]
if case['moderator_id'] not in memory_dict: if case['moderator_id'] not in memory_dict:
memory_dict[str(case['moderator_id'])] = await self.fetch_user_dict(interaction, case['moderator_id']) memory_dict[str(case['moderator_id'])] = await self.fetch_user_dict(interaction, case['moderator_id'])
moderator_user = memory_dict[str(case['moderator_id'])] moderator_user = memory_dict[str(case['moderator_id'])]
target_name = target_user['name'] if target_user['discriminator'] == "0" else f"{target_user['name']}#{target_user['discriminator']}" target_name = target_user['name'] if target_user['discriminator'] == "0" else f"{target_user['name']}#{target_user['discriminator']}"
moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}" moderator_name = moderator_user['name'] if moderator_user['discriminator'] == "0" else f"{moderator_user['name']}#{moderator_user['discriminator']}"
field_name = f"Case #{case['moderation_id']} ({str.title(case['moderation_type'])})" field_name = f"Case #{case['moderation_id']} ({str.title(case['moderation_type'])})"
field_value = f"**Target:** `{target_name}` ({target_user['id']})\n**Moderator:** `{moderator_name}` ({moderator_user['id']})" field_value = f"**Target:** `{target_name}` ({target_user['id']})\n**Moderator:** `{moderator_name}` ({moderator_user['id']})"
if len(case['reason']) > 150: if len(case['reason']) > 150:
field_value += f"\n**Reason:** `{str(case['reason'])[:150]}...`" field_value += f"\n**Reason:** `{str(case['reason'])[:150]}...`"
else: else:
field_value += f"\n**Reason:** `{str(case['reason'])}`" field_value += f"\n**Reason:** `{str(case['reason'])}`"
if case['duration'] != 'NULL': if case['duration'] != 'NULL':
td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case["duration"].split(":"))}) td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case["duration"].split(":"))})
duration_embed = f"{humanize.precisedelta(td)} | <t:{case['end_timestamp']}:R>" if bool(case['expired']) is False else f"{humanize.precisedelta(td)} | Expired" duration_embed = f"{humanize.precisedelta(td)} | <t:{case['end_timestamp']}:R>" if bool(case['expired']) is False else f"{humanize.precisedelta(td)} | Expired"
field_value = field_value + f"\n**Duration:** {duration_embed}" field_value = field_value + f"\n**Duration:** {duration_embed}"
if bool(case['resolved']): if bool(case['resolved']):
field_value = field_value + "\n**Resolved:** True" field_value = field_value + "\n**Resolved:** True"
embed.add_field(name=field_name, value=field_value, inline=False) embed.add_field(name=field_name, value=field_value, inline=False)
await interaction.followup.send(embed=embed, ephemeral=ephemeral) await interaction.followup.send(embed=embed, ephemeral=ephemeral)
@app_commands.command(name="resolve") @app_commands.command(name="resolve")
@ -821,54 +948,69 @@ class Moderation(commands.Cog):
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return return
conf = await self.check_conf(['mysql_database']) conf = await self.check_conf(['mysql_database'])
if conf: if conf:
raise(LookupError) raise(LookupError)
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
db = await self.config.mysql_database() db = await self.config.mysql_database()
query_1 = "SELECT * FROM moderation_%s WHERE moderation_id = %s;" query_1 = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
cursor.execute(query_1, (interaction.guild.id, case_number)) cursor.execute(query_1, (interaction.guild.id, case_number))
result_1 = cursor.fetchone() result_1 = cursor.fetchone()
if result_1 is None or case_number == 0: if result_1 is None or case_number == 0:
await interaction.response.send_message(content=f"There is no moderation with a case number of {case_number}.", ephemeral=True) await interaction.response.send_message(content=f"There is no moderation with a case number of {case_number}.", ephemeral=True)
return return
query_2 = "SELECT * FROM moderation_%s WHERE moderation_id = %s AND resolved = 0;" query_2 = "SELECT * FROM moderation_%s WHERE moderation_id = %s AND resolved = 0;"
cursor.execute(query_2, (interaction.guild.id, case_number)) cursor.execute(query_2, (interaction.guild.id, case_number))
result_2 = cursor.fetchone() result_2 = cursor.fetchone()
if result_2 is None: if result_2 is None:
await interaction.response.send_message(content=f"This moderation has already been resolved!\nUse `/case {case_number}` for more information.", ephemeral=True) await interaction.response.send_message(content=f"This moderation has already been resolved!\nUse `/case {case_number}` for more information.", ephemeral=True)
return return
case = self.generate_dict(result_2) case = self.generate_dict(result_2)
if reason is None: if reason is None:
reason = "No reason given." reason = "No reason given."
if case['moderation_type'] in ['UNMUTE', 'UNBAN']: if case['moderation_type'] in ['UNMUTE', 'UNBAN']:
await interaction.response.send_message(content="You cannot resolve this type of moderation!", ephemeral=True) await interaction.response.send_message(content="You cannot resolve this type of moderation!", ephemeral=True)
if case['moderation_type'] in ['MUTE', 'TEMPBAN', 'BAN']: if case['moderation_type'] in ['MUTE', 'TEMPBAN', 'BAN']:
if case['moderation_type'] == 'MUTE': if case['moderation_type'] == 'MUTE':
try: try:
member = await interaction.guild.fetch_member(case['target_id']) member = await interaction.guild.fetch_member(case['target_id'])
await member.timeout(None, reason=f"Case #{case_number} resolved by {interaction.user.id}") await member.timeout(None, reason=f"Case #{case_number} resolved by {interaction.user.id}")
except discord.NotFound: except discord.NotFound:
pass pass
if case['moderation_type'] in ['TEMPBAN', 'BAN']: if case['moderation_type'] in ['TEMPBAN', 'BAN']:
try: try:
user = await interaction.client.fetch_user(case['target_id']) user = await interaction.client.fetch_user(case['target_id'])
await interaction.guild.unban(user, reason=f"Case #{case_number} resolved by {interaction.user.id}") await interaction.guild.unban(user, reason=f"Case #{case_number} resolved by {interaction.user.id}")
except discord.NotFound: except discord.NotFound:
pass pass
resolve_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET resolved = 1, expired = 1, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s" resolve_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET resolved = 1, expired = 1, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s"
else: else:
resolve_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET resolved = 1, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s" resolve_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET resolved = 1, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s"
cursor.execute(resolve_query, (interaction.user.id, reason, case_number)) cursor.execute(resolve_query, (interaction.user.id, reason, case_number))
database.commit() database.commit()
response_query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;" response_query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
cursor.execute(response_query, (interaction.guild.id, case_number)) cursor.execute(response_query, (interaction.guild.id, case_number))
result = cursor.fetchone() result = cursor.fetchone()
case_dict = self.generate_dict(result) case_dict = self.generate_dict(result)
embed = await self.embed_factory('case', interaction=interaction, case_dict=case_dict) embed = await self.embed_factory('case', interaction=interaction, case_dict=case_dict)
await interaction.response.send_message(content=f"✅ Moderation #{case_number} resolved!", embed=embed) await interaction.response.send_message(content=f"✅ Moderation #{case_number} resolved!", embed=embed)
await self.log(interaction, case_number, True) await self.log(interaction, case_number, True)
cursor.close() cursor.close()
database.close() database.close()
@ -886,6 +1028,7 @@ class Moderation(commands.Cog):
if permissions: if permissions:
await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True)
return return
if case_number != 0: if case_number != 0:
case = await self.fetch_case(case_number, interaction.guild.id) case = await self.fetch_case(case_number, interaction.guild.id)
if case: if case:
@ -899,50 +1042,64 @@ class Moderation(commands.Cog):
conf = await self.check_conf(['mysql_database']) conf = await self.check_conf(['mysql_database'])
if conf: if conf:
raise(LookupError) raise(LookupError)
database = await self.connect() database = await self.connect()
cursor = database.cursor() cursor = database.cursor()
db = await self.config.mysql_database() db = await self.config.mysql_database()
guilds: list[discord.Guild] = self.bot.guilds guilds: list[discord.Guild] = self.bot.guilds
for guild in guilds: for guild in guilds:
if not await self.bot.cog_disabled_in_guild(self, guild): if not await self.bot.cog_disabled_in_guild(self, guild):
tempban_query = f"SELECT target_id, moderation_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'TEMPBAN' AND expired = 0" tempban_query = f"SELECT target_id, moderation_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'TEMPBAN' AND expired = 0"
try: try:
cursor.execute(tempban_query, (time.time(),)) cursor.execute(tempban_query, (time.time(),))
result = cursor.fetchall() result = cursor.fetchall()
except mysql.connector.errors.ProgrammingError: except mysql.connector.errors.ProgrammingError:
continue continue
target_ids = [row[0] for row in result] target_ids = [row[0] for row in result]
moderation_ids = [row[1] for row in result] moderation_ids = [row[1] for row in result]
for target_id, moderation_id in zip(target_ids, moderation_ids): for target_id, moderation_id in zip(target_ids, moderation_ids):
user: discord.User = await self.bot.fetch_user(target_id) user: discord.User = await self.bot.fetch_user(target_id)
try: try:
await guild.unban(user, reason=f"Automatic unban from case #{moderation_id}") await guild.unban(user, reason=f"Automatic unban from case #{moderation_id}")
embed = await self.embed_factory('message', guild, f'Automatic unban from case #{moderation_id}', 'unbanned') embed = await self.embed_factory('message', guild, f'Automatic unban from case #{moderation_id}', 'unbanned')
try: try:
await user.send(embed=embed) await user.send(embed=embed)
except discord.errors.HTTPException: except discord.errors.HTTPException:
pass pass
except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException] as e: except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException] as e:
print(f"Failed to unban {user.name}#{user.discriminator} ({user.id}) from {guild.name} ({guild.id})\n{e}") print(f"Failed to unban {user.name}#{user.discriminator} ({user.id}) from {guild.name} ({guild.id})\n{e}")
expiry_query = f"UPDATE `{db}`.`moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= %s AND expired = 0 AND moderation_type != 'BLACKLIST') OR (expired = 0 AND resolved = 1 AND moderation_type != 'BLACKLIST')" expiry_query = f"UPDATE `{db}`.`moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= %s AND expired = 0 AND moderation_type != 'BLACKLIST') OR (expired = 0 AND resolved = 1 AND moderation_type != 'BLACKLIST')"
cursor.execute(expiry_query, (time.time(),)) cursor.execute(expiry_query, (time.time(),))
blacklist_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'BLACKLIST' AND expired = 0" blacklist_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'BLACKLIST' AND expired = 0"
try: try:
cursor.execute(blacklist_query, (time.time(),)) cursor.execute(blacklist_query, (time.time(),))
result = cursor.fetchall() result = cursor.fetchall()
except mysql.connector.errors.ProgrammingError: except mysql.connector.errors.ProgrammingError:
continue continue
target_ids = [row[0] for row in result] target_ids = [row[0] for row in result]
moderation_ids = [row[1] for row in result] moderation_ids = [row[1] for row in result]
role_ids = [row[2] for row in result] role_ids = [row[2] for row in result]
for target_id, moderation_id, role_id in zip(target_ids, moderation_ids, role_ids): for target_id, moderation_id, role_id in zip(target_ids, moderation_ids, role_ids):
try: try:
member: discord.Member = await guild.fetch_member(target_id) member: discord.Member = await guild.fetch_member(target_id)
role: discord.Role = guild.get_role(role_id) role: discord.Role = guild.get_role(role_id)
if role is None: if role is None:
raise discord.errors.NotFound raise discord.errors.NotFound
except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException]: except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException]:
continue continue
database.commit() database.commit()
cursor.close() cursor.close()
database.close() database.close()
@ -1030,24 +1187,31 @@ class Moderation(commands.Cog):
async def on_submit(self, interaction: discord.Interaction): async def on_submit(self, interaction: discord.Interaction):
message = "" message = ""
if self.address.value != "": if self.address.value != "":
await self.config.mysql_address.set(self.address.value) await self.config.mysql_address.set(self.address.value)
message += f"- Address set to\n - `{self.address.value}`\n" message += f"- Address set to\n - `{self.address.value}`\n"
if self.database.value != "": if self.database.value != "":
await self.config.mysql_database.set(self.database.value) await self.config.mysql_database.set(self.database.value)
message += f"- Database set to\n - `{self.database.value}`\n" message += f"- Database set to\n - `{self.database.value}`\n"
if self.username.value != "": if self.username.value != "":
await self.config.mysql_username.set(self.username.value) await self.config.mysql_username.set(self.username.value)
message += f"- Username set to\n - `{self.username.value}`\n" message += f"- Username set to\n - `{self.username.value}`\n"
if self.password.value != "": if self.password.value != "":
await self.config.mysql_password.set(self.password.value) await self.config.mysql_password.set(self.password.value)
trimmed_password = self.password.value[:8] trimmed_password = self.password.value[:8]
message += f"- Password set to\n - `{trimmed_password}` - Trimmed for security\n" message += f"- Password set to\n - `{trimmed_password}` - Trimmed for security\n"
if message == "": if message == "":
trimmed_password = str(await self.config.mysql_password())[:8] trimmed_password = str(await self.config.mysql_password())[:8]
send = f"No changes were made.\nCurrent configuration:\n- Address:\n - `{await self.config.mysql_address()}`\n- Database:\n - `{await self.config.mysql_database()}`\n- Username:\n - `{await self.config.mysql_username()}`\n- Password:\n - `{trimmed_password}` - Trimmed for security" send = f"No changes were made.\nCurrent configuration:\n- Address:\n - `{await self.config.mysql_address()}`\n- Database:\n - `{await self.config.mysql_database()}`\n- Username:\n - `{await self.config.mysql_username()}`\n- Password:\n - `{trimmed_password}` - Trimmed for security"
else: else:
send = f"Configuration changed:\n{message}" send = f"Configuration changed:\n{message}"
await interaction.response.send_message(send, ephemeral=True) await interaction.response.send_message(send, ephemeral=True)
@moderationset.group(autohelp=True) @moderationset.group(autohelp=True)