Python Discord bot shows online status but won't detect member activities

I’m working on my first Discord bot using Python and running into some issues. The bot connects successfully and shows as online, but it’s not responding to member activity changes like I expected.

I want the bot to detect when users are playing specific games or listening to Spotify, but the activity monitoring functions aren’t triggering at all. The basic message commands work fine, but anything related to user status changes seems broken.

Here’s my current code:

import discord

bot = discord.Client(intents=discord.Intents.default())

@bot.event
async def on_ready():
    print("Bot is ready!")

@bot.event
async def on_message(msg):
    if msg.content == "!test":
        await msg.channel.send("Hello there!")

@bot.event
async def on_member_update(old_member, new_member):
    if new_member.activity is not None:
        if str(new_member.activity) == 'Valorant':
            player = new_member
            await player.send("Stop gaming and go outside!")
        elif isinstance(new_member.activity, discord.Spotify):
            player = new_member
            await player.send("Nice music choice!")
            with open('music.png', 'rb') as img:
                await player.send(file=discord.File(img, 'music.png'))

bot.run('my_token_here')

What am I missing here? Any help would be great!

The Problem: Your Discord bot isn’t responding to user activity changes (like game playing or Spotify listening) even though it connects successfully. The on_member_update event isn’t triggering as expected, while basic message commands work fine. This is likely due to missing or incorrectly configured intents in your Discord application and bot code.

:thinking: Understanding the “Why” (The Root Cause):

Discord requires you to explicitly declare which events your bot is allowed to receive. These are called “Intents.” By default, only a minimal set of intents is enabled. The Intents.default() setting in your code only includes basic functionality, not the events related to user presence (like game activity). Without the correct intents enabled, Discord won’t send these updates to your bot, explaining why on_member_update is not firing. Therefore, your bot is missing the information needed to detect user activity changes.

:gear: Step-by-Step Guide:

Step 1: Enable Privileged Intents in the Discord Developer Portal:

  1. Go to the Discord Developer Portal and navigate to your bot’s application page.
  2. Go to the “Bot” section.
  3. Locate the “Privileged Gateway Intents” section.
  4. Enable the PRESENCES intent. This allows your bot to receive updates about users’ current activities.
  5. Enable the SERVER MEMBERS INTENT. This allows your bot to receive updates about member status changes within a server. This intent might be necessary in order for presence data to be properly updated.
  6. Save the changes.

Step 2: Update Your Bot Code to Request the Required Intents:

  1. Modify your Python code to explicitly request the presences and members intents:
import discord

intents = discord.Intents.default()
intents.presences = True  # Enable presence intent
intents.members = True # Enable server members intent
bot = discord.Client(intents=intents)

# ... rest of your code ...

Step 3: Verify Activity Object Properties:

Your original code attempts to compare str(new_member.activity) to ‘Valorant’. This is unreliable, as the string representation of activity includes more than just the game name. Instead, use the name attribute of the activity object:

@bot.event
async def on_member_update(old_member, new_member):
    if new_member.activities: #check for multiple activities
        for activity in new_member.activities:
            if isinstance(activity, discord.activity.Activity): #check if activity is of type Activity
                if activity.name == 'Valorant':
                    player = new_member
                    await player.send("Stop gaming and go outside!")
            elif isinstance(activity, discord.Spotify):
                player = new_member
                await player.send("Nice music choice!")
                with open('music.png', 'rb') as img:
                    await player.send(file=discord.File(img, 'music.png'))

Step 4: Handle Potential Rate Limits:

The on_member_update event can be triggered frequently, especially in active servers. Implement rate limiting to avoid hitting Discord’s API rate limits and causing your bot to be temporarily disabled. You might consider using a cooldown system or a database to track recent activity updates.

Step 5: Test Thoroughly:

Restart your bot after making these changes. Test in a server where your bot has the necessary permissions. Ensure that the bot can now correctly detect changes in user activities.

:mag: Common Pitfalls & What to Check Next:

  • Permission Issues: Even with the correct intents, your bot needs the appropriate permissions within the Discord server to see member activities. Check the role assignments for your bot.
  • Incorrect Intent Handling: Double-check that both the presences and members intents are enabled correctly, both in your code and in the Discord Developer Portal.
  • Rate Limits: If your bot still doesn’t work correctly, check the Discord Developer Portal for any rate limit errors. Implement rate limiting as described above.
  • Multiple Activities: Users can have multiple activities simultaneously. Loop through new_member.activities to account for this, and handle different activity types separately.
  • Activity Object Structure: The structure of activity objects varies depending on the type of activity (game, streaming, etc.). Always inspect the activity object to understand its properties.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Your code’s fine, but you’re missing the privileged intents needed for member activity tracking.

