Answers for "how to use discord.py cogs in other files"

1

how to load a all cogs automatically discord.py

# bot file
import os
from discord.ext import commands

bot = commands.Bot(command_prefix='.')
# I am assuming that you have a test.py cog in a cogs folder
bot.load_extension('cogs.test') # this is good but you can make it better

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)
Posted by: Guest on October-12-2020
2

discord.py cog

from discord.ext import commands

class Ping(commands.Cog):
    """Receives ping commands"""
    
    @commands.command()
    async def ping(self, ctx: commands.Context):
        await ctx.send("Pong")

def setup(bot: commands.Bot):
    bot.add_cog(Ping())
Posted by: Guest on June-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language