discord.py“命令已存在别名”错误,但它不是

2024-10-01 19:30:59 发布

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

我最近决定学习如何使用discord.py命令,添加命令时遇到了一些麻烦。无论我将命令更改为什么,当我尝试添加命令时,它总是给出相同的错误:

discord.ext.commands.errors.CommandRegistrationError: The command hellothisisacommand is already an existing command or alias.

问题是,它显然不是,而且无论我将命令更改为什么,它总是给出这个错误。这可能是什么原因造成的?代码如下

import discord
from discord.ext import commands
import os

intents = discord.Intents.default()
intents.members = True
intents.presences = True
client = discord.Client()
bot = commands.Bot(command_prefix = '/')


@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@bot.command()
async def hellothisisacommand(ctx):
    await print(True)
    
bot.add_command(hellothisisacommand)
client.run('token')

Tags: import命令clienttrueasyncdefbot错误
1条回答
网友
1楼 · 发布于 2024-10-01 19:30:59

使用bot.command()装饰器本质上等同于(并且排除了使用)bot.add_command

您可以使用commands.command()装饰器然后调用bot.add_command,也可以使用bot.command()装饰器,但不能同时使用两者

Read the docs for more info

以下是文档中的示例:

There are two ways of registering a command. The first one is by using Bot.command() decorator, as seen in the example above. The second is using the command() decorator followed by Bot.add_command() on the instance.

Essentially, these two are equivalent:

from discord.ext import commands

bot = commands.Bot(command_prefix='$')

@bot.command()
async def test(ctx):
    pass

# or:

@commands.command()
async def test(ctx):
    pass

bot.add_command(test)

相关问题 更多 >

    热门问题