discord.Intents.default() doesn’t include what you need. Add presences and members intents:

intents = discord.Intents.default()
intents.presences = True
intents.members = True
bot = discord.Client(intents=intents)

Also enable these in your Discord Developer Portal - toggle on “Presence Intent” and “Server Members Intent” in your bot settings.

That said, Discord bots get messy fast when you’re scaling or adding features. I’ve been there and honestly, automation platforms save a ton of headaches.

Latenode handles Discord stuff really well - you can set up activity monitoring without dealing with intents or hosting. Just create workflows that trigger on user status changes and connect them wherever you need.

I switched to this for several projects and ditched all the deployment pain. The visual workflow builder beats managing complex bot logic by hand.

Check it out: https://latenode.com

You’re missing the intents configuration. Discord blocks presence updates by default, so your activity detection won’t work. Enable presence and member intents in your code AND the Discord Developer Portal. I hit this exact issue with my server monitoring bot. Even after fixing intents, there’s another gotcha - Discord’s activity objects are weird to compare. Your str(new_member.activity) == 'Valorant' probably won’t work because activity names include extra metadata. Try new_member.activity.name instead for Game activities. Also, users can run multiple activities at once, so loop through new_member.activities rather than just checking the main one. This tripped me up since I was only getting partial data.

The Problem: Your Discord bot isn’t responding to user activity changes (like game playing or Spotify listening) even though it connects successfully. The on_member_update event isn’t triggering as expected, while basic message commands work fine. This is likely due to missing or incorrectly configured intents in your Discord application and bot code.

:thinking: Understanding the “Why” (The Root Cause):

Discord requires you to explicitly declare which events your bot is allowed to receive. These are called “Intents.” By default, only a minimal set of intents is enabled. Without the correct intents enabled, Discord won’t send these updates to your bot, explaining why on_member_update is not firing. Your bot is missing the information needed to detect user activity changes. The Intents.default() setting only provides basic functionality; it doesn’t include the events related to user presence (like game activity).

:gear: Step-by-Step Guide:

Step 1: Enable Privileged Intents in the Discord Developer Portal:

  1. Go to the Discord Developer Portal and navigate to your bot’s application page.
  2. Go to the “Bot” section.
  3. Locate the “Privileged Gateway Intents” section.
  4. Enable the PRESENCES intent. This allows your bot to receive updates about users’ current activities.
  5. Enable the SERVER MEMBERS INTENT. This allows your bot to receive updates about member status changes within a server. This intent might be necessary for presence data to be properly updated.
  6. Save the changes.

Step 2: Update Your Bot Code to Request the Required Intents:

Modify your Python code to explicitly request the presences and members intents:

import discord

intents = discord.Intents.default()
intents.presences = True  # Enable presence intent
intents.members = True # Enable server members intent
bot = discord.Client(intents=intents)

# ... rest of your code ...

Step 3: Verify Activity Object Properties:

Your original code compares str(new_member.activity) to ‘Valorant’. This is unreliable because the string representation of activity includes more than just the game name. Use the name attribute of the activity object:

@bot.event
async def on_member_update(old_member, new_member):
    if new_member.activities: #check for multiple activities
        for activity in new_member.activities:
            if isinstance(activity, discord.activity.Activity): #check if activity is of type Activity
                if activity.name == 'Valorant':
                    player = new_member
                    await player.send("Stop gaming and go outside!")
            elif isinstance(activity, discord.Spotify):
                player = new_member
                await player.send("Nice music choice!")
                with open('music.png', 'rb') as img:
                    await player.send(file=discord.File(img, 'music.png'))

Step 4: Handle Potential Rate Limits:

The on_member_update event can be triggered frequently, especially in active servers. Implement rate limiting to avoid hitting Discord’s API rate limits and causing your bot to be temporarily disabled. You might consider using a cooldown system or a database to track recent activity updates.

Step 5: Test Thoroughly:

Restart your bot after making these changes. Test in a server where your bot has the necessary permissions. Ensure that the bot can now correctly detect changes in user activities.

:mag: Common Pitfalls & What to Check Next:

  • Permission Issues: Even with the correct intents, your bot needs the appropriate permissions within the Discord server to see member activities. Check the role assignments for your bot.
  • Incorrect Intent Handling: Double-check that both the presences and members intents are enabled correctly, both in your code and in the Discord Developer Portal.
  • Rate Limits: If your bot still doesn’t work correctly, check the Discord Developer Portal for any rate limit errors. Implement rate limiting as described above.
  • Multiple Activities: Users can have multiple activities simultaneously. Loop through new_member.activities to account for this, and handle different activity types separately.
  • Activity Object Structure: The structure of activity objects varies depending on the type of activity (game, streaming, etc.). Always inspect the activity object to understand its properties.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

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