Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/core/modules.py

541 lines
27 KiB
Python
Raw Normal View History

2023-03-01 09:10:45 +00:00
import re
2022-08-27 16:17:01 +00:00
import traceback
2023-07-07 08:07:13 +00:00
from config import Config, CFG
2023-02-05 14:33:33 +00:00
from core.builtins import Image, Plain, Bot
2023-03-04 08:51:56 +00:00
from core.component import module
2022-08-27 16:17:01 +00:00
from core.exceptions import InvalidHelpDocTypeError
2023-07-07 08:07:13 +00:00
from core.loader import ModulesManager, current_unloaded_modules, err_modules
2022-08-27 16:17:01 +00:00
from core.parser.command import CommandParser
from core.utils.image_table import ImageTable, image_table_render
2022-08-27 16:17:01 +00:00
from database import BotDBUtil
2023-03-04 08:51:56 +00:00
m = module('module',
base=True,
alias={'enable': 'module enable',
'disable': 'module disable',
2023-07-07 18:26:30 +00:00
'load': 'module load',
'reload': 'module reload',
'unload': 'module unload'},
2023-03-04 08:51:56 +00:00
developers=['OasisAkari', 'Light-Beacon'],
required_admin=True
)
2022-08-27 16:17:01 +00:00
2023-05-19 04:46:29 +00:00
@m.command(['enable <module>... {{core.help.module.enable}}',
'enable all {{core.help.module.enable_all}}',
'disable <module>... {{core.help.module.disable}}',
'disable all {{core.help.module.disable_all}}',
'reload <module> ... {{core.help.module.reload}}',
2023-07-07 08:07:13 +00:00
'load <module> ... {{core.help.module.load}}',
'unload <module> ... {{core.help.module.unload}}',
2023-07-18 18:15:12 +00:00
'list {{core.help.module.list}}',
'list legacy {{core.help.module.list.legacy}}'], exclude_from=['QQ|Guild'])
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-08-27 16:17:01 +00:00
if msg.parsed_msg.get('list', False):
2023-07-18 18:15:12 +00:00
if msg.parsed_msg.get('legacy', False):
legacy_help = True
await modules_help(msg, legacy_help)
2022-08-27 16:17:01 +00:00
await config_modules(msg)
2023-07-07 08:07:13 +00:00
@m.command(['enable <module> ... {{core.help.module.enable}}',
2023-05-19 04:46:29 +00:00
'enable all {{core.help.module.enable_all}}',
2023-07-07 08:07:13 +00:00
'disable <module> ... {{core.help.module.disable}}',
2023-05-19 04:46:29 +00:00
'disable all {{core.help.module.disable_all}}',
'reload <module> ... {{core.help.module.reload}}',
2023-07-07 08:07:13 +00:00
'load <module> ... {{core.help.module.load}}',
'unload <module> ... {{core.help.module.unload}}',
2023-07-18 18:24:18 +00:00
'list {{core.help.module.list}}',
2023-07-18 18:15:12 +00:00
'list legacy {{core.help.module.list.legacy}}'], options_desc={'-g': '{core.help.option.module.g}'},
2023-03-04 08:51:56 +00:00
available_for=['QQ|Guild'])
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-08-27 16:17:01 +00:00
if msg.parsed_msg.get('list', False):
2023-07-18 18:15:12 +00:00
if msg.parsed_msg.get('legacy', False):
legacy_help = True
await modules_help(msg, legacy_help)
2022-08-27 16:17:01 +00:00
await config_modules(msg)
2023-02-05 14:33:33 +00:00
async def config_modules(msg: Bot.MessageSession):
2023-04-19 08:29:46 +00:00
alias = ModulesManager.modules_aliases
2023-04-19 08:38:23 +00:00
modules_ = ModulesManager.return_modules_list(
2023-01-22 05:40:53 +00:00
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
enabled_modules_list = BotDBUtil.TargetInfo(msg).enabled_modules
2023-01-22 05:40:53 +00:00
wait_config = [msg.parsed_msg.get(
'<module>')] + msg.parsed_msg.get('...', [])
2022-08-27 16:17:01 +00:00
wait_config_list = []
for module_ in wait_config:
if module_ not in wait_config_list:
if module_ in alias:
wait_config_list.append(alias[module_])
else:
wait_config_list.append(module_)
msglist = []
recommend_modules_list = []
recommend_modules_help_doc_list = []
if msg.parsed_msg.get('enable', False):
enable_list = []
if msg.parsed_msg.get('all', False):
for function in modules_:
if function[0] == '_':
continue
2023-03-04 08:51:56 +00:00
if modules_[function].base or modules_[function].required_superuser:
2022-08-27 16:17:01 +00:00
continue
enable_list.append(function)
else:
for module_ in wait_config_list:
if module_ not in modules_:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.not_found", module=module_))
2022-08-27 16:17:01 +00:00
else:
if modules_[module_].required_superuser and not msg.checkSuperUser():
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("cparser.superuser.permission.denied"))
2023-03-04 08:51:56 +00:00
elif modules_[module_].base:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.base", module=module_))
2022-08-27 16:17:01 +00:00
else:
enable_list.append(module_)
recommend = modules_[module_].recommend_modules
if recommend is not None:
for r in recommend:
if r not in enable_list and r not in enabled_modules_list:
recommend_modules_list.append(r)
if '-g' in msg.parsed_msg and msg.parsed_msg['-g']:
get_all_channel = await msg.get_text_channel_list()
for x in get_all_channel:
query = BotDBUtil.TargetInfo(f'{msg.target.targetFrom}|{x}')
query.enable(enable_list)
for x in enable_list:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.qq_channel_global.success", module=x))
2022-08-27 16:17:01 +00:00
else:
if msg.data.enable(enable_list):
for x in enable_list:
if x in enabled_modules_list:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.already", module=x))
2022-08-27 16:17:01 +00:00
else:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.success", module=x))
2023-04-05 05:36:11 +00:00
support_lang = modules_[x].support_languages
if support_lang is not None:
if msg.locale.locale not in support_lang:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.unsupported_language",
2023-04-05 05:36:11 +00:00
module=x))
2022-08-27 16:17:01 +00:00
if recommend_modules_list:
for m in recommend_modules_list:
try:
2023-05-19 04:46:29 +00:00
recommend_modules_help_doc_list.append(msg.locale.t("core.message.module.module.help", module=m
2023-03-08 11:40:16 +00:00
))
2022-08-27 16:17:01 +00:00
if modules_[m].desc is not None:
2023-03-09 14:18:57 +00:00
d_ = modules_[m].desc
if locale_str := re.findall(r'\{(.*)}', d_):
for l in locale_str:
2023-03-14 10:14:05 +00:00
d_ = d_.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
2023-03-09 14:18:57 +00:00
recommend_modules_help_doc_list.append(d_)
2023-03-04 08:51:56 +00:00
hdoc = CommandParser(modules_[m], msg=msg, bind_prefix=modules_[m].bind_prefix,
command_prefixes=msg.prefixes).return_formatted_help_doc()
if hdoc == '':
2023-03-04 10:08:40 +00:00
hdoc = msg.locale.t('core.help.none')
2023-03-04 08:51:56 +00:00
recommend_modules_help_doc_list.append(hdoc)
2022-08-27 16:17:01 +00:00
except InvalidHelpDocTypeError:
pass
elif msg.parsed_msg.get('disable', False):
disable_list = []
if msg.parsed_msg.get('all', False):
for function in modules_:
if function[0] == '_':
continue
2023-03-04 08:51:56 +00:00
if modules_[function].base or modules_[function].required_superuser:
2022-08-27 16:17:01 +00:00
continue
disable_list.append(function)
else:
for module_ in wait_config_list:
if module_ not in modules_:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.disable.not_found", module=module_))
2022-08-27 16:17:01 +00:00
else:
if modules_[module_].required_superuser and not msg.checkSuperUser():
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("parser.superuser.permission.denied"))
2023-03-04 08:51:56 +00:00
elif modules_[module_].base:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.disable.base", module=module_))
2022-08-27 16:17:01 +00:00
else:
disable_list.append(module_)
if '-g' in msg.parsed_msg and msg.parsed_msg['-g']:
get_all_channel = await msg.get_text_channel_list()
for x in get_all_channel:
query = BotDBUtil.TargetInfo(f'{msg.target.targetFrom}|{x}')
query.disable(disable_list)
for x in disable_list:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.disable.qqchannel_global.success", module=x))
2022-08-27 16:17:01 +00:00
else:
if msg.data.disable(disable_list):
for x in disable_list:
if x not in enabled_modules_list:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.disable.already", module=x))
2022-08-27 16:17:01 +00:00
else:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.disable.success", module=x))
2023-01-26 07:31:13 +00:00
elif msg.parsed_msg.get('reload', False):
2023-01-26 07:49:44 +00:00
if msg.checkSuperUser():
2023-03-08 11:40:16 +00:00
def module_reload(module, extra_modules):
reloadCnt = ModulesManager.reload_module(module)
if reloadCnt > 1:
2023-07-07 18:26:30 +00:00
return f'{msg.locale.t("core.message.module.reload.success", module=module)}' + ('\n' if len(extra_modules) != 0 else '') + \
2023-07-02 03:12:30 +00:00
'\n'.join(extra_modules) + msg.locale.t("core.message.module.reload.with", reloadCnt=reloadCnt - 1)
2023-03-08 11:40:16 +00:00
elif reloadCnt == 1:
2023-07-07 18:26:30 +00:00
return f'{msg.locale.t("core.message.module.reload.success", module=module)}' + (
2023-07-07 08:07:13 +00:00
'\n' if len(extra_modules) != 0 else '') + '\n'.join(extra_modules) + msg.locale.t("core.message.module.reload.no_more")
2023-03-08 11:40:16 +00:00
else:
2023-05-19 04:46:29 +00:00
return f'{msg.locale.t("core.message.module.reload.failed")}'
2023-03-14 10:14:05 +00:00
2023-07-07 08:07:13 +00:00
for module_ in wait_config_list:
if '-f' in msg.parsed_msg and msg.parsed_msg['-f']:
msglist.append(module_reload(module_, []))
elif module_ not in modules_:
msglist.append(msg.locale.t("core.message.module.reload.unbound", module=module_))
2023-01-26 07:31:13 +00:00
else:
2023-07-07 08:07:13 +00:00
if modules_[module_].base:
msglist.append(msg.locale.t("core.message.module.reload.base", module=module_))
else:
extra_reload_modules = ModulesManager.search_related_module(module_, False)
if len(extra_reload_modules):
confirm = await msg.waitConfirm(msg.locale.t("core.message.module.reload.confirm",
modules='\n'.join(extra_reload_modules)))
if not confirm:
continue
2023-07-08 09:39:02 +00:00
unloaded_list = CFG.get('unloaded_modules')
if module_ in unloaded_list:
unloaded_list.remove(module_)
CFG.write('unloaded_modules', unloaded_list)
2023-07-07 08:07:13 +00:00
msglist.append(module_reload(module_, extra_reload_modules))
2023-01-26 07:49:44 +00:00
else:
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("parser.superuser.permission.denied"))
2023-07-07 08:07:13 +00:00
elif msg.parsed_msg.get('load', False):
if msg.checkSuperUser():
for module_ in wait_config_list:
if module_ not in current_unloaded_modules:
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.load.error"))
2023-07-07 08:07:13 +00:00
continue
if ModulesManager.load_module(module_):
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.load.success", module=module_))
2023-07-07 08:07:13 +00:00
unloaded_list = CFG.get('unloaded_modules')
if module_ in unloaded_list:
unloaded_list.remove(module_)
CFG.write('unloaded_modules', unloaded_list)
else:
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.load.failed"))
2023-07-07 08:07:13 +00:00
else:
msglist.append(msg.locale.t("parser.superuser.permission.denied"))
elif msg.parsed_msg.get('unload', False):
if msg.checkSuperUser():
for module_ in wait_config_list:
if module_ not in modules_:
if module_ in err_modules:
2023-07-07 18:20:22 +00:00
if await msg.waitConfirm(msg.locale.t("core.message.module.unload.unavailable.confirm")):
2023-07-07 08:07:13 +00:00
unloaded_list = CFG.get('unloaded_modules')
if not unloaded_list:
unloaded_list = []
2023-07-08 07:11:58 +00:00
if module_ not in unloaded_list:
unloaded_list.append(module_)
CFG.write('unloaded_modules', unloaded_list)
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.unload.success", module=module_))
2023-07-07 08:07:13 +00:00
else:
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.unload.error"))
2023-07-07 08:07:13 +00:00
continue
2023-07-07 18:20:22 +00:00
if await msg.waitConfirm(msg.locale.t("core.message.module.unload.confirm")):
2023-07-07 08:07:13 +00:00
if ModulesManager.unload_module(module_):
2023-07-07 18:20:22 +00:00
msglist.append(msg.locale.t("core.message.module.unload.success", module=module_))
2023-07-07 08:07:13 +00:00
unloaded_list = CFG.get('unloaded_modules')
if not unloaded_list:
unloaded_list = []
unloaded_list.append(module_)
CFG.write('unloaded_modules', unloaded_list)
2023-07-12 15:53:19 +00:00
else:
await msg.finish()
2023-07-07 08:07:13 +00:00
else:
msglist.append(msg.locale.t("parser.superuser.permission.denied"))
2022-08-27 16:17:01 +00:00
if msglist is not None:
if not recommend_modules_help_doc_list:
await msg.finish('\n'.join(msglist))
else:
await msg.sendMessage('\n'.join(msglist))
if recommend_modules_help_doc_list and ('-g' not in msg.parsed_msg or not msg.parsed_msg['-g']):
2023-05-19 04:46:29 +00:00
confirm = await msg.waitConfirm(msg.locale.t("core.message.module.recommends",
2023-04-30 03:30:59 +00:00
msgs='\n'.join(recommend_modules_list) + '\n\n' +
2023-03-08 11:40:16 +00:00
'\n'.join(recommend_modules_help_doc_list)))
2022-08-27 16:17:01 +00:00
if confirm:
if msg.data.enable(recommend_modules_list):
msglist = []
for x in recommend_modules_list:
2023-05-19 04:46:29 +00:00
msglist.append(msg.locale.t("core.message.module.enable.success", module=x))
2022-08-27 16:17:01 +00:00
await msg.finish('\n'.join(msglist))
else:
await msg.finish()
2023-03-04 08:51:56 +00:00
hlp = module('help',
base=True,
developers=['OasisAkari', 'Dianliang233'],
)
2022-08-27 16:17:01 +00:00
2023-05-19 04:46:29 +00:00
@hlp.command('<module> {{core.help.module.help.detail}}')
2023-02-05 14:33:33 +00:00
async def bot_help(msg: Bot.MessageSession):
2023-04-19 08:38:23 +00:00
module_list = ModulesManager.return_modules_list(
2023-01-22 05:40:53 +00:00
targetFrom=msg.target.targetFrom)
2023-04-19 08:29:46 +00:00
alias = ModulesManager.modules_aliases
2022-08-27 16:17:01 +00:00
if msg.parsed_msg is not None:
msgs = []
help_name = msg.parsed_msg['<module>']
if help_name in alias:
help_name = alias[help_name]
if help_name in module_list:
module_ = module_list[help_name]
if module_.desc is not None:
2023-03-01 09:10:45 +00:00
desc = module_.desc
if locale_str := re.match(r'\{(.*)}', desc):
if locale_str:
2023-03-04 10:08:40 +00:00
desc = msg.locale.t(locale_str.group(1))
2023-03-01 09:10:45 +00:00
msgs.append(desc)
2023-03-04 08:51:56 +00:00
help_ = CommandParser(module_list[help_name], msg=msg, bind_prefix=module_list[help_name].bind_prefix,
command_prefixes=msg.prefixes)
if help_.args:
msgs.append(help_.return_formatted_help_doc())
2022-08-27 16:17:01 +00:00
doc = '\n'.join(msgs)
2023-03-04 08:51:56 +00:00
if module_.regex_list.set:
2023-05-19 04:46:29 +00:00
doc += '\n' + msg.locale.t("core.message.module.help.support_regex")
2023-03-04 08:51:56 +00:00
for regex in module_.regex_list.set:
pattern = None
if isinstance(regex.pattern, str):
pattern = regex.pattern
elif isinstance(regex.pattern, re.Pattern):
pattern = regex.pattern.pattern
if pattern:
2023-03-09 14:18:57 +00:00
desc = regex.desc
if desc:
2023-03-11 11:27:26 +00:00
if locale_str := re.findall(r'\{(.*)}', desc):
2023-03-09 14:18:57 +00:00
for l in locale_str:
2023-03-14 10:14:05 +00:00
desc = desc.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.detail",
2023-03-09 14:18:57 +00:00
msg=desc)
else:
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.no_information")
2023-03-04 08:51:56 +00:00
module_alias = module_.alias
2022-08-27 16:17:01 +00:00
malias = []
2023-03-04 08:51:56 +00:00
if module_alias:
for a in module_alias:
malias.append(f'{a} -> {module_alias[a]}')
if module_.developers is not None:
2023-06-26 13:18:54 +00:00
devs = msg.locale.t('message.delimiter').join(module_.developers)
2022-08-27 16:17:01 +00:00
else:
devs = ''
2023-05-19 04:46:29 +00:00
devs_msg = '\n' + msg.locale.t("core.message.module.help.author.type1") + devs
wiki_msg = '\n' + msg.locale.t("core.message.module.help.helpdoc.address",
2023-05-12 18:20:52 +00:00
help_url=Config('help_url')) + '/' + help_name
if len(doc) > 500 and msg.Feature.image:
try:
2023-03-27 10:44:52 +00:00
tables = [ImageTable([[doc, '\n'.join(malias), devs]],
2023-05-19 04:46:29 +00:00
[msg.locale.t("core.message.module.help.table.header.help"),
msg.locale.t("core.message.module.help.table.header.alias"),
msg.locale.t("core.message.module.help.author.type2")])]
render = await image_table_render(tables)
if render:
await msg.finish([Image(render),
2023-03-23 08:20:46 +00:00
Plain(wiki_msg)])
except Exception:
traceback.print_exc()
if malias:
doc += f'\n{msg.locale.t("core.help.alias")}\n' + '\n'.join(malias)
2023-03-04 08:51:56 +00:00
await msg.finish(doc + devs_msg + wiki_msg)
2022-08-27 16:17:01 +00:00
else:
2023-05-19 04:46:29 +00:00
await msg.finish(msg.locale.t("core.message.module.help.not_found"))
2022-08-27 16:17:01 +00:00
2023-07-18 18:15:12 +00:00
@hlp.command(['{{core.help.module.help}}',
2023-07-18 18:28:10 +00:00
'legacy {{core.help.module.help.legacy}}'])
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-04-19 08:38:23 +00:00
module_list = ModulesManager.return_modules_list(
2023-01-22 05:40:53 +00:00
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
target_enabled_list = msg.enabled_modules
2023-07-18 18:15:12 +00:00
legacy_help = False
if 'legacy' in msg.parsed_msg or msg.Feature.image:
legacy_help = True
if legacy_help:
help_msg = [msg.locale.t("core.message.module.help.legacy.base")]
essential = []
for x in module_list:
if module_list[x].base:
essential.append(module_list[x].bind_prefix)
help_msg.append(' | '.join(essential))
help_msg.append(msg.locale.t("core.message.module.help.legacy.external"))
module_ = []
for x in module_list:
if x in target_enabled_list:
module_.append(x)
help_msg.append(' | '.join(module_))
help_msg.append(
msg.locale.t(
"core.message.module.help.legacy.more_information",
prefix=msg.prefixes[0],
help_url=Config('help_url')))
await msg.finish('\n'.join(help_msg))
elif msg.Feature.image:
2022-08-27 16:17:01 +00:00
try:
tables = []
essential = []
m = []
for x in module_list:
module_ = module_list[x]
appends = [module_.bind_prefix]
doc_ = []
2023-03-04 08:51:56 +00:00
help_ = CommandParser(module_, msg=msg, bind_prefix=module_.bind_prefix,
command_prefixes=msg.prefixes)
2022-08-27 16:17:01 +00:00
2023-03-04 08:51:56 +00:00
if module_.desc is not None:
2023-03-09 14:18:57 +00:00
d_ = module_.desc
if locale_str := re.findall(r'\{(.*)}', d_):
for l in locale_str:
2023-03-14 10:14:05 +00:00
d_ = d_.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
2023-03-09 14:18:57 +00:00
doc_.append(d_)
2023-03-04 08:51:56 +00:00
if help_.args:
doc_.append(help_.return_formatted_help_doc())
2022-08-27 16:17:01 +00:00
doc = '\n'.join(doc_)
2023-03-04 08:51:56 +00:00
if module_.regex_list.set:
2023-05-19 04:46:29 +00:00
doc += '\n' + msg.locale.t("core.message.module.help.support_regex")
2023-03-04 08:51:56 +00:00
for regex in module_.regex_list.set:
pattern = None
if isinstance(regex.pattern, str):
pattern = regex.pattern
elif isinstance(regex.pattern, re.Pattern):
pattern = regex.pattern.pattern
if pattern:
2023-03-08 11:40:16 +00:00
desc = regex.desc
if desc:
2023-05-12 18:24:05 +00:00
if locale_str := re.findall(r'\{(.*)}', desc):
2023-03-08 11:40:16 +00:00
for l in locale_str:
2023-05-12 18:24:05 +00:00
desc = desc.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.detail",
2023-03-08 11:40:16 +00:00
msg=desc)
else:
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.no_information")
2022-08-27 16:17:01 +00:00
appends.append(doc)
2023-03-04 08:51:56 +00:00
module_alias = module_.alias
2022-08-27 16:17:01 +00:00
malias = []
2023-03-04 08:51:56 +00:00
if module_alias:
for a in module_alias:
malias.append(f'{a} -> {module_alias[a]}')
2022-08-27 16:17:01 +00:00
appends.append('\n'.join(malias) if malias else '')
2023-03-04 08:51:56 +00:00
if module_.developers:
2023-06-26 13:18:54 +00:00
appends.append(msg.locale.t('message.delimiter').join(module_.developers))
2023-03-04 08:51:56 +00:00
if module_.base:
2022-08-27 16:17:01 +00:00
essential.append(appends)
if x in target_enabled_list:
m.append(appends)
if essential:
2023-01-22 05:40:53 +00:00
tables.append(ImageTable(
2023-05-19 04:46:29 +00:00
essential, [msg.locale.t("core.message.module.help.table.header.base"),
msg.locale.t("core.message.module.help.table.header.help"),
msg.locale.t("core.message.module.help.table.header.alias"),
msg.locale.t("core.message.module.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if m:
2023-05-19 04:46:29 +00:00
tables.append(ImageTable(m, [msg.locale.t("core.message.module.help.table.header.external"),
msg.locale.t("core.message.module.help.table.header.help"),
msg.locale.t("core.message.module.help.table.header.alias"),
msg.locale.t("core.message.module.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if tables:
render = await image_table_render(tables)
if render:
await msg.finish([Image(render),
2023-05-19 04:46:29 +00:00
Plain(msg.locale.t("core.message.module.help.more_information",
2023-05-02 14:31:41 +00:00
prefix=msg.prefixes[0], help_url=Config('help_url'), donate_url=Config('donate_url')))])
2022-08-27 16:17:01 +00:00
except Exception:
traceback.print_exc()
2023-07-18 18:15:12 +00:00
async def modules_help(msg: Bot.MessageSession, legacy_help=False):
module_list = ModulesManager.return_modules_list(
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
if legacy_help:
2023-07-18 18:15:12 +00:00
help_msg = [msg.locale.t("core.message.module.help.legacy.availables")]
2022-08-27 16:17:01 +00:00
module_ = []
for x in module_list:
2023-07-18 18:15:12 +00:00
if x[0] == '_':
continue
if module_list[x].base or module_list[x].required_superuser:
continue
module_.append(module_list[x].bind_prefix)
2022-08-27 16:17:01 +00:00
help_msg.append(' | '.join(module_))
2023-05-12 18:20:52 +00:00
help_msg.append(
msg.locale.t(
2023-05-19 04:46:29 +00:00
"core.message.module.help.legacy.more_information",
2023-05-12 18:20:52 +00:00
prefix=msg.prefixes[0],
help_url=Config('help_url')))
2023-07-18 18:15:12 +00:00
await msg.finish('\n'.join(help_msg))
elif msg.Feature.image:
2022-08-27 16:17:01 +00:00
try:
tables = []
m = []
for x in module_list:
module_ = module_list[x]
if x[0] == '_':
continue
2023-03-04 08:51:56 +00:00
if module_.base or module_.required_superuser:
2022-08-27 16:17:01 +00:00
continue
appends = [module_.bind_prefix]
doc_ = []
2023-03-04 08:51:56 +00:00
help_ = CommandParser(
2023-04-11 10:46:02 +00:00
module_, bind_prefix=module_.bind_prefix, command_prefixes=msg.prefixes, msg=msg)
2023-03-04 08:51:56 +00:00
if module_.desc is not None:
2023-05-12 18:25:09 +00:00
desc = module_.desc
if locale_str := re.findall(r'\{(.*)}', desc):
for l in locale_str:
desc = desc.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
doc_.append(desc)
2023-03-04 08:51:56 +00:00
if help_.args:
doc_.append(help_.return_formatted_help_doc())
2022-08-27 16:17:01 +00:00
doc = '\n'.join(doc_)
2023-03-04 08:51:56 +00:00
if module_.regex_list.set:
2023-05-19 04:46:29 +00:00
doc += '\n' + msg.locale.t("core.message.module.help.support_regex")
2023-03-04 08:51:56 +00:00
for regex in module_.regex_list.set:
pattern = None
if isinstance(regex.pattern, str):
pattern = regex.pattern
elif isinstance(regex.pattern, re.Pattern):
pattern = regex.pattern.pattern
if pattern:
2023-03-08 11:40:16 +00:00
desc = regex.desc
if desc:
2023-05-12 18:20:52 +00:00
if locale_str := re.findall(r'\{(.*)}', desc):
2023-03-08 11:40:16 +00:00
for l in locale_str:
2023-05-12 18:20:52 +00:00
desc = desc.replace(f'{{{l}}}', msg.locale.t(l, fallback_failed_prompt=False))
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.detail",
2023-03-08 11:40:16 +00:00
msg=desc)
else:
2023-05-19 04:46:29 +00:00
doc += f'\n{pattern} ' + msg.locale.t("core.message.module.help.regex.no_information")
2022-08-27 16:17:01 +00:00
appends.append(doc)
2023-03-04 08:51:56 +00:00
module_alias = module_.alias
2022-08-27 16:17:01 +00:00
malias = []
2023-03-04 08:51:56 +00:00
if module_alias:
for a in module_alias:
malias.append(f'{a} -> {module_alias[a]}')
2022-08-27 16:17:01 +00:00
appends.append('\n'.join(malias) if malias else '')
2023-03-04 08:51:56 +00:00
if module_.developers:
2023-06-26 13:18:54 +00:00
appends.append(msg.locale.t('message.delimiter').join(module_.developers))
2022-08-27 16:17:01 +00:00
m.append(appends)
if m:
2023-05-19 04:46:29 +00:00
tables.append(ImageTable(m, [msg.locale.t("core.message.module.help.table.header.external"),
msg.locale.t("core.message.module.help.table.header.help"),
msg.locale.t("core.message.module.help.table.header.alias"),
msg.locale.t("core.message.module.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if tables:
render = await image_table_render(tables)
if render:
await msg.finish([Image(render)])
except Exception:
2023-07-18 18:24:18 +00:00
traceback.print_exc()