diff --git a/moderation/moderation.py b/moderation/moderation.py index 3641a30..0ec8e58 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -124,23 +124,6 @@ class Moderation(commands.Cog): 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 - def generate_dict(self, result): - case: dict = { - "moderation_id": result[0], - "timestamp": result[1], - "moderation_type": result[2], - "target_id": result[3], - "moderator_id": result[4], - "duration": result[5], - "end_timestamp": result[6], - "reason": result[7], - "resolved": result[8], - "resolved_by": result[9], - "resolve_reason": result[10], - "expired": result[11] - } - return case - async def create_guild_table(self, guild: discord.Guild): database = await self.connect() cursor = database.cursor() @@ -212,6 +195,23 @@ class Moderation(commands.Cog): cursor.execute(f"SELECT moderation_id FROM `moderation_{guild_id}` ORDER BY moderation_id DESC LIMIT 1") return cursor.fetchone()[0] + 1 + def generate_dict(self, result): + case: dict = { + "moderation_id": result[0], + "timestamp": result[1], + "moderation_type": result[2], + "target_id": result[3], + "moderator_id": result[4], + "duration": result[5], + "end_timestamp": result[6], + "reason": result[7], + "resolved": result[8], + "resolved_by": result[9], + "resolve_reason": result[10], + "expired": result[11] + } + return case + async def fetch_user_dict(self, interaction: discord.Interaction, user_id: str): """This method returns a dictionary containing either user information or a standard deleted user template.""" try: @@ -234,7 +234,7 @@ class Moderation(commands.Cog): Valid arguments for 'embed_type': - 'message' - - 'log' - WIP + - 'list' - WIP - 'case' Required arguments for 'message': @@ -445,6 +445,42 @@ class Moderation(commands.Cog): pass await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNBAN', target.id, 'NULL', reason) + @app_commands.command(name="history") + async def history(self, interaction: discord.Interaction, target: discord.Member = None, moderator: discord.Member = None, channel: discord.abc.GuildChannel = None, pagesize: app_commands.Range[int, 1, 25] = 5, page: int = 1): + """List previous infractions.""" + database = await self.connect() + cursor = database.cursor() + if target: + query = "SELECT * FROM moderation_%s WHERE target_id = %s" + cursor.execute(query, (interaction.guild.id, target.id)) + results = cursor.fetchall() + result_dict_list = [] + for result in results: + case_dict = self.generate_dict(result) + result_dict_list.append(case_dict) + case_quantity = len(result_dict_list) + page_quantity = round(case_quantity / pagesize) + start_index = (page - 1) * pagesize + end_index = page * pagesize + embed = discord.Embed(color= await self.bot.get_embed_color(None)) + embed.set_author(url=interaction.guild.icon.url, name='Infraction History') + embed.set_footer(text=f"Page {page}/{page_quantity} | {case_quantity} Results") + for case in result_dict_list[start_index:end_index]: + target_user = await self.fetch_user_dict(case['target_id']) + moderator_user = await self.fetch_user_dict(case['moderator_id']) + 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']}" + 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']})\n**Reason:** `{str(case['reason'])[:150]}`" + if case['duration'] != 'NULL': + td = timedelta(**{unit: int(val) for unit, val in zip(["hours", "minutes", "seconds"], case_dict["duration"].split(":"))}) + duration_embed = f"{humanize.precisedelta(td)} | " if case_dict["expired"] == '0' else f"{humanize.precisedelta(td)} | Expired" + field_value = field_value + f"\n**Duration:** {duration_embed}" + if case['resolved'] == '1': + field_value = field_value + "\n**Resolved:** True" + embed.add_field(name=field_name, value=field_value, inline=False) + await interaction.response.send_message(embed=embed) + @app_commands.command(name="resolve") async def resolve(self, interaction: discord.Interaction, case_number: int, reason: str = None): """Resolve a specific case."""