2020-03-16 21:02:05 -04:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
import glob
|
|
|
|
from importlib import import_module, reload
|
|
|
|
from os.path import basename, dirname, isfile
|
|
|
|
from re import escape
|
|
|
|
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
from .command_handler import CommandHandler
|
|
|
|
|
|
|
|
|
|
|
|
class Loader():
|
|
|
|
def __init__(self, client, logger, settings):
|
|
|
|
self.loaded_modules = []
|
|
|
|
self.all_modules = []
|
|
|
|
self.client = client
|
|
|
|
self.logger = logger
|
|
|
|
self.settings = settings
|
2020-04-16 16:26:05 -04:00
|
|
|
self.command_handler = CommandHandler(client, logger, settings)
|
2020-02-16 12:13:00 -05:00
|
|
|
self.botversion = "0.1.3"
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
def load_all_modules(self):
|
|
|
|
self._find_all_modules()
|
|
|
|
|
|
|
|
for module_name in self.all_modules:
|
|
|
|
self.loaded_modules.append(import_module("ubot.modules." + module_name))
|
|
|
|
|
|
|
|
def reload_all_modules(self):
|
|
|
|
self.command_handler.outgoing_commands = {}
|
|
|
|
self.command_handler.incoming_commands = {}
|
|
|
|
|
|
|
|
errors = ""
|
|
|
|
|
|
|
|
for module in self.loaded_modules:
|
|
|
|
try:
|
|
|
|
reload(module)
|
2020-04-17 11:29:58 -04:00
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
2023-10-16 09:46:31 -04:00
|
|
|
except Exception as exception:
|
|
|
|
errors += f"`Error while reloading {module.__name__} -> {exception}\n\n`"
|
|
|
|
raise exception
|
|
|
|
|
|
|
|
return errors or None
|
|
|
|
|
|
|
|
def add(self, **args):
|
|
|
|
def decorator(func):
|
2020-04-16 16:26:05 -04:00
|
|
|
self.command_handler.incoming_commands[args['pattern']] = {
|
|
|
|
"function": func,
|
|
|
|
"noprefix": args.get('noprefix', False),
|
|
|
|
"sudo": args.get('sudo', False)
|
|
|
|
}
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def _find_all_modules(self):
|
|
|
|
mod_paths = glob.glob(dirname(__file__) + "/modules/*.py")
|
|
|
|
|
|
|
|
self.all_modules = [
|
|
|
|
basename(f)[:-3] for f in mod_paths
|
|
|
|
if isfile(f) and f.endswith(".py")
|
|
|
|
]
|
|
|
|
|
|
|
|
system_index = self.all_modules.index("system")
|
|
|
|
self.all_modules.insert(0, self.all_modules.pop(system_index))
|