Commands not working in Discord bot implementation

I’m trying to switch from using on_message events to proper command handling in my Discord bot but running into issues. The bot connects successfully and shows as online, but when I type commands nothing happens at all.

Here’s my current setup:

import discord
from discord import Activity, ActivityType
from discord.ext import commands

discord_client = discord.Client()
command_bot = commands.Bot(command_prefix='$')

@command_bot.command()
async def hello(context):
    await context.send('Hi there!')

@discord_client.event
async def on_ready():
    print('Bot is online:')
    print(discord_client.user.name)
    await discord_client.change_presence(activity=Activity(type=ActivityType.playing, name="music | $help"))

discord_client.run(MY_TOKEN)

I can see the bot come online and the status updates correctly, but typing $hello in any channel does absolutely nothing. No response, no errors in console either. I’ve been stuck on this for hours and can’t figure out what’s wrong. Any ideas what I might be missing here?

You’re creating two client instances when you only need one. Your commands are registered on command_bot but you’re running discord_client - which doesn’t know about those commands. Since commands.Bot inherits from discord.Client, you don’t need both.

Ditch the discord_client = discord.Client() line completely. Use only the Bot instance. Change your on_ready decorator to @command_bot.event and run command_bot.run(MY_TOKEN) at the end. I made this same mistake when I started with discord.py - wasted hours debugging before I realized I was running the wrong client.

you’re mixing up two different clients. you’ve got discord_client = discord.Client() and command_bot = commands.Bot() but you’re running the wrong one at the end. should be command_bot.run(MY_TOKEN) not discord_client. also move that on_ready event to the command_bot instead.

Had this exact problem when migrating my first bot from message events. You’re defining commands on command_bot but running discord_client - which doesn’t know your command decorators exist. Your commands are sitting in memory but the client processing messages can’t see them. Just ditch the separate Client instance and use the Bot object for everything. Move your on_ready event to @command_bot.event instead of @discord_client.event and call command_bot.run(MY_TOKEN) at the bottom. Bot class already includes all Client functionality so you don’t need both.

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