From 5136a492f887bf71c8a2529a7dd9d7e8457ba96d Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 5 Oct 2023 09:21:40 -0400 Subject: [PATCH] feat(moderation): moved embed creation to a seperate method --- moderation/moderation.py | 56 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/moderation/moderation.py b/moderation/moderation.py index adca489..358b145 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -184,17 +184,39 @@ 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 + async def embed_factory(self, type: str, guild: discord.Guild, reason: str, moderation_type: str, response: discord.InteractionMessage, duration: timedelta = None): + """This method creates an embed from set parameters, meant for either moderation logging or contacting the moderated user. + + Valid arguments for 'type': + - 'message' + - 'log' - WIP + - 'case' - WIP""" + if type == 'message': + if moderation_type in ["kicked", "banned", "unbanned"]: + guild_name = guild.name + else: + guild_name = f"[{guild.name}]({response.jump_url})" + if moderation_type in ["tempbanned", "muted"] and duration: + embed_duration = f" for {humanize.precisedelta(duration)}" + else: + embed_duration = "" + embed = discord.Embed(title=str.title(moderation_type), description=f"You have been {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.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&") + return embed + else: + raise(TypeError("'type' argument is invalid!")) + @app_commands.command(name="warn") async def warn(self, interaction: discord.Interaction, target: discord.Member, reason: str): """Warn a user.""" - response = 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}`") try: - embed = discord.Embed(title="Warned", description=f"You have been warned in [{interaction.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None), timestamp=datetime.now()) - embed.add_field(name='Reason', value=f"`{reason}`") - embed.set_footer(text=f"Case #{await self.get_next_case_number(interaction.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 = self.embed_factory('message', interaction.guild, reason, 'warned', interaction.original_response()) await target.send(embed=embed) except discord.errors.HTTPException: - await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") + pass await self.mysql_log(interaction.guild.id, interaction.user.id, 'WARN', target.id, 'NULL', reason) @app_commands.command(name="mute") @@ -212,14 +234,12 @@ class Moderation(commands.Cog): await interaction.response.send_message("Please provide a duration that is less than 28 days.") return await target.timeout(parsed_time) - response = 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}`") try: - embed = discord.Embed(title="Muted", description=f"You have been muted for {humanize.precisedelta(parsed_time)} in [{interaction.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None), timestamp=datetime.now()) - embed.add_field(name='Reason', value=f"`{reason}`") - embed.set_footer(text=f"Case #{await self.get_next_case_number(interaction.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 = self.embed_factory('message', interaction.guild, reason, 'muted', interaction.original_response(), parsed_time) await target.send(embed=embed) except discord.errors.HTTPException: - await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") + pass await self.mysql_log(interaction.guild.id, interaction.user.id, 'MUTE', target.id, parsed_time, reason) @app_commands.command(name="unmute") @@ -233,27 +253,23 @@ class Moderation(commands.Cog): else: await target.timeout(None) reason = "No reason given." - response = 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}`") try: - embed = discord.Embed(title="Unmuted", description=f"You have been unmuted in [{interaction.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None), timestamp=datetime.now()) - embed.add_field(name='Reason', value=f"`{reason}`") - embed.set_footer(text=f"Case #{await self.get_next_case_number(interaction.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 = self.embed_factory('message', interaction.guild, reason, 'unmuted', interaction.original_response()) await target.send(embed=embed) except discord.errors.HTTPException: - await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") + pass await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNMUTE', target.id, 'NULL', reason) @app_commands.command(name="kick") async def kick(self, interaction: discord.Interaction, target: discord.Member, reason: str): """Kick a user.""" - response = 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}`") try: - embed = discord.Embed(title="Warned", description=f"You have been kicked from {interaction.guild.name}.", color=await self.bot.get_embed_color(None), timestamp=datetime.now()) - embed.add_field(name='Reason', value=f"`{reason}`") - embed.set_footer(text=f"Case #{await self.get_next_case_number(interaction.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 = self.embed_factory('message', interaction.guild, reason, 'kicked', interaction.original_response()) await target.send(embed=embed) except discord.errors.HTTPException: - await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") + pass await target.kick(f"Kicked by {interaction.user.id} for: {reason}") await self.mysql_log(interaction.guild.id, interaction.user.id, 'KICK', target.id, 'NULL', reason)