how to add commands in cogs discord.py code example
Example 1: how to load a all cogs automatically discord.py
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
bot.load_extension('cogs.test')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
else:
print(f'Unable to load {filename[:-3]}')
bot.run(token)
Example 2: 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}")