Skip to content

Instantly share code, notes, and snippets.

@wiktorpp
Forked from 15696/cogs.py
Last active September 26, 2023 20:28
Show Gist options
  • Save wiktorpp/2f7cba2fd33e21a9463fcd3fa7f96da6 to your computer and use it in GitHub Desktop.
Save wiktorpp/2f7cba2fd33e21a9463fcd3fa7f96da6 to your computer and use it in GitHub Desktop.
Simple cogs example in discord.py (tested on 2.0.1)
#cogs / test.py
from discord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client
@commands.hybrid_command()
@commands.cooldown(1, 10, commands.BucketType.user)
async def hello(self, ctx):
await ctx.reply(f"Hello {ctx.author.mention}")
async def setup(client):
await client.add_cog(Test(client))
# main.py
import discord
from discord.ext import commands
import os
class Client(commands.Bot):
def __init__(self):
super().__init__(
command_prefix = commands.when_mentioned_or("&"),
intents = discord.Intents.all(),
help_command = commands.DefaultHelpCommand(dm_help=True)
)
async def setup_hook(self): #overwriting a handler
print(f"\033[31mLogged in as {client.user}\033[39m")
cogs_folder = f"{os.path.abspath(os.path.dirname(__file__))}/cogs"
for filename in os.listdir(cogs_folder):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
await client.tree.sync()
print("Loaded cogs")
client = Client()
client.run(os.getenv("TOKEN"))
@icebarf
Copy link

icebarf commented Mar 13, 2023

Thanks, this example is a lot clearer.

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