【问题标题】:Why am I getting the error "The command is already an existing command or alias" when it shouldn't be?为什么我不应该收到错误“命令已经是现有命令或别名”?
【发布时间】:2021-03-23 15:22:20
【问题描述】:

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

无论我如何更改命令,它总是给出同样的错误。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

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

    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.")
    ...
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2022-10-01
      相关资源
      最近更新 更多