首页 > 解决方案 > 为什么我不应该收到错误“命令已经是现有命令或别名”?

问题描述

我只是在尝试制作 Discord 机器人,并尝试将此命令放在一个类别中,但是,无论我如何调用该命令,都会出现此错误。这是我的代码:

import discord,random
from discord.ext import commands 

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

@bot.event
async def on_ready():
    print("bot is ready for stuff")
    await bot.change_presence(activity=discord.Game(name=";help"))

class general_stuff(commands.Cog):
    """Stuff that's not important to the bot per say"""

    @bot.command()
    async def lkibashfjiabfiapbfaipb(self, message):
        await message.send("test received.")

bot.add_cog(general_stuff())
bot.run("TOKEN")

这是我回来的错误:

The command lkibashfjiabfiapbfaipb is already an existing command or alias.

无论我更改多少命令,它都会不断给出相同的错误。

标签: pythondiscorddiscord.py

解决方案


你在正确的轨道上。您收到错误的原因是当您启动程序时,它从顶部读取并向下运行;

class general_stuff(commands.Cog):
    """Stuff that's not important to the bot per se"""

    @bot.command() # Command is already registered here
    async def lkibashfjiabfiapbf(self, message):
        await message.send("test received.")

bot.add_cog(general_stuff()) 
# It tries to load the class general_stuff, but gets the error because
# it's trying to load the same command as it loaded before

@bot.command将该方法添加到机器人并加载它。使用 Cogs,您可以使用@commands.command(). 它只是将方法转换为命令,但不加载它。

您的代码应如下所示

...
class general_stuff(commands.Cog):
    """Stuff that's not important to the bot per se"""

    @commands.command()
    async def lkibashfjiabfiapbf(self, message):
        await message.send("test received.")
...

参考:


推荐阅读