Creating an automatic AFK status removal for Discord bot

Hey everyone! I’m new to Python and I’m trying to make a Discord bot. I’ve got an AFK function working but it’s not quite right. Here’s my problem:

The bot sets a user as AFK when they use the command, but they stay AFK until they use the command again. What I want is for the bot to remove the AFK status as soon as the user sends any message in the server.

I’ve got the basic AFK stuff working. It sets the status and notifies others when they mention an AFK user. But I can’t figure out how to make it automatically remove the AFK status when the user talks again.

Here’s a simplified version of what I’ve got so far:

afk_users = {}

@bot.command()
async def afk(ctx, message='No message'):
    afk_users[ctx.author] = message
    await ctx.send('You are now AFK')

@bot.event
auth async def on_message(message):
    if message.author in afk_users:
        # How do I remove AFK status here?
    
    for mention in message.mentions:
        if mention in afk_users:
            await message.channel.send(f'{mention} is AFK: {afk_users[mention]}')

    await bot.process_commands(message)

Any ideas on how to make this work? Thanks!

Your approach is sound, but there’s a small optimization you could make. Instead of checking if the author is in afk_users for every message, you could use the .pop() method with a default value. This way, you’re only performing one dictionary operation:

if afk_users.pop(message.author, None):
await message.channel.send(f’{message.author.name} is no longer AFK.')

This removes the user from afk_users if they’re in it, and does nothing if they’re not. It’s more efficient and cleaner.

Also, consider adding a cooldown to the AFK command to prevent spam. You can use Discord.py’s built-in cooldown decorator for this:

@commands.cooldown(1, 60, commands.BucketType.user)
@bot.command()
async def afk(ctx, message=‘No message’):
# Your existing code here

This limits users to using the AFK command once per minute.

I’ve dealt with this exact issue before, and I can share what worked for me. In your on_message event, you’re on the right track. Here’s how I handled it:

When a message is received, check if the author is in afk_users. If they are, remove them from the dictionary and send a welcome back message. Something like this:

if message.author in afk_users:
del afk_users[message.author]
await message.channel.send(f’Welcome back, {message.author.name}! Your AFK status has been removed.')

This automatically removes their AFK status as soon as they send any message. Just make sure to place this check before processing commands, so it catches all messages.

One thing to watch out for: If you’re using any bot-specific prefixes, you might want to add a check to ensure you’re not removing AFK status for bot commands. Otherwise, using the AFK command itself could immediately remove the status.

Hope this helps! Let me know if you need any clarification.

yo, i’ve got a tip for ya. in the on_message function, try this:

if message.author in afk_users:
del afk_users[message.author]
await message.channel.send(f’{message.author.name} is back!')

it’ll remove the afk status when they chat. just remember to put it before you process commands. good luck with ur bot!