首页 > 解决方案 > discord.py - 已经存在的命令或别名无缘无故地重复

问题描述

这是我的代码

main.py

import discord
from discord.ext import commands

for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    client.load_extension(f'cogs.{filename[:-3]}')

@client.event
async def on_message(message):
  if client.user == message.author:
    return
# The message logging command comes here
  await client.process_commands(message)


client.run("NeverShareTheToken")

./cogs/botinfo.py

import discord
from discord.ext import commands
from json import load as loadjson

class BotGeneralCommands(commands.Cog):
    def __init__(self, client: commands.Bot):
        self.client = client

        self.botconfigdata = loadjson(open("config.json", "r"))
        self.bot_inv_link = self.botconfigdata["invite-link"]

    @commands.command(aliases=["invite", "botlink", "invitelink"])
    async def invite(self, ctx):
        await ctx.send("```Hey there! Make sure you have me in your server too! Bot Invite link:```" + str(self.bot_inv_link))

def setup(client: commands.Bot):
    client.add_cog(BotGeneralCommands(client))

ERROR I GET, WHEN I RUN THE FILE

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 618, in _load_from_module_spec
    setup(self)
  File "/home/runner/Main-Discord-Bot-of-ZeaCeR5641/cogs/botinfo.py", line 79, in setup
    client.add_cog(BotGeneralCommands(client))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 507, in add_cog
    cog = cog._inject(self)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/cog.py", line 413, in _inject
    raise e
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/cog.py", line 407, in _inject
    bot.add_command(command)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1155, in add_command
    raise CommandRegistrationError(alias, alias_conflict=True)
discord.ext.commands.errors.CommandRegistrationError: The alias invite is already an existing command or alias.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 523, in <module>
    client.load_extension(f'cogs.{filename[:-3]}')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 678, in load_extension
    self._load_from_module_spec(spec, name)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 623, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.botinfo' raised an error: CommandRegistrationError: The alias invite is already an existing command or alias.

我从字面上检查了这个不和谐机器人和这个命令的所有其他齿轮(invite只存在于这里......)。为什么我不断收到此错误?

我所做的:我注释掉了那段代码,再次启动机器人,它给了我同样的错误,但命令名称不同,我评论了所有显示的命令 11 次(即使它们只在一个中定义地方 )。但我不断收到这个错误!

这个机器人有超过 240 条命令,它和问题有什么关系吗?

我能做些什么?

标签: pythonpython-3.xdiscorddiscord.py

解决方案


@commands.command当您在没有 kwarg 的情况下使用时,Discord.py 将函数名称作为命令名称name。所以既然你有:

@commands.command(aliases=["invite", "botlink", "invitelink"])
async def invite(self, ctx):

添加别名,然后invitediscord.py 库尝试添加invite为命令名称,因为它是函数的名称。您可以简单地删除邀请作为别名来解决问题:

@commands.command(aliases=["botlink", "invitelink"])
async def invite(self, ctx):

推荐阅读