2023-10-16 09:46:31 -04:00
|
|
|
import glob
|
2020-05-16 22:50:25 -04:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2020-12-04 21:03:55 -05:00
|
|
|
from functools import partial
|
2023-10-16 09:46:31 -04:00
|
|
|
from importlib import import_module, reload
|
2024-08-02 19:38:55 -04:00
|
|
|
from logging import Logger
|
2023-10-16 09:46:31 -04:00
|
|
|
from os.path import basename, dirname, isfile
|
2023-06-08 08:36:25 -04:00
|
|
|
from traceback import print_exc
|
2024-08-02 19:38:55 -04:00
|
|
|
from typing import TYPE_CHECKING
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-09 13:15:08 -04:00
|
|
|
from aiohttp import ClientSession
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2024-08-02 19:38:55 -04:00
|
|
|
from ubot.settings import Settings
|
|
|
|
|
2020-07-23 10:03:34 -04:00
|
|
|
from .cache import Cache
|
2020-09-15 20:02:44 -04:00
|
|
|
from .command import (CallbackQueryCommand, Command, InlineArticleCommand,
|
|
|
|
InlinePhotoCommand)
|
2023-10-16 09:46:31 -04:00
|
|
|
from .command_handler import CommandHandler
|
2020-08-24 12:07:23 -04:00
|
|
|
from .database import Database
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2024-08-02 19:38:55 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ubot import MicroBot
|
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
class Loader():
|
2024-08-02 19:38:55 -04:00
|
|
|
thread_pool: ThreadPoolExecutor = ThreadPoolExecutor()
|
|
|
|
db: Database = Database()
|
|
|
|
micro_bot: "MicroBot"
|
|
|
|
settings: Settings
|
|
|
|
logger: Logger
|
|
|
|
command_handler: CommandHandler
|
|
|
|
aioclient: ClientSession
|
|
|
|
cache: Cache
|
2020-07-18 15:11:09 -04:00
|
|
|
|
|
|
|
loaded_modules = []
|
|
|
|
all_modules = []
|
|
|
|
|
2023-10-16 14:32:57 -04:00
|
|
|
def __init__(self, micro_bot):
|
|
|
|
self.micro_bot = micro_bot
|
|
|
|
self.settings = micro_bot.settings
|
|
|
|
self.logger = micro_bot.logger
|
2024-08-02 19:38:55 -04:00
|
|
|
|
|
|
|
async def _initialize_loader(self):
|
|
|
|
self.aioclient = ClientSession()
|
|
|
|
self.cache = Cache(self.aioclient)
|
2023-10-16 14:32:57 -04:00
|
|
|
self.command_handler = CommandHandler(self)
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
def load_all_modules(self):
|
|
|
|
self._find_all_modules()
|
|
|
|
|
|
|
|
for module_name in self.all_modules:
|
2020-06-24 14:21:58 -04:00
|
|
|
try:
|
|
|
|
self.loaded_modules.append(import_module("ubot.modules." + module_name))
|
|
|
|
except Exception as exception:
|
|
|
|
self.logger.error(f"Error while loading {module_name}: {exception}")
|
2023-06-08 08:36:25 -04:00
|
|
|
print_exc()
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
def reload_all_modules(self):
|
2025-06-19 07:57:37 -04:00
|
|
|
self.command_handler.clear_commands()
|
2023-10-16 09:46:31 -04:00
|
|
|
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
|
|
|
|
|
2025-06-19 07:57:37 -04:00
|
|
|
def add(self, pattern: str = None, lenient: bool = False, **args):
|
2025-06-19 07:31:45 -04:00
|
|
|
def decorator(func):
|
|
|
|
args["pattern"] = args.get("pattern", pattern)
|
2025-06-19 07:57:37 -04:00
|
|
|
self.command_handler.push_incoming_command(Command(func, args), lenient)
|
2025-06-19 07:31:45 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2025-06-19 07:57:37 -04:00
|
|
|
def add_list(self, pattern: list = None, lenient: bool = False, **args):
|
2020-06-16 17:18:11 -04:00
|
|
|
pattern_list = args.get("pattern", pattern)
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
for pattern in pattern_list:
|
2020-09-15 20:02:44 -04:00
|
|
|
this_args = args.copy()
|
|
|
|
this_args["pattern"] = pattern
|
2025-06-19 07:57:37 -04:00
|
|
|
self.command_handler.push_incoming_command(Command(func, this_args), lenient)
|
2020-08-24 09:33:53 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2025-06-19 07:59:25 -04:00
|
|
|
def add_dict(self, pattern: dict = None, lenient: bool = False, **args):
|
2020-08-24 09:33:53 -04:00
|
|
|
pattern_dict = args.get("pattern", pattern)
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
for pattern, extra in pattern_dict.items():
|
2020-10-03 17:18:32 -04:00
|
|
|
if isinstance(pattern, tuple):
|
|
|
|
for patt in pattern:
|
|
|
|
this_args = args.copy()
|
|
|
|
this_args["pattern"] = patt
|
|
|
|
this_args["extra"] = args.get('extra', extra)
|
2025-06-19 07:59:25 -04:00
|
|
|
self.command_handler.push_incoming_command(Command(func, this_args), args.get("lenient", lenient))
|
2020-10-03 17:18:32 -04:00
|
|
|
else:
|
|
|
|
this_args = args.copy()
|
|
|
|
this_args["pattern"] = pattern
|
|
|
|
this_args["extra"] = args.get('extra', extra)
|
2025-06-19 07:59:25 -04:00
|
|
|
self.command_handler.push_incoming_command(Command(func, this_args), args.get("lenient", lenient))
|
2020-06-16 17:18:11 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2020-05-21 13:53:11 -04:00
|
|
|
def add_inline_photo(self, pattern=None, **args):
|
2020-05-16 17:52:29 -04:00
|
|
|
def decorator(func):
|
2020-09-15 20:02:44 -04:00
|
|
|
args["pattern"] = args.get("pattern", pattern)
|
|
|
|
self.command_handler.inline_photo_commands.append(InlinePhotoCommand(func, args))
|
2020-05-16 17:52:29 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
def add_inline_article(self, pattern=None, **args):
|
|
|
|
def decorator(func):
|
2020-09-15 20:02:44 -04:00
|
|
|
args["pattern"] = args.get("pattern", pattern)
|
|
|
|
self.command_handler.inline_article_commands.append(InlineArticleCommand(func, args))
|
2020-05-25 14:04:56 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2020-06-15 14:23:24 -04:00
|
|
|
def add_callback_query(self, data_id=None, **args):
|
|
|
|
def decorator(func):
|
2020-09-15 20:02:44 -04:00
|
|
|
args["data_id"] = args.get("data_id", data_id)
|
|
|
|
self.command_handler.callback_queries.append(CallbackQueryCommand(func, args))
|
2020-06-15 14:23:24 -04:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2020-09-18 20:22:38 -04:00
|
|
|
def get_cmds_by_func(self, func) -> list:
|
2025-06-19 07:57:37 -04:00
|
|
|
return [i for i in self.command_handler.all_incoming_commands if i.function == func]
|
2020-09-18 20:28:07 -04:00
|
|
|
|
|
|
|
def get_cbs_by_func(self, func) -> list:
|
|
|
|
return [i for i in self.command_handler.callback_queries if i.function == func]
|
2020-09-18 20:22:38 -04:00
|
|
|
|
2020-12-04 21:03:55 -05:00
|
|
|
async def run_async(self, function, *args):
|
2023-10-16 14:32:57 -04:00
|
|
|
return await self.micro_bot.client.loop.run_in_executor(self.thread_pool, partial(function, *args))
|
2020-12-04 21:03:55 -05:00
|
|
|
|
2020-06-23 15:28:09 -04:00
|
|
|
def prefix(self):
|
2023-10-16 14:32:57 -04:00
|
|
|
return ", ".join(self.micro_bot.settings.get_list("hard_cmd_prefix") or ["/"])
|
2020-06-23 15:28:09 -04:00
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
def _find_all_modules(self):
|
2020-06-24 14:13:40 -04:00
|
|
|
module_paths = glob.glob(dirname(__file__) + "/modules/*.py")
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-06-24 14:13:40 -04:00
|
|
|
self.all_modules = sorted([
|
|
|
|
basename(f)[:-3] for f in module_paths
|
2023-10-16 09:46:31 -04:00
|
|
|
if isfile(f) and f.endswith(".py")
|
2020-06-24 14:13:40 -04:00
|
|
|
])
|