import discord from discord import app_commands from discord.ext import commands from discord.utils import get afks = {} # a remove func that removes [AFK] from the users name and returns it def remove(user): if "[AFK]" in user.split(): return " ".join(user.split()[1:]) return user class afk(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_ready(self): print("AFK Online") # When the user sends a message remove them from the afk list @commands.Cog.listener() async def on_message(self, message): if message.author.bot: return if message.author.id in afks.keys(): afks.pop(message.author.id) try: await message.author.edit(nick=remove(message.author.display_name)) except: pass await message.channel.send(f"{message.author.mention} is no longer AFK") for user_id, reason in afks.items(): if user_id == self.bot.user.id: return member = get(message.guild.members, id=user_id) if (message.reference and member == (await message.channel.fetch_message(message.reference.message_id)).author) or member.id in message.raw_mentions: await message.reply(f"{member.display_name} is AFK: {reason}") @app_commands.command(name="afk", description="Set your status to AFK") async def afk(self, interaction: discord.Interaction, *, reason:str = None): if interaction.user.id in afks.keys(): afks.pop(interaction.user.id) else: try: await interaction.user.edit(nick=f"[AFK] {interaction.user.display_name}") except: pass afks[interaction.user.id] = reason em = discord.Embed(title=f":zzz: Member AFK", description=f"{interaction.user.mention} has went AFK", color=0x00ff00) em.set_thumbnail(url=interaction.user.avatar) em.set_author(name=self.bot.user.name, icon_url=self.bot.user.avatar) em.add_field(name="AFK Note: ", value=reason) em.set_footer(text=f"Powered by {self.bot.user.name}", icon_url=self.bot.user.avatar) await interaction.response.send_message(embed=em) async def setup(bot): await bot.add_cog(afk(bot))