diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index d4ff649..fe75229 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -1,5 +1,4 @@ import contextlib -import sys from typing import Callable, Awaitable, Set, Dict @@ -13,7 +12,7 @@ from pagermaid.scheduler import scheduler import pyromod.listen from pyrogram import Client -pgm_version = "1.2.13" +pgm_version = "1.2.14" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() @@ -21,7 +20,9 @@ working_dir = getcwd() read_context = {} help_messages = {} hook_functions: Dict[str, Set[Callable[[], Awaitable[None]]]] = { - "startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set(), "process_error": set(), } + "startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set(), "process_error": set(), + "load_plugins_finished": set(), +} all_permissions = [] logs = getLogger(__name__) @@ -40,7 +41,6 @@ root_logger.addHandler(file_handler) basicConfig(level=DEBUG if Config.DEBUG else INFO) logs.setLevel(DEBUG if Config.DEBUG else INFO) -# easy check if not Config.API_ID or not Config.API_HASH: logs.warning("Api-ID or Api-HASH Not Found!") Config.API_ID = Config.DEFAULT_API_ID diff --git a/pagermaid/__main__.py b/pagermaid/__main__.py index d9cd89d..6d9576c 100644 --- a/pagermaid/__main__.py +++ b/pagermaid/__main__.py @@ -38,6 +38,7 @@ async def main(): await process_exit(start=True, _client=bot) logs.info(lang('start')) + await Hook.load_success_exec() await Hook.startup() await idle() diff --git a/pagermaid/config.py b/pagermaid/config.py index 3439c80..146392b 100644 --- a/pagermaid/config.py +++ b/pagermaid/config.py @@ -51,7 +51,7 @@ class Config: LOG_ID = int(os.environ.get("PGM_LOG_ID", config["log_chatid"])) IPV6 = strtobool(os.environ.get("PGM_IPV6", config["ipv6"])) ALLOW_ANALYTIC = strtobool(os.environ.get("PGM_ALLOW_ANALYTIC", config["allow_analytic"]), True) - SENTRY_API = "https://262b310907314998ba2931fa8b3b3624@o1342815.ingest.sentry.io/6617119" + SENTRY_API = "https://fbff8c554f6a44b989b490864679546e@o1342815.ingest.sentry.io/6617119" MIXPANEL_API = "c79162511383b0fa1e9c062a2a86c855" TIME_FORM = os.environ.get("PGM_TIME_FORM", config["time_form"]) DATE_FORM = os.environ.get("PGM_DATE_FORM", config["date_form"]) @@ -85,7 +85,6 @@ class Config: with open(f"data{os.sep}alias.json", encoding="utf-8") as f: alias_dict = load_json(f) except Exception as e: - print(f"[Degrade] Reading alias file failed:{e}") alias_dict = {} except ValueError as e: print(e) diff --git a/pagermaid/hook.py b/pagermaid/hook.py index da4a821..85aa595 100644 --- a/pagermaid/hook.py +++ b/pagermaid/hook.py @@ -65,6 +65,18 @@ class Hook: return decorator + @staticmethod + def load_success(): + """ + 注册一个插件加载完成钩子 + """ + + def decorator(function): + hook_functions["load_plugins_finished"].add(function) + return function + + return decorator + @staticmethod async def startup(): if cors := [startup(**inject(None, startup)) for startup in hook_functions["startup"]]: # noqa @@ -143,3 +155,11 @@ class Hook: raise StopPropagation from e except Exception as exception: logs.info(f"[process_error]: {type(exception)}: {exception}") + + @staticmethod + async def load_success_exec(): + if cors := [load(**inject(None, load)) for load in hook_functions["load_plugins_finished"]]: # noqa + try: + await asyncio.gather(*cors) + except Exception as exception: + logs.info(f"[load_success_exec]: {type(exception)}: {exception}") diff --git a/pagermaid/modules/help.py b/pagermaid/modules/help.py index 8b901dc..bc7ff94 100644 --- a/pagermaid/modules/help.py +++ b/pagermaid/modules/help.py @@ -108,7 +108,7 @@ async def lang_change(message: Message): with open('config.yml', 'w', encoding="utf-8") as f: f.write(file) await message.edit(f"{lang('lang_change_to')} {to_lang}, {lang('lang_reboot')}") - reload_all() + await reload_all() else: await message.edit(f'{lang("lang_current_lang")} {Config.LANGUAGE}\n\n' f'{lang("lang_all_lang")}{",".join(dir__)}') @@ -146,7 +146,7 @@ async def alias_commands(message: Message): with open(f"data{sep}alias.json", 'w', encoding="utf-8") as f: json_dump(Config.alias_dict, f) await message.edit(lang('alias_success')) - reload_all() + await reload_all() except KeyError: await message.edit(lang('alias_no_exist')) return @@ -160,4 +160,4 @@ async def alias_commands(message: Message): with open(f"data{sep}alias.json", 'w', encoding="utf-8") as f: json_dump(Config.alias_dict, f) await message.edit(lang('alias_success')) - reload_all() + await reload_all() diff --git a/pagermaid/modules/plugin.py b/pagermaid/modules/plugin.py index ac7bb39..f7188c2 100644 --- a/pagermaid/modules/plugin.py +++ b/pagermaid/modules/plugin.py @@ -80,7 +80,7 @@ async def plugin(message: Message): f"{lang('apt_plugin')} " f"{path.basename(file_path)[:-3]} {lang('apt_installed')}") await log(f"{lang('apt_install_success')} {path.basename(file_path)[:-3]}.") - reload_all() + await reload_all() elif len(message.parameter) >= 2: process_list = message.parameter message = await message.edit(lang('apt_processing')) @@ -131,7 +131,7 @@ async def plugin(message: Message): restart = len(success_list) > 0 await message.edit(text) if restart: - reload_all() + await reload_all() else: await message.edit(lang('arg_error')) elif message.parameter[0] == "remove": @@ -147,7 +147,7 @@ async def plugin(message: Message): json.dump(version_json, f) await message.edit(f"{lang('apt_remove_success')} {message.parameter[1]}") await log(f"{lang('apt_remove')} {message.parameter[1]}.") - reload_all() + await reload_all() elif "/" in message.parameter[1]: await message.edit(lang('arg_error')) else: @@ -198,7 +198,7 @@ async def plugin(message: Message): await message.edit(f"{lang('apt_plugin')} {message.parameter[1]} " f"{lang('apt_enable')}") await log(f"{lang('apt_enable')} {message.parameter[1]}.") - reload_all() + await reload_all() else: await message.edit(lang('apt_not_exist')) else: @@ -211,7 +211,7 @@ async def plugin(message: Message): await message.edit(f"{lang('apt_plugin')} {message.parameter[1]} " f"{lang('apt_disable')}") await log(f"{lang('apt_disable')} {message.parameter[1]}.") - reload_all() + await reload_all() else: await message.edit(lang('apt_not_exist')) else: @@ -288,7 +288,7 @@ async def plugin(message: Message): with open(f"{plugin_directory}version.json", "w") as f: json.dump(version_json, f) await message.edit(f"{lang('apt_name')}\n\n" + lang("apt_reading_list") + need_update) - reload_all() + await reload_all() elif message.parameter[0] == "search": if len(message.parameter) == 1: await message.edit(lang("apt_search_no_name")) diff --git a/pagermaid/modules/reload.py b/pagermaid/modules/reload.py index 6ed47ff..b396174 100644 --- a/pagermaid/modules/reload.py +++ b/pagermaid/modules/reload.py @@ -4,11 +4,12 @@ import pagermaid.config import pagermaid.modules from pagermaid import bot, logs, help_messages, all_permissions, hook_functions +from pagermaid.hook import Hook from pagermaid.listener import listener from pagermaid.utils import lang, Message -def reload_all(): +async def reload_all(): bot.dispatcher.remove_all_handlers() bot.job.remove_all_jobs() with contextlib.suppress(RuntimeError): @@ -38,6 +39,7 @@ def reload_all(): except BaseException as exception: logs.info(f"{lang('module')} {plugin_name} {lang('error')}: {exception}") pagermaid.modules.plugin_list.remove(plugin_name) + await Hook.load_success_exec() @listener(is_plugin=False, command="reload", @@ -45,5 +47,5 @@ def reload_all(): description=lang('reload_des')) async def reload_plugins(message: Message): """ To reload plugins. """ - reload_all() + await reload_all() await message.edit(lang("reload_ok")) diff --git a/requirements.txt b/requirements.txt index 5f1c1fc..1e99757 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -pyrogram==2.0.59 -TgCrypto>=1.2.4 +pyrogram==2.0.60 +TgCrypto==1.2.5 Pillow>=8.4.0 pytz>=2021.3 PyYAML>=6.0 @@ -8,5 +8,5 @@ psutil>=5.8.0 httpx apscheduler sqlitedict -casbin>=1.17.2 +casbin==1.17.4 sentry-sdk>=1.10.1