v i i l k a s
v i i l k a s

Reputation: 49

discord.py rewrite: TypeError: cogs must derive from Cog

As my bot is getting bigger, I'm trying to implement cogs, however I have ran across a problem. I have my whole code set up and ready, but for some weird reason I keep getting this error:

    Traceback (most recent call last):
  File "C:\Users\Lauras\Desktop\Akagi Bot\main.py", line 107, in <module>
    bot.add_cog("cogs.fun")
  File "C:\Users\Lauras\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 477, in add_cog
    raise TypeError('cogs must derive from Cog')
TypeError: cogs must derive from Cog

My code on main.py looks like this:

   import discord
    import asyncio
    import typing
    import random
    import json
    import oauth
    from discord.ext import commands

bot = commands.Bot(command_prefix='~')

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Activity(name='with Kaga :3',type=0))
    print (discord.__version__)
    print(f"{bot.user.name} - {bot.user.id}")
    print ('Akagi is ready to serve the Commander :3 !')

    bot.add_cog("cogs.fun")
    bot.run(oauth.bot_token)

And the "fun" cog is as follows:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='~')

class FunCog:
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def hug(self, ctx):
        await ctx.send('has been hugged by', file=discord.File('iloveyou.gif'))
        pass


def setup(bot: commands.Bot):
    bot.add_cog(FunCog(bot))

What could be the problem? I'm also using discord.py rewrite. Thanks !

Upvotes: 4

Views: 12541

Answers (2)

MaxiMax
MaxiMax

Reputation: 71

I recommend checking out https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html This will help you get a better understanding of Cogs.

Firstly, you need to change bot.add_cog("cogs.fun") to bot.load_extension("cogs.fun")

This isn't necessary but you don't need to define bot again. Change def setup(bot: commands.Bot): to def setup(bot):

You will also need to change class FunCog: to class FunCog(commands.Cog):

I recommend staying up to date with changes when new updates come out for the rewrite version. Here is a quick look into an example of a working cog file.. Hope this helped! Max.

Upvotes: 4

v i i l k a s
v i i l k a s

Reputation: 49

Thank you to @Ellisein for helping me out with the class FunCog(commands.Cog): string of code. Another thing that helped me to fix the code was to replace bot.add_cog("cogs.fun") in the main.py with the bot.load_extension("cogs.fun") !

Upvotes: 0

Related Questions