2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-22 18:19:16 +00:00
microbot/ubot/loader.py

176 lines
5.4 KiB
Python
Raw Normal View History

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
2020-05-16 22:50:25 -04:00
from concurrent.futures import ThreadPoolExecutor
2023-10-16 09:46:31 -04:00
from importlib import import_module, reload
from os.path import basename, dirname, isfile
2020-05-09 13:15:08 -04:00
from aiohttp import ClientSession
2020-08-24 12:07:23 -04:00
from telethon.tl.types import DocumentAttributeFilename
2023-10-16 09:46:31 -04:00
2020-07-23 10:03:34 -04:00
from .cache import Cache
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
class Loader():
2020-07-18 15:11:09 -04:00
aioclient = ClientSession()
thread_pool = ThreadPoolExecutor()
2020-07-23 10:03:34 -04:00
cache = Cache(aioclient)
2020-08-24 12:07:23 -04:00
db = Database()
2020-07-18 15:11:09 -04:00
loaded_modules = []
all_modules = []
2023-10-16 09:46:31 -04:00
def __init__(self, client, logger, settings):
self.client = client
self.logger = logger
self.settings = settings
2020-07-18 15:11:09 -04:00
self.command_handler = CommandHandler(client, settings, 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:
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-10-16 09:46:31 -04:00
def reload_all_modules(self):
2020-06-16 17:00:09 -04:00
self.command_handler.incoming_commands = []
self.command_handler.inline_photo_commands = []
self.command_handler.inline_article_commands = []
self.command_handler.callback_queries = []
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
2020-08-24 09:33:53 -04:00
def add(self, pattern: str = None, **args):
2023-10-16 09:46:31 -04:00
def decorator(func):
args["pattern"] = args.get("pattern", pattern)
self.command_handler.incoming_commands.append(Command(func, args))
2023-10-16 09:46:31 -04:00
2020-04-17 16:34:12 -04:00
return func
2023-10-16 09:46:31 -04:00
return decorator
2020-08-24 09:33:53 -04:00
def add_list(self, pattern: list = None, **args):
2020-06-16 17:18:11 -04:00
pattern_list = args.get("pattern", pattern)
def decorator(func):
for pattern in pattern_list:
this_args = args.copy()
this_args["pattern"] = pattern
self.command_handler.incoming_commands.append(Command(func, this_args))
2020-08-24 09:33:53 -04:00
return func
return decorator
def add_dict(self, pattern: dict = None, **args):
pattern_dict = args.get("pattern", pattern)
def decorator(func):
for pattern, extra in pattern_dict.items():
this_args = args.copy()
this_args["pattern"] = pattern
this_args["extra"] = args.get('extra', extra)
self.command_handler.incoming_commands.append(Command(func, this_args))
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):
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):
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
def add_callback_query(self, data_id=None, **args):
def decorator(func):
args["data_id"] = args.get("data_id", data_id)
self.command_handler.callback_queries.append(CallbackQueryCommand(func, args))
return func
return decorator
2020-09-18 20:22:38 -04:00
def get_cmds_by_func(self, func) -> list:
return [i for i in self.command_handler.outgoing_commands if i.function == func]
async def get_text(self, event, with_reply=True, return_msg=False, default=None):
if event.args:
if return_msg:
2020-05-04 19:08:13 -04:00
if event.is_reply:
return event.args, await event.get_reply_message()
return event.args, event
return event.args
elif event.is_reply and with_reply:
reply = await event.get_reply_message()
if return_msg:
return reply.text, reply
return reply.text
else:
if return_msg:
return default, event
return default
async def get_image(self, event):
if event and event.media:
if event.photo:
2020-05-09 12:22:02 -04:00
return event.photo
elif event.document:
if DocumentAttributeFilename(file_name='AnimatedSticker.tgs') in event.media.document.attributes:
2020-08-02 08:23:54 -04:00
return
if event.gif or event.video or event.audio or event.voice:
2020-08-02 08:23:54 -04:00
return
2020-05-09 12:22:02 -04:00
return event.media.document
else:
2020-08-02 08:23:54 -04:00
return
else:
2020-08-02 08:23:54 -04:00
return
2020-06-23 15:28:09 -04:00
def prefix(self):
return (self.settings.get_list('cmd_prefix') or ['.'])[0]
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
])