from bot.cogs.cog_manager import BaseCog import discord import datetime import re class AFKCog(BaseCog): def __init__(self, bot): super().__init__(bot) self.afk = False self.afk_since = None self.afk_reason = None def cleanup(self): self.afk = False self.afk_since = None self.afk_reason = None async def cmd_afk(self, message): """ Set AFK status Usage: .afk [reason] """ parts = message.content.split(' ', 1) reason = parts[1] if len(parts) > 1 else "AFK" self.afk = True self.afk_since = datetime.datetime.now() self.afk_reason = reason await message.edit(content=f"You are now AFK: {reason}") async def cmd_unafk(self, message): """ Remove AFK status Usage: .unafk """ if not self.afk: await message.edit(content="You are not AFK.") return self.afk = False afk_duration = datetime.datetime.now() - self.afk_since hours, remainder = divmod(afk_duration.seconds, 3600) minutes, seconds = divmod(remainder, 60) duration_str = "" if hours > 0: duration_str += f"{hours} hour{'s' if hours != 1 else ''} " if minutes > 0: duration_str += f"{minutes} minute{'s' if minutes != 1 else ''} " if seconds > 0 or (hours == 0 and minutes == 0): duration_str += f"{seconds} second{'s' if seconds != 1 else ''}" await message.edit(content=f"Welcome back! You were AFK for {duration_str.strip()}.") self.afk_since = None self.afk_reason = None async def handle_afk_dm(self, message): """ Handle incoming DMs when in AFK mode """ if not self.afk: return # Don't respond to self if message.author == self.bot.user: return # Check if we already replied recently # This prevents spam if someone sends multiple messages async for msg in message.channel.history(limit=10): if msg.author == self.bot.user and "I'm currently AFK" in msg.content: # Only reply once every 5 minutes to the same person time_diff = datetime.datetime.now(datetime.timezone.utc) - msg.created_at if time_diff.total_seconds() < 300: # 5 minutes return # Calculate AFK duration afk_duration = datetime.datetime.now() - self.afk_since hours, remainder = divmod(afk_duration.seconds, 3600) minutes, seconds = divmod(remainder, 60) duration_str = "" if hours > 0: duration_str += f"{hours} hour{'s' if hours != 1 else ''} " if minutes > 0: duration_str += f"{minutes} minute{'s' if minutes != 1 else ''} " if seconds > 0 or (hours == 0 and minutes == 0): duration_str += f"{seconds} second{'s' if seconds != 1 else ''}" # Send AFK message await message.channel.send( f"I'm currently AFK ({duration_str.strip()}): {self.afk_reason}\n" f"I'll respond when I return." )