monolith/legacy/utils/loaders/command_loader.py

26 lines
1.0 KiB
Python
Raw Normal View History

from os import listdir
2022-07-21 12:40:09 +00:00
from main import CommandMap
2022-07-21 12:39:59 +00:00
from utils.logging.debug import debug
2022-07-21 12:40:05 +00:00
from utils.logging.log import error
2022-07-21 12:39:41 +00:00
def loadCommands(allowDup=False):
for filename in listdir("commands"):
if filename.endswith(".py") and filename != "__init__.py":
commandName = filename[0:-3]
2022-07-21 12:39:41 +00:00
className = commandName.capitalize() + "Command"
2022-07-21 12:40:03 +00:00
# try:
module = __import__("commands.%s" % commandName)
2022-07-21 12:40:05 +00:00
if commandName not in CommandMap:
2022-07-21 12:40:03 +00:00
CommandMap[commandName] = getattr(getattr(module, commandName), className)
debug("Registered command: %s" % commandName)
else:
if allowDup:
CommandMap[commandName] = getattr(getattr(module, commandName), className)
debug("Registered command: %s" % commandName)
2022-07-21 12:40:03 +00:00
error("Duplicate command: %s" % (commandName))
# except Exception as err:
# error("Exception while loading command %s:\n%s" % (commandName, err))