-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #642 from allthingslinux/tess-remove-afk
feat(clearafk): Add a command to clear another user's AFK status
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <member>" | ||
|
||
@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)) |