I've just gotten into writing discord bots. While trying to follow online instructions and tutorials, my bot would not respond to commands. It responded perfectly fine to on_message(), but no matter what I try it won't respond to commands. I'm sure it's something simple, but I would appreciate the help.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
TOKEN = '<token-here>'
@bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
@bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
@bot.command(name='go')
async def dosomething(ctx):
print("command called") #Tried putting this in help in debugging
await message.channel.send("I did something")
bot.run(TOKEN)
Picture of me prompting the bot and the results
I made the same mistake at first.
@bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
This function overiding the on_message event so it is never sent to bot.command()
To fix it you just have to add await bot.process_commands(message) at the end of the on_message function:
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
await bot.process_commands(message)
Haven't tested yet but that should fix your issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With