Kljunar Man
Kljunar Man

Reputation: 143

Command not found error in Discord Python code

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 :)

Upvotes: 1

Views: 10165

Answers (2)

Kljunar Man
Kljunar Man

Reputation: 143

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 :)

Upvotes: 8

Patrick Haugh
Patrick Haugh

Reputation: 60984

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.

Upvotes: 9

Related Questions