Carcerector
Carcerector

Reputation: 51

Discord Bot Not Responding to Commands (Python)

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

Upvotes: 4

Views: 14067

Answers (3)

I am goku
I am goku

Reputation: 51

just put client.process_commands(message)

in on_message event at last..

Upvotes: 1

Jacob
Jacob

Reputation: 161

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.

Upvotes: 16

Shock9616
Shock9616

Reputation: 116

Ok. First of all, the only import statement that you need at the top is from discord.ext import commands. The other two are not necessary.

Second of all, I tried messing around with your code myself and found that the on_message() function seems to interfere with the commands so taking that out should help.

Third of all, I only found this out when I duplicated one of my own working bots and slowly changed all of the code until it was identical to yours. For some reason python didn't like it when I just copied and pasted your code. I have never seen something like this before so I honestly don't know what to say other than that your code is correct and should work as long as you take the on_message() function out.

Here's the final code that I got working:

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.command()
async def dosomething(ctx):
    await ctx.send("I did something")

bot.run(TOKEN)

As you can see the only things that I have changed from your code are that I removed the redundant imports at the top, and I deleted the on_message() function. It works perfectly like this on my end so I would suggest that you re-type it out like this in a new file and see if that works.

If that doesn't work for you then my next guess would be that there is a problem with your installation of discord.py so you might try uninstalling it and then reinstalling it.

If none of that helps let me know and I will see if I can help you find anything else that might be the cause of the problem.

Upvotes: 0

Related Questions