PagerMaid-Pyro/pagermaid/modules/reload.py

61 lines
2.2 KiB
Python
Raw Normal View History

2022-06-30 06:49:03 +00:00
import contextlib
2022-06-20 13:55:14 +00:00
import importlib
2022-12-22 04:00:10 +00:00
import os
2022-06-20 13:55:14 +00:00
import pagermaid.config
import pagermaid.modules
2023-01-14 05:49:08 +00:00
from pagermaid import logs, help_messages, all_permissions, hook_functions, read_context
from pagermaid.enums import Message
2022-11-14 14:11:27 +00:00
from pagermaid.hook import Hook
2022-06-20 13:55:14 +00:00
from pagermaid.listener import listener
2023-01-14 05:49:08 +00:00
from pagermaid.services import bot, scheduler
from pagermaid.utils import lang
2022-06-20 13:55:14 +00:00
2022-11-14 14:11:27 +00:00
async def reload_all():
2023-01-14 05:49:08 +00:00
read_context.clear()
2022-06-20 13:55:14 +00:00
bot.dispatcher.remove_all_handlers()
bot.job.remove_all_jobs()
2022-06-30 06:49:03 +00:00
with contextlib.suppress(RuntimeError):
bot.cancel_all_listener()
2022-06-20 13:55:14 +00:00
loaded_plugins = list(pagermaid.modules.plugin_list)
loaded_plugins.extend(iter(pagermaid.modules.module_list))
# init
importlib.reload(pagermaid.modules)
importlib.reload(pagermaid.config)
help_messages.clear()
all_permissions.clear()
for functions in hook_functions.values():
functions.clear() # noqa: clear all hooks
2022-06-20 13:55:14 +00:00
for module_name in pagermaid.modules.module_list:
try:
module = importlib.import_module(f"pagermaid.modules.{module_name}")
if module_name in loaded_plugins:
importlib.reload(module)
except BaseException as exception:
logs.info(f"{lang('module')} {module_name} {lang('error')}: {type(exception)}: {exception}")
2022-11-26 03:01:51 +00:00
for plugin_name in pagermaid.modules.plugin_list.copy():
2022-06-20 13:55:14 +00:00
try:
plugin = importlib.import_module(f"plugins.{plugin_name}")
2022-12-22 04:00:10 +00:00
if plugin_name in loaded_plugins and os.path.exists(plugin.__file__):
2022-06-20 13:55:14 +00:00
importlib.reload(plugin)
except BaseException as exception:
logs.info(f"{lang('module')} {plugin_name} {lang('error')}: {exception}")
pagermaid.modules.plugin_list.remove(plugin_name)
2022-11-14 14:11:27 +00:00
await Hook.load_success_exec()
2022-06-20 13:55:14 +00:00
@listener(is_plugin=False, command="reload",
need_admin=True,
description=lang('reload_des'))
async def reload_plugins(message: Message):
""" To reload plugins. """
2022-11-14 14:11:27 +00:00
await reload_all()
2022-06-20 13:55:14 +00:00
await message.edit(lang("reload_ok"))
2023-01-14 05:49:08 +00:00
@scheduler.scheduled_job("cron", hour="4", id="reload.clear_read_context")
async def clear_read_context_cron():
read_context.clear()