-
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 #660 from maukkis/feat/add-permafk-command
Feat add permafk command
- Loading branch information
Showing
4 changed files
with
92 additions
and
5 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
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
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,71 @@ | ||
import contextlib | ||
import textwrap | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
from tux.bot import Tux | ||
from tux.database.controllers import AfkController | ||
from tux.utils.constants import CONST | ||
from tux.utils.flags import generate_usage | ||
|
||
|
||
class PermAfk(commands.Cog): | ||
def __init__(self, bot: Tux) -> None: | ||
self.bot = bot | ||
self.db = AfkController() | ||
self.permafk.usage = generate_usage(self.permafk) | ||
|
||
async def remove_afk(self, target: int) -> None: | ||
await self.db.remove_afk(target) | ||
|
||
@commands.hybrid_command(name="permafk") | ||
@commands.guild_only() | ||
async def permafk(self, ctx: commands.Context[Tux], *, reason: str = "No reason.") -> discord.Message: | ||
""" | ||
Set yourself permanently AFK until you rerun the command. | ||
Parameters | ||
---------- | ||
ctx : commands.Context[Tux] | ||
The context of the command. | ||
reason : str, optional | ||
The reason you are AFK. | ||
""" | ||
|
||
target = ctx.author | ||
|
||
assert ctx.guild | ||
assert isinstance(target, discord.Member) | ||
|
||
if await self.db.is_afk(target.id, guild_id=ctx.guild.id): | ||
await self.remove_afk(target.id) | ||
|
||
return await ctx.send("Welcome back!") | ||
|
||
if len(target.display_name) >= CONST.NICKNAME_MAX_LENGTH - 6: | ||
truncated_name = f"{target.display_name[: CONST.NICKNAME_MAX_LENGTH - 9]}..." | ||
new_name = f"[AFK] {truncated_name}" | ||
|
||
else: | ||
new_name = f"[AFK] {target.display_name}" | ||
|
||
shortened_reason = textwrap.shorten(reason, width=100, placeholder="...") | ||
await self.db.insert_afk(target.id, target.display_name, shortened_reason, ctx.guild.id, True) | ||
|
||
with contextlib.suppress(discord.Forbidden): | ||
await target.edit(nick=new_name) | ||
|
||
return await ctx.send( | ||
content="\N{SLEEPING SYMBOL} || You are now permanently afk! To remove afk run this command again. " | ||
+ f"Reason: `{shortened_reason}`", | ||
allowed_mentions=discord.AllowedMentions( | ||
users=False, | ||
everyone=False, | ||
roles=False, | ||
), | ||
) | ||
|
||
|
||
async def setup(bot: Tux): | ||
await bot.add_cog(PermAfk(bot)) |
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