Discord Bot Not Responding to Commands (Python)
Asked Answered
I

3

5

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

Italicize answered 5/11, 2020 at 7:17 Comment(0)
B
0

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.

Be answered 5/11, 2020 at 8:36 Comment(2)
Thank you very much! I tried it and got it to work.Italicize
Great! Glad I could help! Would you be able to mark the answer as correct then please?Be
Z
16

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.

Zehe answered 5/11, 2020 at 9:5 Comment(1)
Thank you so much, also been stuck.Gilly
P
1

just put client.process_commands(message)

in on_message event at last..

Philippeville answered 18/11, 2022 at 16:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Penchant
B
0

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.

Be answered 5/11, 2020 at 8:36 Comment(2)
Thank you very much! I tried it and got it to work.Italicize
Great! Glad I could help! Would you be able to mark the answer as correct then please?Be

© 2022 - 2024 — McMap. All rights reserved.