discord.py cog classes
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
3.67
3
class Test(commands.cog):
def __init__(self, client):
self.client = client
self._last_member = None
Thank you!
3
0
4
3
import os
import discord
from discord.ext import commands
prefix = "?"
cogs_folder="./cogs"
bot = commands.Bot(command_prefix=prefix)
for file in cogs_folder:
if file.endswith(".py"):
try:
bot.load_extension(f"cogs.{file[:-3]}")
print(f"Loaded: {file}")
except:
print(f"Could not load: {file}")
Thank you!
3
0
Are there any code examples left?
New code examples in category Python