当前位置: 首页 > 知识库问答 >
问题:

不一致py-已存在命令或别名,无需重复

仇征
2023-03-14

这是我的密码

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

错误我得到,当我运行文件

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.

我确实检查了这个discord bot和这个命令的每个其他cog(invite仅存在于此…)。为什么我总是犯这个错误?

我所做的:我注释掉了那段代码,再次启动bot,它给了我相同的错误,但是命令名不同,我注释了所有显示的命令11次(尽管它们只在一个地方定义)。但我总是犯这样的错误!

这个机器人有超过240个命令,它与问题有关吗?

我能做什么?

共有1个答案

东郭展
2023-03-14

不一致当您使用@命令时,py将函数名作为命令名。命令不带名称kwarg。既然你已经:

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

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

@commands.command(aliases=["botlink", "invitelink"])
async def invite(self, ctx):
 类似资料:
  • 上面的代码正在运行。 我没有得到任何错误,但我也希望机器人显示他们是afk的原因,当他们提到。有人能帮忙吗?

  • 我在我的discord机器人上遇到了一个问题。命令不起作用。我认为代码不是问题。除了命令外,一切都很完美。不和谐,如果我使用!在这里测试mytext,什么都不会发生。 提前谢谢你!

  • 我是discord API的新手,我很难弄清楚为什么我的命令不能被识别。我已经通读了文档,但我不确定该去哪里看。任何帮助都将不胜感激。不要介意硬编码的列表。我计划将来改变这一点。现在我只想确保它能正常工作。

  • 我一直在用discord编程一个机器人。py(重写分支)和我想添加一个ban命令。bot仍然没有禁止该成员,只是显示了一个错误: 它将禁止ping用户并确认它确实禁止了ping

  • 我得到了一个unban命令代码,在控制台中出现以下错误: (节点:9348)未处理的PromisejectionWarning:TypeError:无法读取未定义的at对象的属性“member”。在客户端执行(C:\Users\19nik\Documents\GitHub\bot project\commands\unban.js:9:22)。(C:\Users\19nik\Documents\G

  • 最近,我开始研究一个不和谐的机器人,并添加了斜杠命令。 这是我的interactionHandler.js 有没有办法删除命令,因为我找不到方法。我想得到命令的ID,但是我不知道怎么做。感谢所有的帮助者:)Discord.js v13