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
|
|
|
|
from re import escape
|
|
|
|
|
2020-05-09 13:15:08 -04:00
|
|
|
from aiohttp import ClientSession
|
2023-10-16 09:46:31 -04:00
|
|
|
from telethon import events
|
2020-05-04 12:39:09 -04:00
|
|
|
from telethon.tl.types import DocumentAttributeFilename
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
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-05-06 11:10:00 -04:00
|
|
|
self.help_dict = {}
|
2020-05-09 13:15:08 -04:00
|
|
|
self.aioclient = ClientSession()
|
2020-05-16 22:50:25 -04:00
|
|
|
self.thread_pool = ThreadPoolExecutor()
|
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.incoming_commands = {}
|
2020-05-16 17:52:29 -04:00
|
|
|
self.command_handler.inline_photo_commands = {}
|
2020-05-25 14:04:56 -04:00
|
|
|
self.command_handler.inline_article_commands = {}
|
2020-05-06 11:10:00 -04:00
|
|
|
self.help_dict = {}
|
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-05-02 17:04:27 -04:00
|
|
|
def add(self, pattern=None, **args):
|
|
|
|
pattern = args.get("pattern", pattern)
|
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
def decorator(func):
|
2020-05-06 11:10:00 -04:00
|
|
|
if func.__module__.split(".")[-1] in self.help_dict:
|
|
|
|
self.help_dict[func.__module__.split(".")[-1]] += [pattern]
|
|
|
|
else:
|
|
|
|
self.help_dict[func.__module__.split(".")[-1]] = [pattern]
|
|
|
|
|
2020-05-02 17:04:27 -04:00
|
|
|
self.command_handler.incoming_commands[pattern] = {
|
2020-04-16 16:26:05 -04:00
|
|
|
"function": func,
|
|
|
|
"noprefix": args.get('noprefix', False),
|
2020-05-05 19:51:55 -04:00
|
|
|
"sudo": args.get('sudo', False),
|
2020-06-09 21:05:53 -04:00
|
|
|
"extras": args.get('extras', None),
|
|
|
|
"nsfw": args.get('nsfw', False),
|
2020-06-12 14:20:18 -04:00
|
|
|
"admin": args.get('admin', False),
|
|
|
|
"owner": args.get('owner', False)
|
2020-04-16 16:26:05 -04:00
|
|
|
}
|
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-05-21 13:53:11 -04:00
|
|
|
def add_inline_photo(self, pattern=None, **args):
|
|
|
|
pattern = args.get("pattern", pattern)
|
|
|
|
|
2020-05-16 17:52:29 -04:00
|
|
|
def decorator(func):
|
2020-05-21 13:53:11 -04:00
|
|
|
self.command_handler.inline_photo_commands[pattern] = {
|
|
|
|
"function": func,
|
|
|
|
"default": args.get("default", None)
|
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):
|
|
|
|
pattern = args.get("pattern", pattern)
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
self.command_handler.inline_article_commands[pattern] = {
|
|
|
|
"function": func,
|
|
|
|
"default": args.get("default", None)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2020-05-04 12:39:09 -04:00
|
|
|
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()
|
|
|
|
|
2020-05-04 12:39:09 -04:00
|
|
|
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
|
2020-05-04 12:39:09 -04:00
|
|
|
elif event.document:
|
|
|
|
if DocumentAttributeFilename(file_name='AnimatedSticker.tgs') in event.media.document.attributes:
|
2020-05-09 12:22:02 -04:00
|
|
|
return None
|
2020-05-04 12:39:09 -04:00
|
|
|
if event.gif or event.video or event.audio or event.voice:
|
2020-05-09 12:22:02 -04:00
|
|
|
return None
|
2020-05-04 12:39:09 -04:00
|
|
|
|
2020-05-09 12:22:02 -04:00
|
|
|
return event.media.document
|
|
|
|
else:
|
|
|
|
return None
|
2020-05-04 12:39:09 -04:00
|
|
|
else:
|
2020-05-09 12:22:02 -04:00
|
|
|
return None
|
2020-05-04 12:39:09 -04:00
|
|
|
|
2023-10-16 09:46:31 -04:00
|
|
|
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")
|
|
|
|
]
|
|
|
|
|
2020-06-12 14:21:31 -04:00
|
|
|
system_index = self.all_modules.index("sudo")
|
2023-10-16 09:46:31 -04:00
|
|
|
self.all_modules.insert(0, self.all_modules.pop(system_index))
|