You need to enable JavaScript to run this app. 使用discord.py Cogs组织Bot时加载扩展报TypeError的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

使用discord.py Cogs组织Bot时加载扩展报TypeError的问题求助

报错核心原因

你遇到的TypeError本质是对discord.py对象的引用拼写错误:
在Cats.py中写的class Images(commands.cog):,这里全小写的cog是commands包下的一个子模块(负责存放Cog相关的实现逻辑),不是可被继承的类。Python执行类创建逻辑时,尝试把模块当父类做初始化,就会抛出“module() takes at most 2 arguments (3 given)”的报错。
discord.ext.commands 提供的、供自定义Cog继承的基类是大写C开头的commands.Cog,二者虽然拼写接近,但完全是不同的对象。

修复方案
  1. 修正Cats.py中的基类大小写,移除代码中未实际使用的冗余导入
  2. 如果你使用的是discord.py 2.0及以上版本,将同步的setup函数替换为异步写法,适配新版API规范;如果仍在使用discord.py 1.x版本,保留原有同步setup逻辑即可
    修改后的完整Cats.py代码如下:
import aiohttp
import discord
from discord.ext import commands

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

    @commands.command()
    async def meow(self, ctx):
        async with ctx.channel.typing():
            async with aiohttp.ClientSession() as cs:
                async with cs.get("http://aws.random.cat/meow") as r:
                    data = await r.json()

                    em = discord.Embed(title="Meow")
                    em.set_image(url=data['file'])

                    await ctx.send(embed=em)

async def setup(bot):
    await bot.add_cog(Images(bot))
额外排查点
  • 检查同目录下tictactoe.py、gamble.py两个cog文件,确认没有同样的小写commands.cog拼写错误,避免加载时报同类问题
  • 确认cogs文件夹根目录下存在空的__init__.py文件,保证Python能正确识别该目录为可导入的Python包
  • 你在main.py中写的扩展加载逻辑本身没有问题,不需要调整

内容的提问来源于stack exchange,提问作者UrTech Tips

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.10 02:36:04