You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人启动报错TypeError: Cogs must derive from cog 求助

Fixing "TypeError: Cogs must derive from cog" in Your Discord Bot

Hey there! Let's get that frustrating cog error sorted out for you. That TypeError is telling you loud and clear: your Discord bot's cog class isn't properly built on the required base class from discord.py.

Here's what's going wrong (and how to fix it)

The core issue here is that all cogs in discord.py must inherit from commands.Cog. If your class doesn't do this, the bot has no way of recognizing it as a valid cog. Let's walk through the key fixes:

  1. Make sure your cog class inherits from commands.Cog
    This is the most common culprit. Your cog class definition needs to look like this (capital "C" in Cog matters!):

    from discord.ext import commands
    
    # Correct way to define a cog class
    class ExampleCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        # Add your commands, events, etc. here
    
  2. Double-check your setup function
    Every cog file needs a setup function that registers the cog with your bot. It should look exactly like this:

    def setup(bot):
        # Pass your cog class instance to the bot
        bot.add_cog(ExampleCog(bot))
    
  3. Verify your imports are correct
    Make sure you're importing commands properly from discord.ext—typos here (like misspelling discord or commands) can break the whole thing. No shortcuts here, just the full import line: from discord.ext import commands.

Quick check for your code

Even without diving into your full code, these are the first things to look for:

  • Did you forget to add (commands.Cog) after your cog class name?
  • Is your setup function missing, or does it not call bot.add_cog() with your cog instance?
  • Did you accidentally misspell Cog (using lowercase "c" instead of uppercase)?

Fix those points, restart your bot, and that error should vanish.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 09:42:31