python - 为什么我不应该收到错误“命令已经是现有命令或别名”?
问题描述
我只是在尝试制作 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.
无论我更改多少命令,它都会不断给出相同的错误。
解决方案
你在正确的轨道上。您收到错误的原因是当您启动程序时,它从顶部读取并向下运行;
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.")
...
参考:
推荐阅读
- php - 使用 PHP 自动下载文件 .ZIP
- amazon-web-services - AWS Transfer Family API Gateway 身份提供商是否需要使用 Terraforms `aws_apigatewayv2_api` 或`aws_api_gateway_rest_api`?
- javascript - 使用onclick事件在div标签中附加CKEditor?
- amazon-web-services - 从本地机器设置到 api 网关的隧道
- azure - Azure 函数在本地运行,从门户运行时失败
- kubernetes - 完整的 pod 状态列表
- reactjs - 如何在 cra 中使用 Nextjs
- python - 通过 API 的 CSV - 只能连接 str
- python - 如何通过使用 python 给出条件来自动复制文件?
- javascript - 如何使用 React 显示图像上传预览?