Why does on_message() stop commands from working?

Question:

Basically, everything appears to work fine and start up, but for some reason I can’t call any of the commands. I’ve been looking around for easily an hour now and looking at examples/watching videos and I can’t for the life of me figure out what is wrong. Code below:

import discord
import asyncio
from discord.ext import commands

bot = commands.Bot(command_prefix = '-')
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_message(message):
    if message.content.startswith('-debug'):
        await message.channel.send('d')

@bot.command(pass_context=True)
async def ping(ctx):
    await ctx.channel.send('Pong!')

@bot.command(pass_context=True)
async def add(ctx, *, arg):
    await ctx.send(arg)

The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won’t call the commands.

Asked By: Acemcbean

||

Answers:

From the documentation:

Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:

@bot.event
async def on_message(message):
    # do some extra stuff here

    await bot.process_commands(message)

The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.

Answered By: Patrick Haugh

Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:

@bot.command()
async def debug(ctx):
    await ctx.send("d")
Answered By: Max Shouman