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

436 lines
21 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-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
from core.loader import ModulesManager
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',
'reload': 'module reload'},
developers=['OasisAkari', 'Light-Beacon'],
required_admin=True
)
2022-08-27 16:17:01 +00:00
2023-03-08 11:40:16 +00:00
@m.command(['enable <module>... {{core.module.help.enable}}',
'enable all {{core.module.help.enable_all}}',
'disable <module>... {{core.module.help.disable}}',
'disable all {{core.module.help.disable_all}}',
'reload <module> ... {{core.module.help.reload}}',
'list {{core.module.help.list}}'], 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):
await modules_help(msg)
await config_modules(msg)
2023-03-08 11:40:16 +00:00
@m.command(['enable <module>... {{core.module.help.enable}}',
'enable all {{core.module.help.enable_all}}',
'disable <module>... {{core.module.help.disable}}',
'disable all {{core.module.help.disable_all}}',
'reload <module> ... {{core.module.help.reload}}',
'list {{core.module.help.list}}'], options_desc={'-g': '{core.module.option.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):
await modules_help(msg)
await config_modules(msg)
2023-02-05 14:33:33 +00:00
async def config_modules(msg: Bot.MessageSession):
2022-08-27 16:17:01 +00:00
alias = ModulesManager.return_modules_alias_map()
2023-01-22 05:40:53 +00:00
modules_ = ModulesManager.return_modules_list_as_dict(
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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.enable.not_found", module=module_))
2022-08-27 16:17:01 +00:00
else:
if modules_[module_].required_superuser and not msg.checkSuperUser():
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.enable.perimission.denied",
module=module_))
2023-03-04 08:51:56 +00:00
elif modules_[module_].base:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.enable.already", module=x))
2022-08-27 16:17:01 +00:00
else:
2023-03-09 05:13:29 +00:00
msglist.append(msg.locale.t("core.module.message.enable.success", module=x))
2022-08-27 16:17:01 +00:00
if recommend_modules_list:
for m in recommend_modules_list:
try:
2023-03-08 11:40:16 +00:00
recommend_modules_help_doc_list.append(msg.locale.t("core.module.message.module.help", module=module
))
2022-08-27 16:17:01 +00:00
if modules_[m].desc is not None:
2023-01-22 05:40:53 +00:00
recommend_modules_help_doc_list.append(
modules_[m].desc)
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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.disable.not_found", module=module_))
2022-08-27 16:17:01 +00:00
else:
if modules_[module_].required_superuser and not msg.checkSuperUser():
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.disable.permission.denied",
module=module_))
2023-03-04 08:51:56 +00:00
elif modules_[module_].base:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.disable.already", module=x))
2022-08-27 16:17:01 +00:00
else:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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:
return f'{msg.locale.t("core.module.message.reload.success")}{module} ' + ' '.join(
extra_modules) + msg.locale.t("core.module.message.reload.with", reloadCnt=reloadCnt - 1)
elif reloadCnt == 1:
return f'{msg.locale.t("core.module.message.reload.success")}{module} ' + ' '.join(extra_modules) + \
msg.locale.t("core.module.message.reload.no_more")
else:
return f'重载模块失败。'
2023-01-26 07:49:44 +00:00
if '-f' in msg.parsed_msg and msg.parsed_msg['-f']:
msglist.append(module_reload(module_))
elif module_ not in modules_:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.reload.unbound", module=module_))
2023-01-26 07:31:13 +00:00
else:
2023-03-04 08:51:56 +00:00
if modules_[module_].base:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.reload.base", module=module_))
2023-01-26 07:31:13 +00:00
else:
2023-01-28 05:53:11 +00:00
extra_reload_modules = ModulesManager.search_related_module(module_, False)
2023-01-26 13:21:46 +00:00
if len(extra_reload_modules):
2023-03-08 11:40:16 +00:00
confirm = await msg.waitConfirm(msg.locale.t("core.module.message.reload.confirm",
modules='\n'.join(extra_reload_modules)))
2023-01-26 13:21:46 +00:00
if not confirm:
2023-01-27 09:26:05 +00:00
await msg.finish()
2023-01-26 13:21:46 +00:00
return
2023-01-28 05:53:11 +00:00
msglist.append(module_reload(module_, extra_reload_modules))
2023-01-26 07:49:44 +00:00
else:
2023-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.reload.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-03-08 11:40:16 +00:00
confirm = await msg.waitConfirm(msg.locale.t("core.module.message.enable.recommend",
msgs=
'\n'.join(recommend_modules_list) + '\n\n' +
'\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-03-08 11:40:16 +00:00
msglist.append(msg.locale.t("core.module.message.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-03-08 11:40:16 +00:00
@hlp.command('<module> {{core.module.help.help.detail}}')
2023-02-05 14:33:33 +00:00
async def bot_help(msg: Bot.MessageSession):
2023-01-22 05:40:53 +00:00
module_list = ModulesManager.return_modules_list_as_dict(
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
alias = ModulesManager.return_modules_alias_map()
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-03-08 11:40:16 +00:00
doc += '\n' + msg.locale.t("core.module.message.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
doc += f'\n{pattern} {regex.desc if regex.desc else msg.locale.t("core.module.message.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]}')
2022-08-27 16:17:01 +00:00
if malias:
2023-03-04 10:08:40 +00:00
doc += f'\n{msg.locale.t("core.help.alias")}\n' + '\n'.join(malias)
2023-03-04 08:51:56 +00:00
if module_.developers is not None:
2023-03-08 11:40:16 +00:00
devs = ''.join(module_.developers)
2022-08-27 16:17:01 +00:00
else:
devs = ''
2023-03-08 11:40:16 +00:00
devs_msg = '\n' + msg.locale.t("core.module.message.help.author.type1") + devs
wiki_msg = f'\n' + msg.locale.t("core.module.message.help.helpdoc.address") + help_name
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-03-08 11:40:16 +00:00
await msg.finish(msg.locale.t("core.module.message.help.not_found"))
2022-08-27 16:17:01 +00:00
2023-03-08 11:40:16 +00:00
@hlp.command('{{core.module.help.help}}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-01-22 05:40:53 +00:00
module_list = ModulesManager.return_modules_list_as_dict(
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
target_enabled_list = msg.enabled_modules
legacy_help = True
if 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:
doc_.append(module_.desc)
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-03-08 11:40:16 +00:00
doc += '\n' + msg.locale.t("core.module.message.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:
if locale_str := re.findall(r'\{(.*)}', x):
for l in locale_str:
x = x.replace(f'{{{l}}}', msg.locale.t(l))
doc += f'\n{pattern} ' + msg.locale.t("core.module.message.help.regex.detail",
msg=desc)
else:
doc += f'\n{pattern} ' + msg.locale.t("core.module.message.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:
appends.append(''.join(module_.developers))
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-03-08 11:40:16 +00:00
essential, [msg.locale.t("core.module.message.help.table.header.base"),
msg.locale.t("core.module.message.help.table.header.help"),
msg.locale.t("core.module.message.help.table.header.alias"),
msg.locale.t("core.module.message.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if m:
2023-03-08 11:40:16 +00:00
tables.append(ImageTable(m, [msg.locale.t("core.module.message.help.table.header.external"),
msg.locale.t("core.module.message.help.table.header.help"),
msg.locale.t("core.module.message.help.table.header.alias"),
msg.locale.t("core.module.message.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if tables:
render = await image_table_render(tables)
if render:
legacy_help = False
await msg.finish([Image(render),
2023-03-08 11:40:16 +00:00
Plain(msg.locale.t("core.module.message.help.more_information", prefix=msg.prefixes[0]))])
2022-08-27 16:17:01 +00:00
except Exception:
traceback.print_exc()
if legacy_help:
2023-03-08 11:40:16 +00:00
help_msg = [msg.locale.t("core.module.message.help.legacy.base")]
2022-08-27 16:17:01 +00:00
essential = []
for x in module_list:
2023-03-04 08:51:56 +00:00
if module_list[x].base:
2022-08-27 16:17:01 +00:00
essential.append(module_list[x].bind_prefix)
help_msg.append(' | '.join(essential))
2023-03-08 11:40:16 +00:00
help_msg.append(msg.locale.t("core.module.message.help.legacy.external"))
2022-08-27 16:17:01 +00:00
module_ = []
for x in module_list:
if x in target_enabled_list:
module_.append(x)
help_msg.append(' | '.join(module_))
2023-03-08 11:40:16 +00:00
help_msg.append(msg.locale.t("core.module.message.help.legacy.more_information", prefix=msg.prefixes[0]))
2022-08-27 16:17:01 +00:00
if msg.Feature.delete:
2023-03-08 11:40:16 +00:00
help_msg.append(msg.locale.t("core.module.message.help.legacy.recall"))
2022-08-27 16:17:01 +00:00
send = await msg.sendMessage('\n'.join(help_msg))
await msg.sleep(60)
await send.delete()
2023-02-05 14:33:33 +00:00
async def modules_help(msg: Bot.MessageSession):
2023-01-22 05:40:53 +00:00
module_list = ModulesManager.return_modules_list_as_dict(
targetFrom=msg.target.targetFrom)
2022-08-27 16:17:01 +00:00
legacy_help = True
if 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(
module_, bind_prefix=module_.bind_prefix, command_prefixes=msg.prefixes)
if module_.desc is not None:
doc_.append(module_.desc)
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-03-08 11:40:16 +00:00
doc += '\n' + msg.locale.t("core.module.message.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:
if locale_str := re.findall(r'\{(.*)}', x):
for l in locale_str:
x = x.replace(f'{{{l}}}', msg.locale.t(l))
doc += f'\n{pattern} ' + msg.locale.t("core.module.message.help.regex.detail",
msg=desc)
else:
doc += f'\n{pattern} ' + msg.locale.t("core.module.message.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:
appends.append(''.join(module_.developers))
2022-08-27 16:17:01 +00:00
m.append(appends)
if m:
2023-03-08 11:40:16 +00:00
tables.append(ImageTable(m, [msg.locale.t("core.module.message.help.table.header.external"),
msg.locale.t("core.module.message.help.table.header.help"),
msg.locale.t("core.module.message.help.table.header.alias"),
msg.locale.t("core.module.message.help.author.type2")]))
2022-08-27 16:17:01 +00:00
if tables:
render = await image_table_render(tables)
if render:
legacy_help = False
await msg.finish([Image(render)])
except Exception:
traceback.print_exc()
if legacy_help:
2023-03-08 11:40:16 +00:00
help_msg = [msg.locale.t("core.module.message.help.legacy.availables")]
2022-08-27 16:17:01 +00:00
module_ = []
for x in module_list:
if x[0] == '_':
continue
2023-03-04 08:51:56 +00:00
if module_list[x].base or module_list[x].required_superuser:
2022-08-27 16:17:01 +00:00
continue
module_.append(module_list[x].bind_prefix)
help_msg.append(' | '.join(module_))
2023-03-08 11:40:16 +00:00
help_msg.append(msg.locale.t("core.module.message.help.legacy.more_information", prefix=msg.prefixes[0]))
2022-08-27 16:17:01 +00:00
if msg.Feature.delete:
2023-03-08 11:40:16 +00:00
help_msg.append(msg.locale.t("core.module.message.help.legacy.recall"))
2022-08-27 16:17:01 +00:00
send = await msg.sendMessage('\n'.join(help_msg))
await msg.sleep(60)
await send.delete()