From 5f5fe459cb03d578afcce6d1f4249f222acbb77b Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 14 Dec 2023 20:16:25 -0500 Subject: [PATCH] misc(moderation): changed all case_number arguments to case --- moderation/moderation.py | 113 +++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 51 deletions(-) diff --git a/moderation/moderation.py b/moderation/moderation.py index 6c046ef..7e51cd3 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -1007,12 +1007,12 @@ class Moderation(commands.Cog): await interaction.followup.send(embed=embed, ephemeral=ephemeral) @app_commands.command(name="resolve") - async def resolve(self, interaction: discord.Interaction, case_number: int, reason: str = None): + async def resolve(self, interaction: discord.Interaction, case: int, reason: str = None): """Resolve a specific case. Parameters ----------- - case_number: int + case: int Case number of the case you're trying to resolve reason: str Reason for resolving case""" @@ -1030,24 +1030,24 @@ class Moderation(commands.Cog): db = await self.config.mysql_database() 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)) result_1 = cursor.fetchone() - 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) + if result_1 is None or case == 0: + await interaction.response.send_message(content=f"There is no moderation with a case number of {case}.", ephemeral=True) return 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)) result_2 = cursor.fetchone() 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}` for more information.", ephemeral=True) return - case = self.generate_dict(result_2) + case_dict = self.generate_dict(result_2) if reason is None: reason = "No reason given." - changes: list = case['changes'] + changes: list = case_dict['changes'] if len(changes) > 25: await interaction.response.send_message(content="Due to limitations with Discord's embed system, you cannot edit a case more than 25 times.", ephemeral=True) return @@ -1055,9 +1055,9 @@ class Moderation(commands.Cog): changes.append( { 'type': "ORIGINAL", - 'timestamp': case['timestamp'], - 'reason': case['reason'], - 'user_id': case['moderator_id'] + 'timestamp': case_dict['timestamp'], + 'reason': case_dict['reason'], + 'user_id': case_dict['moderator_id'] } ) changes.append( @@ -1069,23 +1069,23 @@ class Moderation(commands.Cog): } ) - if case['moderation_type'] in ['UNMUTE', 'UNBAN']: + if case_dict['moderation_type'] in ['UNMUTE', 'UNBAN']: 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'] == 'MUTE': + if case_dict['moderation_type'] in ['MUTE', 'TEMPBAN', 'BAN']: + if case_dict['moderation_type'] == 'MUTE': try: - member = await interaction.guild.fetch_member(case['target_id']) + member = await interaction.guild.fetch_member(case_dict['target_id']) - await member.timeout(None, reason=f"Case #{case_number} resolved by {interaction.user.id}") + await member.timeout(None, reason=f"Case #{case} resolved by {interaction.user.id}") except discord.NotFound: pass - if case['moderation_type'] in ['TEMPBAN', 'BAN']: + if case_dict['moderation_type'] in ['TEMPBAN', 'BAN']: try: - user = await interaction.client.fetch_user(case['target_id']) + user = await interaction.client.fetch_user(case_dict['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} resolved by {interaction.user.id}") except discord.NotFound: pass @@ -1093,26 +1093,32 @@ class Moderation(commands.Cog): else: resolve_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET resolved = 1, changes = %s, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s" - cursor.execute(resolve_query, (json.dumps(changes), interaction.user.id, reason, case_number)) + cursor.execute(resolve_query, (json.dumps(changes), interaction.user.id, reason, classmethod)) database.commit() - embed = await self.embed_factory('case', interaction=interaction, case_dict=await self.fetch_case(case_number, interaction.guild.id)) - await interaction.response.send_message(content=f"✅ Moderation #{case_number} resolved!", embed=embed) - await self.log(interaction, case_number, True) + embed = await self.embed_factory('case', interaction=interaction, case_dict=await self.fetch_case(case, interaction.guild.id)) + await interaction.response.send_message(content=f"✅ Moderation #{case} resolved!", embed=embed) + await self.log(interaction, case, True) cursor.close() database.close() @app_commands.command(name="case") - async def case(self, interaction: discord.Interaction, case_number: int, ephemeral: bool = False, changes: bool = False, export: bool = False): + @app_commands.choices(export=[ + Choice(name="Export as File", value='file'), + Choice(name='Export as Codeblock', value='codeblock') + ]) + async def case(self, interaction: discord.Interaction, case: int, ephemeral: bool = False, changes: bool = False, export: Choice[str] = None): """Check the details of a specific case. Parameters ----------- - case_number: int + case: int What case are you looking up? ephemeral: bool Hide the command response + changes: bool + List the changes made to the case export: bool Export the case to a JSON file""" permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction) @@ -1120,38 +1126,43 @@ class Moderation(commands.Cog): await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) return - if case_number != 0: - case = await self.fetch_case(case_number, interaction.guild.id) - if case: + if case != 0: + case_dict = await self.fetch_case(case, interaction.guild.id) + if case_dict: if export: - if len(str(case)) > 1800: - filename = str(data_manager.cog_data_path(cog_instance=self)) + str(os.sep) + f"moderation_{interaction.guild.id}_case_{case_number}.json" + if len(str(case)) > 1800 or export == 'file': + filename = str(data_manager.cog_data_path(cog_instance=self)) + str(os.sep) + f"moderation_{interaction.guild.id}_case_{case}.json" with open(filename, "w", encoding="utf-8") as f: - json.dump(case, f, indent=2) + json.dump(case_dict, f, indent=2) - await interaction.response.send_message(file=discord.File(filename, f"moderation_{interaction.guild.id}_case_{case_number}.json"), ephemeral=ephemeral) + if export == 'codeblock': + content = f"Case #{case} exported.\n*Case was too large to export as codeblock, so it has been uploaded as a `.json` file.*" + else: + content = f"Case #{case} exported." + + await interaction.response.send_message(content=content, file=discord.File(filename, f"moderation_{interaction.guild.id}_case_{case}.json"), ephemeral=ephemeral) os.remove(filename) return else: - await interaction.response.send_message(content=f"```json\n{json.dumps(case, indent=2)}```", ephemeral=ephemeral) + await interaction.response.send_message(content=f"```json\n{json.dumps(case_dict, indent=2)}```", ephemeral=ephemeral) return if changes: - embed = await self.embed_factory('changes', interaction=interaction, case_dict=case) + embed = await self.embed_factory('changes', interaction=interaction, case_dict=case_dict) else: - embed = await self.embed_factory('case', interaction=interaction, case_dict=case) + embed = await self.embed_factory('case', interaction=interaction, case_dict=case_dict) await interaction.response.send_message(embed=embed, ephemeral=ephemeral) return - await interaction.response.send_message(content=f"No case with case number `{case_number}` found.", ephemeral=True) + await interaction.response.send_message(content=f"No case with case number `{case}` found.", ephemeral=True) @app_commands.command(name="edit") - async def edit(self, interaction: discord.Interaction, case_number: int, reason: str): + async def edit(self, interaction: discord.Interaction, case: int, reason: str): """Edit the reason of a specific case. Parameters ----------- - case_number: int + case: int What case are you editing? reason: str What is the new reason?""" @@ -1160,14 +1171,14 @@ class Moderation(commands.Cog): await interaction.response.send_message(f"I do not have the `{permissions}` permission, required for this action.", ephemeral=True) return - if case_number != 0: - case = await self.fetch_case(case_number, interaction.guild.id) - if case: + if case != 0: + case_dict = await self.fetch_case(case, interaction.guild.id) + if case_dict: conf = await self.check_conf(['mysql_database']) if conf: raise(LookupError) - changes: list = case['changes'] + changes: list = case_dict['changes'] if len(changes) > 25: await interaction.response.send_message(content="Due to limitations with Discord's embed system, you cannot edit a case more than 25 times.", ephemeral=True) return @@ -1175,9 +1186,9 @@ class Moderation(commands.Cog): changes.append( { 'type': "ORIGINAL", - 'timestamp': case['timestamp'], - 'reason': case['reason'], - 'user_id': case['moderator_id'] + 'timestamp': case_dict['timestamp'], + 'reason': case_dict['reason'], + 'user_id': case_dict['moderator_id'] } ) changes.append( @@ -1194,19 +1205,19 @@ class Moderation(commands.Cog): db = await self.config.mysql_database() update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s WHERE moderation_id = %s" - cursor.execute(update_query, (json.dumps(changes), reason, case_number)) + cursor.execute(update_query, (json.dumps(changes), reason, case)) database.commit() - new_case = await self.fetch_case(case_number, interaction.guild.id) + new_case = await self.fetch_case(case, interaction.guild.id) embed = await self.embed_factory('case', interaction=interaction, case_dict=new_case) - await interaction.response.send_message(content=f"✅ Moderation #{case_number} edited!", embed=embed, ephemeral=True) - await self.log(interaction, case_number) + await interaction.response.send_message(content=f"✅ Moderation #{case} edited!", embed=embed, ephemeral=True) + await self.log(interaction, case) cursor.close() database.close() return - await interaction.response.send_message(content=f"No case with case number `{case_number}` found.", ephemeral=True) + await interaction.response.send_message(content=f"No case with case number `{case}` found.", ephemeral=True) @tasks.loop(minutes=1) async def handle_expiry(self):