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.

我确实检查了这个不和谐bot和这个命令的每个其他齿轮(invite只存在于这里…)。为什么我一直得到这个错误?

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

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

我能做什么?

当您使用@commands.command而不使用namekwarg时,Discord.py将函数名作为命令名。既然你有:

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

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

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

最新更新