1

Whenever I run my discord python code, and test it in the discord chat, it says the ping command is not found even though I defined it in the code.

I've tried to use both Bot and Client and both gave the same error.

import discord
from discord.ext import commands


bot_prefix= "]"
bot = commands.Bot(command_prefix=bot_prefix)


bot.run("*")

@bot.event
async def on_ready():
    print("ok")
@bot.event
async def on_message(message):
    print(message.content)


@bot.command()
async def ping(ctx):
    latency = bot.latency
    await ctx.send(latency)

Personal information replaced with "*"

The bot should send a message in the user's channel saying the latency of the bot, but instead I just get an error that says: "Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "ping" is not found" even though I defined the ping command in the code.

Also, it should be noted that the on_ready event never runs; I never get the print statement in the console log.

Any help is appreciated thanks :)

2
  • Does the bot come online when you run it. Did you set it up and add it to the server correctly? Commented Apr 2, 2019 at 15:44
  • Yes, it comes online and I can tell that it is getting response from the server because the error only comes when I type "]ping". Commented Apr 2, 2019 at 17:17

2 Answers 2

9

bot.run must be the last line in your code. Python executes sequentially, so all of the stuff below bot.run isn't called until after the bot is finished running.

Sign up to request clarification or add additional context in comments.

Comments

8

Okay I got it fixed!!

Apparently there's an issue with the on_message function, I guess I just skipped over it in the FAQ. Anyone confused about this, just add the line:

await bot.process_commands(message)

into your on_message function. When you define your own on_message function it overrides the original one that passes the message into the commands handler.

Also make sure to use bot.run() at the end of your code, after your function declarations. Simple mistakes, but they're all fixed now :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.