discord.py rewrite: typeerror: cogs must derive from cog

  • Last Update :
  • Techknowledgy :

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>
       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 = '~')

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


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

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

def setup(bot: commands.Bot):

Suggestion : 2

The name of the cog is automatically derived from the class name but can be overridden. See Meta Options.,All commands must now take a self parameter to allow usage of instance attributes that can be used to maintain state.,Just as we remove a cog by its name, we can also retrieve it by its name as well. This allows us to use a cog as an inter-command communication protocol to share data. For example:,Note that we reference the cog by name, which we can override through Meta Options. So if we ever want to remove the cog eventually, we would have to do the following.

class Greetings(commands.Cog):
   def __init__(self, bot):
   self.bot = bot
self._last_member = None

async def on_member_join(self, member):
   channel = member.guild.system_channel
if channel is not None:
   await channel.send(f 'Welcome {member.mention}.')

async def hello(self, ctx, *, member: discord.Member = None):
"Says hello"
member = member or ctx.author
if self._last_member is None or self._last_member.id != member.id:
   await ctx.send(f 'Hello {member.name}~')
   await ctx.send(f 'Hello {member.name}... This feels familiar.')
self._last_member = member
await bot.add_cog(Greetings(bot))
await bot.remove_cog('Greetings')
class Economy(commands.Cog):

   async def withdraw_money(self, member, money):
      # implementation here

      async def deposit_money(self, member, money):
         # implementation here

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

def coinflip(self):
   return random.randint(0, 1)

async def gamble(self, ctx, money: int):
"Gambles some money."
economy = self.bot.get_cog('Economy')
if economy is not None:
   await economy.withdraw_money(ctx.author, money)
if self.coinflip() == 1:
   await economy.deposit_money(ctx.author, money * 1.5)
class MyCog(commands.Cog, name = 'My Cog'):
>>> cog = bot.get_cog('Greetings') >>>
   commands = cog.get_commands() >>>
      for c in commands

Suggestion : 3

GatewayNotFound – The gateway to connect to Discord is not found. Usually if this is thrown then there is a Discord API outage.,Represents a client connection that connects to Discord. This class is used to interact with the Discord WebSocket and API.,TypeError – The cog does not inherit from Cog.,This is only for the messages received from the client WebSocket. The voice WebSocket will not trigger this event.

async def on_ready():
   for guild in client.fetch_guilds(limit = 150):
guilds = await client.fetch_guilds(limit = 150).flatten()
# guilds is now a list of Guild...
loop.run_until_complete(start( * args, ** kwargs))
except KeyboardInterrupt:
# cancel all tasks lingering
for guild in client.guilds:
   for channel in guild.channels:
   yield channel
for guild in client.guilds:
   for member in guild.members:
   yield member

Suggestion : 4

how to make it that your discord bot can autorole in the discord.py rewrite , how to let only admins do a command in discord.py , how to send a message in a specific channel discord.py , discord.py get list of availbale commands

# bot file
import os
from discord.ext
import commands

bot = commands.Bot(command_prefix = '.')
# I am assuming that you have a test.py cog in a cogs folder
bot.load_extension('cogs.test') # this is good but you can make it better

for filename in os.listdir('./cogs'):
   if filename.endswith('.py'):
   bot.load_extension(f 'cogs.{filename[:-3]}')

   print(f 'Unable to load {filename[:-3]}')

from discord.ext
import commands

class Test_Cog(commands.Cog):
   def __init__(self, bot):
   self.bot = bot # defining bot as global
var in class

@commands.Cog.listener() # this is a decorator
for events / listeners
async def on_ready(self):
   print('Bot is ready!.')

@commands.command() # this is
for making a command
async def ping(self, ctx):
   await ctx.send(f 'Pong! {round(self.bot.latency * 1000)}')

def setup(bot): # a extension must have a setup
bot.add_cog(Test_Cog(bot)) # adding a cog
ListOfCogs = client.cogs # this is a dictionary!

for NameOfCog, TheClassOfCog in ListOfCogs.items(): # we can loop trough it!