I m new to discord.py too but here i m sharing my little knowledge! [Sorry if i have some problems in my vocab!]
Cogs are like subclasses that helps you to organise your code and distribute your code in different sections, but still having a single bot and not to have a very long long code with different types of commands in a single file.
More Info At: Discord.py Documentation For Cogs
Create a folder named cogs
in directory same as of your bot.py
To load and unload cogs using command you can use this code:
import discord
from discord.ext import commands
import os
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
This will load cogs whenever you make one.
- Create a cog example [example.py] in
cogs
folder.
- You need to define class cog at beginning of every cog and define cog setup at end of every cog as such:
import discord
class Example(commands.Cog): #Here Example is name of cog example.py
def __init__(self, client):
self.client = client
#Your code
#Your code
#Your code
def setup(client):
client.add_cog(Example(client)) #Here Example is name of cog example.py
Just Use load command once if it doesnt load at first.
Cogs will automatically load as soon as you restart bot
or you can simply unload and load them by using load [cogname]
and unload [cogname]
command.
@client.event
changes to @commands.Cog.listener
decorator
@client.commands
changes to @commands.command
decorator
Please Forgive Me For Any Mistakes And Mislead ^^!
Credits : A Simple Tutorial By Lucas On Youtube!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…