discord.py cog
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.5
2
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!
2
0
4
3
# 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)
Thank you!
3
0
4
1
class Test(commands.cog):
def __init__(self, client):
self.client = client
self._last_member = None
Thank you!
1
0
4.25
4
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())
Thank you!
4
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types