Archived
1
0
Fork 0

module reload

This commit is contained in:
Light Beacon 2023-01-26 15:31:13 +08:00
parent 003784696b
commit 9962b7058b
2 changed files with 58 additions and 2 deletions

View file

@ -2,6 +2,7 @@ import importlib
import os
import re
import traceback
import sys
from typing import Dict, Union
from core.elements import Command, Schedule, RegexCommand, StartUp, PrivateAssets
@ -54,6 +55,13 @@ class ModulesManager:
else:
raise ValueError(f'Duplicate bind prefix "{module.bind_prefix}"')
@staticmethod
def remove_module(module_name):
if module_name in ModulesManager.modules:
ModulesManager.modules.pop(module_name)
else:
raise ValueError(f'Moudule "{module_name}" is not exist')
@staticmethod
def bind_to_module(bind_prefix: str, meta):
if bind_prefix in ModulesManager.modules:
@ -125,3 +133,27 @@ class ModulesManager:
else:
d.update({module.bind_prefix: module})
return d
@staticmethod
def reload_module(module_name:str):
"""
重载该模块
"""
try:
module = sys.modules['modules.' + module_name]
if module_name in ModulesManager.modules:
ModulesManager.remove_module(module_name)
cnt = 0
loadedModList = list(sys.modules.keys())
for mod in loadedModList:
if mod.startswith(f'modules.{module_name}.'):
importlib.reload(sys.modules[mod])
cnt += 1
importlib.reload(module)
Logger.info(f'Succeeded reloaded modules.{module_name}!')
return cnt
except:
tb = traceback.format_exc()
errmsg = f'Failed to reload {module_name}: \n{tb}'
Logger.error(errmsg)
return -1

View file

@ -11,8 +11,8 @@ from database import BotDBUtil
module = on_command('module',
base=True,
alias={'enable': 'module enable', 'disable': 'module disable'},
developers=['OasisAkari'],
alias={'enable': 'module enable', 'disable': 'module disable','reload':'module reload'},
developers=['OasisAkari','Light-Beacon'],
required_admin=True
)
@ -21,6 +21,7 @@ module = on_command('module',
'enable all {开启所有模块}',
'disable <module>... {关闭一个/多个模块}',
'disable all {关闭所有模块。}',
'reload <module> ... {重载一个/多个模块。}',
'list {查看所有可用模块}'], exclude_from=['QQ|Guild'])
async def _(msg: MessageSession):
if msg.parsed_msg.get('list', False):
@ -32,6 +33,7 @@ async def _(msg: MessageSession):
'enable all [-g] {开启所有模块}',
'disable [-g] <module>... {关闭一个/多个模块}',
'disable all [-g] {关闭所有模块。}',
'reload <module> [-f] {重载一个/多个模块。}',
'list {查看所有可用模块}'], options_desc={'-g': '对频道进行全局操作'},
available_for=['QQ|Guild'])
async def _(msg: MessageSession):
@ -143,6 +145,19 @@ async def config_modules(msg: MessageSession):
msglist.append(f'失败:“{x}”模块已经关闭')
else:
msglist.append(f'成功:关闭模块“{x}')
elif msg.parsed_msg.get('reload', False):
if '-f' in msg.parsed_msg and msg.parsed_msg['-f']:
msglist.append(module_reload(module_))
else:
if module_ not in modules_:
msglist.append(f'失败:“{module_}”模块尚未绑定')
else:
if not msg.checkSuperUser():
msglist.append(f'失败:你没有重载模块的权限。')
elif isinstance(modules_[module_], Command) and modules_[module_].base:
msglist.append(f'失败:“{module_}”为基础模块,无法重载。')
else:
msglist.append(module_reload(module_))
if msglist is not None:
if not recommend_modules_help_doc_list:
await msg.finish('\n'.join(msglist))
@ -350,3 +365,12 @@ async def modules_help(msg: MessageSession):
send = await msg.sendMessage('\n'.join(help_msg))
await msg.sleep(60)
await send.delete()
def module_reload(module_):
reloadCnt = ModulesManager.reload_module(module_)
if reloadCnt > 0:
return f'成功:重载模块“{module_}”以及该模块下的{reloadCnt}个文件。'
elif reloadCnt == 0:
return f'成功:重载模块“{module_}”,未发现已加载的其余文件'
else:
return f'重载模块“{module_}”失败。'