更改虚拟机后找不到命令错误

Command not found error after changing virtual machine

好的,在我切换到另一个虚拟机提供商之前,我有一个音乐机器人代码可以正常工作。所有要求都与我以前的虚拟机完全相同,因为我复制并粘贴了所有内容,包括 requirements.txt。机器人 运行 通常有 0 个错误,直到我尝试 运行 任何命令。它给了我这个错误:

discord.ext.commands.errors.CommandNotFound: Command "play" is not found

我试过回滚到我开始项目时使用的重写版本, 分配 bot = commands.Bot(command_prefix='prefix')

后将 @client.command 更改为 @bot.command
#I've assigned client = discord.ext.commands
@client.command(name='play', aliases=['sing'])
async def play(self, ctx, *, search: str):
    #then some code

更新 1:运行 它作为一个齿轮并凸起:

discord.ext.commands.errors.ExtensionFailed: Extension 'music' raised an error: TypeError: cogs must derive from Cog

更新 2:不知道为什么回滚重写版本不起作用。可能是我没做对。

只是简单地 运行 它作为一个齿轮。

请注意,齿轮的工作方式最近已更新:

https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html

如果您仍想 运行 它作为一个独立的机器人, 你的机器人应该看起来像这样:

from discord.ext.commands import Bot

bot = Bot("!")

@bot.command(name='play', aliases=['sing'])
async def play(ctx, *, search: str):  # Note no self
    #then some code

bot.run("token")

重要的是,您 运行 的机器人与您注册命令的机器人是同一个机器人。您还将 self 传递给您的机器人,即使它不在齿轮中,这没有意义。

好的,所以我找到了问题。 当我尝试 运行 它作为独立的机器人时,该机器人不起作用。 第一次将它用作齿轮的原因是因为。 discord.py 重写中改变了齿轮的工作方式。 这些是我所做的更改:

#in cogs/music.py
class Music:
    #Code

@bot.event
async def on_ready():
    print('Music bot online')

#in cogs/music.py
class Music(commands.Cog):
    #Code

@commands.Cog.listener()
async def on_ready():
    print('Music bot online')

感谢传奇人物@PatrickHaugh 帮助我解决这个问题。