feat(moderation): moved embed creation to a seperate method
Some checks failed
Pylint / Pylint (push) Failing after 1m17s

This commit is contained in:
Seaswimmer 2023-10-05 09:21:40 -04:00
parent 27070d3920
commit 5136a492f8
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -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") cursor.execute(f"SELECT moderation_id FROM `moderation_{guild_id}` ORDER BY moderation_id DESC LIMIT 1")
return cursor.fetchone()[0] + 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") @app_commands.command(name="warn")
async def warn(self, interaction: discord.Interaction, target: discord.Member, reason: str): async def warn(self, interaction: discord.Interaction, target: discord.Member, reason: str):
"""Warn a user.""" """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: 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 = self.embed_factory('message', interaction.guild, reason, 'warned', interaction.original_response())
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&")
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: 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) await self.mysql_log(interaction.guild.id, interaction.user.id, 'WARN', target.id, 'NULL', reason)
@app_commands.command(name="mute") @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.") await interaction.response.send_message("Please provide a duration that is less than 28 days.")
return return
await target.timeout(parsed_time) 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: 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 = self.embed_factory('message', interaction.guild, reason, 'muted', interaction.original_response(), parsed_time)
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&")
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: 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) await self.mysql_log(interaction.guild.id, interaction.user.id, 'MUTE', target.id, parsed_time, reason)
@app_commands.command(name="unmute") @app_commands.command(name="unmute")
@ -233,27 +253,23 @@ class Moderation(commands.Cog):
else: else:
await target.timeout(None) await target.timeout(None)
reason = "No reason given." 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: 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 = self.embed_factory('message', interaction.guild, reason, 'unmuted', interaction.original_response())
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&")
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: 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) await self.mysql_log(interaction.guild.id, interaction.user.id, 'UNMUTE', target.id, 'NULL', reason)
@app_commands.command(name="kick") @app_commands.command(name="kick")
async def kick(self, interaction: discord.Interaction, target: discord.Member, reason: str): async def kick(self, interaction: discord.Interaction, target: discord.Member, reason: str):
"""Kick a user.""" """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: 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 = self.embed_factory('message', interaction.guild, reason, 'kicked', interaction.original_response())
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&")
await target.send(embed=embed) await target.send(embed=embed)
except discord.errors.HTTPException: 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 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) await self.mysql_log(interaction.guild.id, interaction.user.id, 'KICK', target.id, 'NULL', reason)