Discord commands error: Command cannot be found

Discord commands error: Command cannot be found

我正在尝试为我的 discord 机器人添加调平功能,但我遇到了一些问题。这是代码。当我输入 !xp 时出现错误,它随之而来,不知道如何解决。

Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "xp" is not found.

我还有一个名为 'lvldb.json' 的单独文件。

import discord
from discord.ext import commands

import asyncio
import json

bot = commands.Bot(command_prefix='!')

client = discord.Client()

m = {}

@client.event
async def on_ready():
    global m
    with open("lvldb.json", "r") as j:
        m = json.load(j)
        j.close()
    if len(m) == 0:
        m = {}
        for member in client.get_guild(GUILDTOKEN).members:
            m[str(member.id)] = {"xp": 0, "messageCountdown": 0}
    print("ready")
    while True:
        try:
            for member in client.get_guild(GUILDOTOKEN).members:
                m[str(member.id)]["messageCountdown"] -= 1
        except:
            pass
        await asyncio.sleep(1)


@client.event
async def on_message(message):
    global m
    if message.content == "!stop" and message.author.id == ID:
        with open("lvldb.json", "w") as j:
            j.write(json.dumps(m))
            j.close()
        await client.close()
    elif message.content == "!xp":
        await message.channel.send(str( m[str(message.author.id)]["xp"]))
    elif message.author != client.user:
        if m[str(message.author.id)]["messageCountdown"] <= 0:
            m[str(message.author.id)]["xp"] += 10
            m[str(message.author.id)]["messageCountdown"] = 10


@client.event
async def on_member_join(member):
    m[str(member.id)] = {"xp": 0, "messageCountdown": 0}

您正在创建 commands.Botdiscord.Client。你不想这样做; Bot 已经是通用客户端的子类,可以做它需要做的一切。只做一个,比如

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_message(message):
    ...
@bot.event
async def on_member_join(member):
    ...

您也会收到错误消息,因为 discord.py 认为用户试图调用以前缀开头的命令。它尝试获取命令,但在无法获取时引发异常。你可以默默忽略它(见here):

@bot.event
async def on_command_error(ctx, err):
    if err.__class__ is commands.errors.CommandNotFound:
        # who cares?
        return
    print('oh no, an error occured, printing it:', file=sys.stderr)
    traceback.print_exception(type(err), err, err.__traceback__, file=sys.stderr)