diff --git a/tux/cogs/moderation/clearafk.py b/tux/cogs/moderation/clearafk.py new file mode 100644 index 00000000..778d2591 --- /dev/null +++ b/tux/cogs/moderation/clearafk.py @@ -0,0 +1,58 @@ +import contextlib + +import discord +from discord.ext import commands + +from tux.bot import Tux +from tux.database.controllers import AfkController +from tux.utils import checks + + +class ClearAFK(commands.Cog): + def __init__(self, bot: Tux) -> None: + self.bot = bot + self.db = AfkController() + self.clear_afk.usage = "clearafk " + + @commands.hybrid_command( + name="clearafk", + aliases=["cafk", "removeafk"], + description="Clear a member's AFK status and reset their nickname.", + ) + @commands.guild_only() + @checks.has_pl(2) # Ensure the user has the required permission level + async def clear_afk( + self, + ctx: commands.Context[Tux], + member: discord.Member, + ) -> discord.Message: + """ + Clear a member's AFK status and reset their nickname. + + Parameters + ---------- + ctx : commands.Context[Tux] + The context in which the command is being invoked. + member : discord.Member + The member whose AFK status is to be cleared. + """ + + assert ctx.guild + + if not await self.db.is_afk(member.id, guild_id=ctx.guild.id): + return await ctx.send(f"{member.mention} is not currently AFK.", ephemeral=True) + + # Fetch the AFK entry to retrieve the original nickname + entry = await self.db.get_afk_member(member.id, guild_id=ctx.guild.id) + + await self.db.remove_afk(member.id) + + if entry and entry.nickname: + with contextlib.suppress(discord.Forbidden): + await member.edit(nick=entry.nickname) # Reset nickname to original + + return await ctx.send(f"AFK status for {member.mention} has been cleared.", ephemeral=True) + + +async def setup(bot: Tux) -> None: + await bot.add_cog(ClearAFK(bot))