Pycord Error: discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'

Pycord Error: discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'

我正在尝试加载一个 cog(位于 cogs 目录中),但每次我尝试加载它时,我都会收到以下错误消息:

discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'

我完全按照教程中的方式进行操作,但仍然无法正常工作。这是我的代码:

# main.py

import discord, os, dotenv

# setup
bot = discord.Bot(
    intents = discord.Intents.default(),
    prefix = "zrun "
)

@bot.event
async def on_ready():
    print(f'Connected as {bot.user}')

# load cmds
bot.load_extension('cogs.cmds')

# get token and run bot
dotenv.load_dotenv()
bot.run(os.getenv('TOKEN'))

cog 文件:

# cogs/cmds.py

import discord
from discord.ext import commands

class Cmds(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def echo(self, ctx, msg:str):
        """Sends the specified text in the chat"""

        await ctx.send(msg)

def setup(bot):
    bot.add_cog(Cmds(bot))

是的,我搜索了 StackOveflow,但没有任何与 cog 相关的答案解决了我的问题。

答案很简单。

discord.Bot 不支持前缀命令,仅支持应用程序命令(如斜线命令和上下文菜单)。

如果你想使用前缀命令,你首先需要通过输入 from discord.ext import commands 导入命令扩展,然后将 discord.Bot 切换到你定义机器人的 commands.Bot

还要确保有 message_content 意图,因为你需要它有前缀命令,我不确定 discord.Intents.default() 是否已更新以包含它。