Skip to content

Instantly share code, notes, and snippets.

@Copy-link
Forked from wiktorpp/cogs_slash_test.py
Created June 18, 2023 09:54
Show Gist options
  • Save Copy-link/8007e03fc87d6ecb25e8ee431f081e37 to your computer and use it in GitHub Desktop.
Save Copy-link/8007e03fc87d6ecb25e8ee431f081e37 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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment