为什么我会收到错误“该命令已经是一个现有的命令或别名”,而它不应该是?

2024-10-01 04:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是试着制作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.

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


Tags: import命令asyncisdefbot错误await
1条回答
网友
1楼 · 发布于 2024-10-01 04:54:32

你在正确的轨道上。出现错误的原因是,当你启动程序时,它从顶部读取,然后向下运行

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将方法添加到bot并加载它。使用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.")
...

参考资料:

相关问题 更多 >