python discord bot cogs code example

Example 1: discord.py cogs example

import os
import discord
from discord.ext import commands

prefix = "?"
cogs_folder="./cogs"

bot = commands.Bot(command_prefix=prefix)

for file in cogs_folder:
  if file.endswith(".py"):
    try:
      bot.load_extension(f"cogs.{file[:-3]}")
      print(f"Loaded: {file}")
    except:
      print(f"Could not load: {file}")

Example 2: get cogs discord.py

ListOfCogs = client.cogs # this is a dictionary!
print(len(ListOfCogs))
    
for NameOfCog,TheClassOfCog in ListOfCogs.items(): # we can loop trough it!
	print(NameOfCog)