Forum Archive

Discord.py 1.2.2 Bot doesn't respond to commands

Noxive

Ok, so I'm trying to create Discord Bot and i can't figure out why my commands don't work. The on_message() events work properly if i type shrug the bot sends the emoji but when i try to call the $test command bot doesn't respond literraly no errors and no response on the channel. I've spent like 2 hours reading the documentation and everything is ok, but its not.

Python 3.6.8
discord-py 1.2.2

Please somebody help.

from discord.ext.commands import Bot

TOKEN = 'Here goes my token'

client = discord.Client()
bot = Bot(command_prefix='$') """ Command prefix is marked red in PyCharm"""


@bot.command()
async def test(ctx, arg):
    await ctx.send(arg)

"""
After the bot logging on to the server 
on_ready event occurs and prints the name of the logged in Bot
"""


@client.event
async def on_ready():
    print("We have logged in as: {0.user}".format(client))

"""
When the message is sent event on_message() occurs
"""


@client.event
async def on_message(message):

    """
    If the message is from the bot nothing happens.
    ...
    When the user inputs $hello bot sends Hello! message on the channel.
    ...
    For the message = 'shrug' bot sends shrug emoji in ASCII onto the channel.
    """

    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send("Hello!")

    if message.content.startswith("shrug"):
        await message.channel.send('¯\_(ツ)_/¯')

client.run(TOKEN)

ellie_ff1493

Can you do some basic debugging and find out if the functions get called, so we know if it’s a problem with the return or the message isn’t making it to the function

ellie_ff1493

Just put a few prints in it

ccc

This is probably unrelated but on-the-same-line comments like on line 6 should be done with the pound/hash (#) sign instead of with triple quotes.

a = 'a'  # letter a
b = 'b'  """ letter b"""
assert len(a) == len(b), (a, b)
cvp

@Noxive I really don't know discord but if you see the doc, you have to use your $test with a parameter,
like "$test world". Did you do so? Else nothing happens.

Noxive

@ellie_ff1493 I did. Nothing happens. Like the command event never even occured.

@ccc @cvp I deleted all the comments and PyDoc, there is no change.

JonB

could you try:

@bot.command()
async def test(ctx):
    await ctx.send('test')

then type $test

JonB

oh, i see.

client.run('token')

shuld be

bot.run('token')

also, client.on_message, etc all become bot.on_message
Bot is a subclass of Client -- your bot instance IS your client.

Noxive

@JonB Tried the function, nothing no response not on the server or the terminal

@bot.command()
async def test(ctx):
    print("test Called")
    await ctx.send('test')

I changed the client.event to bot.event, the on_message event and on_ready work fine. The command still doesn't respond

```
import discord
from discord.ext.commands import Bot

TOKEN = 'MyToken'

bot = Bot(command_prefix='$')

@bot.command()
async def test(ctx):
print("test Called")
await ctx.send('test')

@bot.event
async def on_ready():
print("We have logged in as: {0.user}".format(bot))

@bot.event
async def on_message(message):

if message.author == bot.user:
    return

if message.content.startswith("shrug"):
    print("shrug")
    await message.channel.send('¯\_(ツ)_/¯')

bot.run(TOKEN)

Noxive

Ok finally solved it. I missed this fragment in the docs:

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

Thanks everyone for help.