I have a discord bot that is quite large and for readability, i would like to organize into folders.
I have the main file, and the settings file. i am receiving HTTP responses from the main file right now, which is quite hard to read when going through the file. Ideally, I would like the http requests and commands in a different location... Although this is mostly personal preference i figured someone could show me how theirs is organized. I also have functions that do not fit in any such as an embed builder function
You can use cogs to separate your commands into categories/files.
On you main bot file (bot.py or main.py) write the code that imports os and that loads your cogs files.
On your bot's root folder create a folder called cogs and then a file with the name you want for example utility and give the .py extension, after that the name of the file (utility.py in this case) is the one that you gonna put on the list (list marked with # list)
import discord
import os
# list
initial_extensions = [
'cogs.utilty',
]
# the code to load all your cogs
for extension in initial_extensions:
try:
client.load_extension(extension)
except Exception as e:
print(f'Failed to load extension {extension}.', file=sys.stderr)
traceback.print_exc()
client.run('token')
Now on your cogs file (utility.py) write the next code.
import discord
from discord import client
import os
from discord.ext import commands
class Utility(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print('Bot is ready!')
Do not forget that you probably need to rewrite most of your code but it's simple things. Hope this helps : ) Docs: https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html