Discord bot shows ready message but stays offline - Python

I’m working on building a Discord bot with Python and running into a weird problem. My code executes without errors and prints the ready message in the console, but the bot never shows up as online in my Discord server.

import os
from discord.ext import commands
import discord
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv('API_KEY')

permissions = discord.Intents.default()
permissions.message_content = True
client = commands.Bot(command_prefix='$', intents=permissions)

@client.event
async def on_ready():
    print("Bot is now active")

@commands.command()
async def hello(ctx):
    await ctx.send("Hello there!")

client.add_command(hello)
client.run(API_KEY)

I have the token stored properly in my .env file and gave the bot all necessary permissions when adding it to my server. The bot appears to start correctly based on the console output, but it remains offline in Discord. Any ideas what might be causing this issue?

Had this exact same frustrating issue a few months back. Your code looks right, but there’s probably a permissions mismatch between what your bot has and what it needs. Double-check that you selected the “bot” scope in the OAuth2 URL generator when you invited it - not just the application scope. Make sure your bot has “Send Messages” permission at minimum. Without basic permissions, Discord sometimes connects the bot but keeps it showing as offline. Try regenerating your bot token completely and updating your .env file with the new one. Discord invalidates tokens without warning sometimes, especially when you’re testing a lot during development.

Your token configuration looks like the problem. When you set that API_KEY environment variable, make sure you’re using the actual bot token - not the client secret or some other key. Discord bots need the specific token from the Bot section in your app settings.

Check your .env file for extra spaces or hidden characters around the token. I had the same issue where my bot connected but stayed offline - turned out I’d copied whitespace with the token. Print the first few characters of your API_KEY to make sure it starts with the right bot token format.

Check your bot’s status settings - Discord sometimes shows bots offline even when they’re connected. Add activity=discord.Game(name="something") to your Bot() constructor or use await client.change_presence() in on_ready. Also double-check you’re using the right server ID where you invited the bot. I’ve mixed up servers before lol