-
-
Save wiktorpyk/2f7cba2fd33e21a9463fcd3fa7f96da6 to your computer and use it in GitHub Desktop.
Simple cogs example in discord.py (tested on 2.0.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
Thanks, this example is a lot clearer.