Skip to content

Instantly share code, notes, and snippets.

@15696
Last active June 18, 2024 07:23
Show Gist options
  • Save 15696/a1b10f044fbd658ce76ab1f862a1bda2 to your computer and use it in GitHub Desktop.
Save 15696/a1b10f044fbd658ce76ab1f862a1bda2 to your computer and use it in GitHub Desktop.
simple cogs example in discord.py
# main.py
from discord.ext import commands
import os
client = commands.Bot(command_prefix = "!")
for f in os.listdir("./cogs"):
if f.endswith(".py"):
client.load_extension("cogs." + f[:-3])
client.run("token")
# cogs / test.py
from discord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client # sets the client variable so we can use it in cogs
@commands.Cog.listener()
async def on_ready(self):
# an example event with cogs
@commands.command()
async def command(self, ctx):
# an example command with cogs
def setup(client):
client.add_cog(Test(client))
@Honkou
Copy link

Honkou commented May 13, 2023

@bot.event
async def setup_hook() -> None:
for extension in extensions:
await bot.load_extension(extension)

That's what I was missing in all the official and fancy guidelines.
Thank you, sir, you are a true savior!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment