How to detect user's current game activity without requiring messages in Discord bot

I’m working on a Discord bot using Python and I have a specific requirement. Right now my bot only checks what game someone is playing when they send a message in chat. Here’s what I currently have:

@bot.event
async def on_message(msg):
    user_activity = msg.author.activity.name
    gamer_role = discord.utils.get(msg.guild.roles, name="Player")
    if user_activity == "Valorant":
        await msg.author.add_roles(gamer_role)
    else:
        await msg.author.remove_roles(gamer_role)

The problem is that users need to type something in chat for the bot to detect their game status. I want the bot to automatically detect when someone starts playing Valorant and assign them the Player role without them having to chat first. Is there an event or method that can monitor user activity changes in real-time? Any help would be appreciated.

First, make sure you’ve got presences intent enabled. Most people forget to add intents = discord.Intents.default() and intents.presences = True before creating the bot client. Also heads up - users can run multiple activities at once (like Spotify + Valorant), so you’ll probably need to loop through after.activities instead of just checking after.activity.

Use the on_presence_update event instead of tracking messages. It triggers whenever someone’s activity or status changes. Here’s the updated code:

@bot.event
async def on_presence_update(before, after):
    if before.activity != after.activity:
        gamer_role = discord.utils.get(after.guild.roles, name="Player")
        if after.activity and after.activity.name == "Valorant":
            await after.add_roles(gamer_role)
        elif gamer_role in after.roles:
            await after.remove_roles(gamer_role)

Make sure you’ve enabled the presence intent in the Discord Developer Portal and set intents.presences = True in your code. Your bot will then track presence changes automatically without users having to type anything.

The presence intent approach works, but there’s the catch: Discord limited presence data for most bots in 2022. Now it only functions for verified bots with approved use cases or those in under 100 servers. If presence detection isn’t working, consider a hybrid method. Keep your on_message handler as a backup, and add periodic checks using on_voice_state_update, as gamers often join voice channels when playing. You might also include a manual !checkgame command for users to update their own roles. Combining multiple detection methods tends to yield better results, particularly for smaller community bots unable to access privileged intents.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.