1from discord.ext import commands
2
3class Test_Cog(commands.Cog):
4 def __init__(self, bot):
5 self.bot = bot # defining bot as global var in class
6
7 @commands.Cog.listener() # this is a decorator for events/listeners
8 async def on_ready(self):
9 print('Bot is ready!.')
10
11 @commands.command() # this is for making a command
12 async def ping(self, ctx):
13 await ctx.send(f'Pong! {round(self.bot.latency * 1000)}')
14
15def setup(bot): # a extension must have a setup function
16 bot.add_cog(Test_Cog(bot)) # adding a cog
1# bot file
2import os
3from discord.ext import commands
4
5bot = commands.Bot(command_prefix='.')
6# I am assuming that you have a test.py cog in a cogs folder
7bot.load_extension('cogs.test') # this is good but you can make it better
8
9for filename in os.listdir('./cogs'):
10 if filename.endswith('.py'):
11 bot.load_extension(f'cogs.{filename[:-3]}')
12
13 else:
14 print(f'Unable to load {filename[:-3]}')
15
16bot.run(token)
1from discord.ext import commands
2
3class Ping(commands.Cog):
4 """Receives ping commands"""
5
6 @commands.command()
7 async def ping(self, ctx: commands.Context):
8 await ctx.send("Pong")
9
10def setup(bot: commands.Bot):
11 bot.add_cog(Ping())
1class Test(commands.cog):
2
3 def __init__(self, client):
4 self.client = client
5 self._last_member = None
1import os
2import discord
3from discord.ext import commands
4
5prefix = "?"
6cogs_folder="./cogs"
7
8bot = commands.Bot(command_prefix=prefix)
9
10for file in cogs_folder:
11 if file.endswith(".py"):
12 try:
13 bot.load_extension(f"cogs.{file[:-3]}")
14 print(f"Loaded: {file}")
15 except:
16 print(f"Could not load: {file}")