exe,
There are these things called "cogs", you can have a cog for events and some for other categories.
Here's an example.
import discord.py
from discord.ext import commands, tasks
class ExampleCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command() # You use commands.command() instead of bot.command()
async def test(self, ctx): # you must always have self, if not it will not work.
await ctx.send("**Test**")
@commands.Cog.listener() # You use commands.Cog.listener() instead of bot.event
async def on_ready(self):
print("Test")
def setup(bot):
bot.add_cog(ExampleCog(bot))
Whenever you use bot
replace it with self.bot
when you're using cogs.
When you are using cogs, you need to have the cog files in a separate folder.
You should make a folder called "./cogs/examplecog.py
"
In your main bot file, you should have the code that is written below in order for the bot to read the cog files.
for file in os.listdir("./cogs"): # lists all the cog files inside the cog folder.
if file.endswith(".py"): # It gets all the cogs that ends with a ".py".
name = file[:-3] # It gets the name of the file removing the ".py"
bot.load_extension(f"cogs.{name}") # This loads the cog.
A benefit of having cogs is you don't need to restart the bot every time you want the new code to work, you just do !reload <cogname>
, !unload <cogname>
, or !load <cogname>
and for those commands to work you need the code that is below.
Reload Cog Below
@bot.command()
@commands.is_owner()
async def reload(ctx, *, name: str):
try:
bot.reload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog reloaded')
Unload Cog Below
@bot.command()
@commands.is_owner()
async def unload(ctx, *, name: str):
try:
bot.unload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog unloaded')
Load Cog Below
@bot.command()
@commands.is_owner()
async def load(ctx, *, name: str):
try:
bot.load_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog loaded')
I hope you understood all of this.
it took me about an hour to write this.
You can get more help on Discord.py on this Discord Server
Finally, Have a nice day, and best of luck with your bot.