-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.py
53 lines (41 loc) · 1.45 KB
/
loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from importlib import import_module
from os import system
from pkgutil import iter_modules, ModuleInfo
try:
from disnake.ext import commands # type: ignore
except ImportError:
from discord.ext import commands # type: ignore
def snake_to_pascal(string):
"""Convert a snake_case string to PascalCase."""
return "".join(x.capitalize() for x in string.split("_"))
class Color:
GREEN = "\033[92m"
RED = "\033[91m"
END = "\033[0m"
class Package:
def __init__(self, folder_name: str, module: ModuleInfo):
self.module = module
self.name = module.name
self.full = f"{folder_name}.{self.name}"
class Loader:
def __init__(self, bot: commands.Bot, folder_name: str = None):
self.bot = bot
self.folder_name = folder_name
system("")
@property
def packages(self):
for pkg in sorted(iter_modules([self.folder_name]), key=lambda x: x.name):
if not pkg.name.startswith("_"):
yield Package(self.folder_name, pkg)
else:
continue
def load_cogs(self):
for pkg in self.packages:
module = import_module(pkg.full)
cog_name = snake_to_pascal(pkg.name)
cog = getattr(module, cog_name, None)
if cog is not None:
self.bot.add_cog(cog(self.bot))
print(f"{Color.GREEN}Loaded COG: {Color.RED}{cog_name}{Color.END}")
else:
continue