1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:23:12 +00:00
Lumi/loader.py

55 lines
1.9 KiB
Python

from loguru import logger
from discord.ext import commands
from lib.const import CONST
from pathlib import Path
import aiofiles.os
class CogLoader(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.cog_ignore_list: set[str] = CONST.COG_IGNORE_LIST
async def is_cog(self, path: Path) -> bool:
cog_name: str = path.stem
return (
path.suffix == ".py"
and cog_name not in self.cog_ignore_list
and not path.name.startswith("_")
and await aiofiles.os.path.isfile(path)
)
async def load_cogs(self, path: Path) -> None:
try:
if await aiofiles.os.path.isdir(path):
for item in path.iterdir():
try:
await self.load_cogs(path=item)
except Exception as e:
logger.exception(f"Error loading cog from {item}: {e}")
elif await self.is_cog(path):
relative_path: Path = path.relative_to(Path(__file__).parent)
module: str = (
str(relative_path).replace("/", ".").replace("\\", ".")[:-3]
)
try:
await self.bot.load_extension(name=module)
logger.debug(f"Loaded cog: {module}")
except Exception as e:
logger.exception(f"Error loading cog: {module}. Error: {e}")
except Exception as e:
logger.exception(f"Error loading cogs from {path}: {e}")
async def load_cog_from_dir(self, dir_name: str) -> None:
path: Path = Path(__file__).parent / dir_name
await self.load_cogs(path)
@classmethod
async def setup(cls, bot: commands.Bot) -> None:
cog_loader = cls(bot)
await cog_loader.load_cog_from_dir(dir_name="modules")
await bot.add_cog(cog_loader)