discord py load cogds code example
Example 1: how to make a cog discord.py
from discord.ext import commands
class Test_Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print('Bot is ready!.')
@commands.command()
async def ping(self, ctx):
await ctx.send(f'Pong! {round(self.bot.latency * 1000)}')
def setup(bot):
bot.add_cog(Test_Cog(bot))
Example 2: how to load a all cogs automatically discord.py
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
bot.load_extension('cogs.test')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
else:
print(f'Unable to load {filename[:-3]}')
bot.run(token)