diff --git a/moderation/moderation.py b/moderation/moderation.py index 8a6a4b6..3fd8efe 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -5,7 +5,7 @@ import discord import humanize import mysql.connector from pytimeparse2 import disable_dateutil, parse -from redbot.core import Config, checks, commands +from redbot.core import app_commands, checks, Config, commands class Moderation(commands.Cog): @@ -177,62 +177,85 @@ class Moderation(commands.Cog): database.close() logging.debug("MySQL row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, 0, NULL", guild_id, moderation_id, timestamp, moderation_type, target_id, author_id, duration, end_timestamp, reason) - @commands.hybrid_command(name="warn") - @commands.mod() - async def warn(self, ctx: commands.Context, target: discord.Member, *, reason: str): + async def get_next_case_number(self, guild_id: str): + """This method returns the next case number from the MySQL table for a specific guild.""" + database = await self.connect() + cursor = database.cursor() + cursor.execute(f"SELECT moderation_id FROM `moderation_{guild_id}` ORDER BY moderation_id DESC LIMIT 1") + return cursor.fetchone()[0] + 1 + + @app_commands.command(name="warn") + async def warn(self, interaction: discord.Interaction, target: discord.Member, reason: str): """Warn a user.""" - response = await ctx.send(content=f"{target.mention} has been warned!\n**Reason** - `{reason}`") + response = 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 [{ctx.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None)) + 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&") 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.*") - await self.mysql_log(ctx.guild.id, ctx.author.id, 'WARN', target.id, 'NULL', reason) + await self.mysql_log(interaction.guild.id, interaction.user.id, 'WARN', target.id, 'NULL', reason) - @commands.hybrid_command(name="mute", aliases=['timeout', 'tm']) - @commands.mod() - async def mute(self, ctx: commands.Context, target: discord.Member, duration: str, *, reason: str): + @app_commands.command(name="mute", aliases=['timeout', 'tm']) + async def mute(self, interaction: discord.Interaction, target: discord.Member, duration: str, reason: str): """Mute a user.""" if target.is_timed_out() is True: - await ctx.send(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False)) + await interaction.response.send_message(f"{target.mention} is already muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) return try: parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True) except ValueError: - await ctx.send(f"Please provide a valid duration!\nSee `{ctx.prefix}tdc`") + await interaction.response.send_message(f"Please provide a valid duration!", ephemeral=True) return if parsed_time.total_seconds() / 1000 > 2419200000: - await ctx.send("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 await target.timeout(parsed_time) - response = await ctx.send(content=f"{target.mention} has been muted for {humanize.precisedelta(parsed_time)}!\n**Reason** - `{reason}`") + response = 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 [{ctx.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None)) + 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&") 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.*") - await self.mysql_log(ctx.guild.id, ctx.author.id, 'MUTE', target.id, parsed_time, reason) + await self.mysql_log(interaction.guild.id, interaction.user.id, 'MUTE', target.id, parsed_time, reason) - @commands.hybrid_command(name="unmute", aliases=['untimeout', 'utm']) - @commands.mod() - async def unmute(self, ctx: commands.Context, target: discord.Member, *, reason: str = None): + @app_commands.command(name="unmute", aliases=['untimeout', 'utm']) + async def unmute(self, interaction: discord.Interaction, target: discord.Member, reason: str = None): """Unmute a user.""" if target.is_timed_out() is False: - await ctx.send(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False)) + await interaction.response.send_message(f"{target.mention} is not muted!", allowed_mentions=discord.AllowedMentions(users=False), ephemeral=True) return - await target.timeout(None) - if reason is None: + if reason: + await target.timeout(None, reason=f"Unmuted by {interaction.user.id} for: {reason}") + else: + await target.timeout(None) reason = "No reason given." - response = await ctx.send(content=f"{target.mention} has been unmuted!\n**Reason** - `{reason}`") + response = 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 [{ctx.guild.name}]({response.jump_url}).", color=await self.bot.get_embed_color(None)) + 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&") 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.*") - await self.mysql_log(ctx.guild.id, ctx.author.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") + 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}`") + 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&") + 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.*") + 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) @commands.group(autohelp=True) @checks.admin()