Ejemplo 1: cómo hacer un engranaje discord.py
from discord.ext import commands
class Test_Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot # defining bot as global var in class
@commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self):
print('Bot is ready!.')
@commands.command() # this is for making a command
async def ping(self, ctx):
await ctx.send(f'Pong! {round(self.bot.latency * 1000)}')
def setup(bot): # a extension must have a setup function
bot.add_cog(Test_Cog(bot)) # adding a cog
Ejemplo 2: cómo cargar todos los engranajes automáticamente 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)
Ejemplo 3: obtener cogs discord.py
ListOfCogs = client.cogs # this is a dictionary!
print(len(ListOfCogs))
for NameOfCog,TheClassOfCog in ListOfCogs.items(): # we can loop trough it!
print(NameOfCog)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)