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/__init__.py

127 lines
5.1 KiB
Python
Raw Normal View History

2021-07-26 12:43:51 +00:00
import asyncio
import os
from core.loader import ModulesManager
2021-07-24 08:59:15 +00:00
from core.elements import MessageSession
from database import BotDBUtil
2021-07-06 18:00:26 +00:00
from core.decorator import command
2021-07-26 12:43:51 +00:00
from core.parser.command import CommandParser
2021-02-01 15:13:11 +00:00
2021-07-19 16:12:29 +00:00
@command('module',
is_base_function=True,
2021-07-26 12:43:51 +00:00
is_admin_function=True,
help_doc=('module enable (<module>|all) {开启一个或所有模块}',
'module disable (<module>|all) {关闭一个或所有模块}'),
alias={'enable': 'module enable', 'disable': 'module disable'}
)
async def config_modules(msg: MessageSession):
command_third_word = msg.parsed_msg['<module>']
if command_third_word in ModulesManager.return_modules_alias_map():
command_third_word = ModulesManager.return_modules_alias_map()[command_third_word]
2021-07-26 12:43:51 +00:00
query = BotDBUtil.Module(msg)
msglist = []
2021-07-26 12:43:51 +00:00
if msg.parsed_msg['enable']:
if msg.parsed_msg['all']:
for function in ModulesManager.return_modules_list_as_dict():
if query.enable(function):
msglist.append(f'成功:打开模块“{function}')
elif query.enable(command_third_word):
msglist.append(f'成功:打开模块“{command_third_word}')
2021-07-26 12:43:51 +00:00
elif msg.parsed_msg['disable']:
if msg.parsed_msg['all']:
for function in ModulesManager.return_modules_list_as_dict():
if query.disable(function):
msglist.append(f'成功:打开模块“{function}')
elif query.disable(command_third_word):
msglist.append(f'成功:关闭模块“{command_third_word}')
if msglist is not None:
2021-07-26 12:43:51 +00:00
await msg.sendMessage('\n'.join(msglist))
2021-07-26 12:43:51 +00:00
@command('help',
is_base_function=True,
help_doc=('help {查看所有可用模块}',
'help <module> {查看一个模块的详细信息}')
)
async def bot_help(msg: MessageSession):
module_list = ModulesManager.return_modules_list_as_dict()
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:
2021-07-26 12:43:51 +00:00
help_name = alias[help_name]
if help_name in module_list:
help_ = CommandParser(module_list[help_name].help_doc).return_formatted_help_doc()
if help_ is not None:
msgs.append(help_)
if msgs:
await msg.sendMessage('\n'.join(msgs))
else:
2021-07-26 12:43:51 +00:00
help_msg = ['基础命令:']
2021-02-07 09:56:01 +00:00
essential = []
2021-07-26 12:43:51 +00:00
for x in module_list:
if module_list[x].is_base_function:
essential.append(module_list[x].bind_prefix)
2021-02-07 09:56:01 +00:00
help_msg.append(' | '.join(essential))
2021-02-01 17:41:45 +00:00
help_msg.append('模块扩展命令:')
2021-02-07 09:56:01 +00:00
module = []
2021-07-26 12:43:51 +00:00
for x in module_list:
if BotDBUtil.Module(msg).check_target_enabled_module(module_list[x].bind_prefix):
module.append(x)
2021-02-07 09:56:01 +00:00
help_msg.append(' | '.join(module))
print(help_msg)
help_msg.append('使用~help <对应模块名>查看详细信息。\n使用~modules查看所有的可用模块。\n你也可以通过查阅文档获取帮助:\nhttps://bot.teahou.se/modules/')
2021-07-26 12:43:51 +00:00
help_msg.append('[本消息将在一分钟后撤回]')
send = await msg.sendMessage('\n'.join(help_msg))
await asyncio.sleep(60)
await send.delete()
2021-02-01 15:13:11 +00:00
2021-07-26 12:43:51 +00:00
@command('modules',
is_base_function=True,
help_doc='modules {查看所有可用模块}'
)
async def modules_help(msg: MessageSession):
module_list = ModulesManager.return_modules_list_as_dict()
help_msg = ['当前可用的模块有:']
2021-02-07 09:56:01 +00:00
module = []
2021-07-26 12:43:51 +00:00
for x in module_list:
module.append(module_list[x].bind_prefix)
2021-02-10 14:11:26 +00:00
help_msg.append(' | '.join(module))
2021-02-10 15:49:43 +00:00
help_msg.append('使用~help <模块名>查看详细信息。\n你也可以通过查阅文档获取帮助:\nhttps://bot.teahou.se/modules/')
2021-07-26 12:43:51 +00:00
help_msg.append('[本消息将在一分钟后撤回]')
send = await msg.sendMessage('\n'.join(help_msg))
await asyncio.sleep(60)
await send.delete()
2021-02-01 15:13:11 +00:00
2021-07-26 12:43:51 +00:00
@command('version',
is_base_function=True,
help_doc='version {查看机器人的版本号}'
)
async def bot_version(msg: MessageSession):
2021-03-05 16:19:06 +00:00
version = os.path.abspath('.version')
openfile = open(version, 'r')
2021-07-26 12:43:51 +00:00
msgs = '当前运行的代码版本号为:' + openfile.read()
await msg.sendMessage(msgs, msgs)
2021-03-05 16:19:06 +00:00
openfile.close()
2021-03-05 16:08:10 +00:00
2021-07-26 12:43:51 +00:00
@command('admin',
is_base_function=True,
is_admin_function=True,
help_doc=('admin add <user>', 'admin del <user>')
)
async def config_gu(msg: MessageSession):
if msg.parsed_msg['add']:
user = msg.parsed_msg['<user>']
if user:
if BotDBUtil.SenderInfo(f"{msg.target.senderFrom}|{user}").add_TargetAdmin(msg.target.targetId):
await msg.sendMessage("成功")
if msg.parsed_msg['del']:
user = msg.parsed_msg['<user>']
if user:
if BotDBUtil.SenderInfo(f"{msg.target.senderFrom}|{user}").remove_TargetAdmin(msg.target.targetId):
await msg.sendMessage("成功")