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/core/parser/message.py

75 lines
4.2 KiB
Python
Raw Normal View History

2021-02-01 15:13:11 +00:00
import re
import traceback
2021-02-01 15:13:11 +00:00
from core.elements import MsgInfo
2021-07-08 15:52:05 +00:00
from core.loader import Modules, ModulesAliases
2021-07-07 16:00:24 +00:00
from core.logger import Logger
from core.template import sendMessage, Nudge, kwargs_AsDisplay, RemoveDuplicateSpace
2021-06-07 13:49:39 +00:00
from core.utils import remove_ineffective_text
2021-07-19 16:12:29 +00:00
from core.parser.command import CommandParser, InvalidCommandFormatError, InvalidHelpDocTypeError
from database import BotDBUtil
2021-02-01 15:13:11 +00:00
2021-06-07 13:49:39 +00:00
command_prefix = ['~', ''] # 消息前缀
2021-02-03 14:43:24 +00:00
2021-07-15 14:59:32 +00:00
async def parser(message: dict):
2021-02-03 07:40:17 +00:00
"""
接收消息必经的预处理器
2021-07-15 14:59:32 +00:00
:param message: 从监听器接收到的dict该dict将会经过此预处理器传入下游
2021-02-03 07:40:17 +00:00
:return: 无返回
"""
2021-07-15 14:59:32 +00:00
display = RemoveDuplicateSpace(kwargs_AsDisplay(message)) # 将消息转换为一般显示形式
senderInfo = BotDBUtil.SenderInfo(message[MsgInfo].senderId)
2021-07-15 14:59:32 +00:00
if senderInfo.query.isInBlackList and not senderInfo.query.isInWhiteList or len(display) == 0:
2021-06-04 13:53:24 +00:00
return
if display[0] in command_prefix: # 检查消息前缀
2021-07-15 14:59:32 +00:00
Logger.info(message)
2021-06-04 13:53:24 +00:00
command = re.sub(r'^' + display[0], '', display)
2021-06-07 13:49:39 +00:00
command_list = remove_ineffective_text(command_prefix, command.split('&&')) # 并行命令处理
2021-07-15 14:59:32 +00:00
if len(command_list) > 5 and not senderInfo.query.isSuperUser:
await sendMessage(message, '你不是本机器人的超级管理员最多只能并排执行5个命令。')
2021-07-12 13:31:11 +00:00
return
2021-06-04 13:53:24 +00:00
for command in command_list:
command_spilt = command.split(' ') # 切割消息
2021-06-04 13:53:24 +00:00
try:
2021-07-15 14:59:32 +00:00
message['trigger_msg'] = command # 触发该命令的消息,去除消息前缀
command_first_word = command_spilt[0]
2021-07-08 15:52:05 +00:00
if command_first_word in ModulesAliases:
command_spilt[0] = ModulesAliases[command_first_word]
command = ' '.join(command_spilt)
command_spilt = command.split(' ')
command_first_word = command_spilt[0]
2021-07-15 14:59:32 +00:00
message['trigger_msg'] = command
2021-07-08 15:52:05 +00:00
if command_first_word in Modules: # 检查触发命令是否在模块列表中
2021-07-15 14:59:32 +00:00
await Nudge(message)
2021-07-09 09:36:38 +00:00
module = Modules[command_first_word]
if module.is_superuser_function:
2021-07-15 14:59:32 +00:00
if not senderInfo.query.isSuperUser:
await sendMessage(message, '你没有使用该命令的权限。')
2021-07-09 09:36:38 +00:00
if module.is_admin_function:
2021-07-13 15:48:43 +00:00
...
if not module.is_base_function:
2021-07-15 14:59:32 +00:00
check_command_enable = BotDBUtil.Module(message).check_target_enabled_module(command_first_word) # 检查群组是否开启模块
if not check_command_enable: # 若未开启
2021-07-15 14:59:32 +00:00
await sendMessage(message, f'此模块未启用,请管理员在群内发送~enable {command_first_word}启用本模块。')
return
2021-07-19 16:12:29 +00:00
help_doc = module.help_doc
if help_doc is not None:
try:
cparser = CommandParser(help_doc)
try:
message['parsed_msg'] = cparser.parse(command)
except InvalidCommandFormatError:
await sendMessage(message, f'语法错误。\n' + cparser.return_formatted_help_doc())
except InvalidHelpDocTypeError:
await sendMessage(message, f'此模块的帮助信息有误,请联系开发者处理。')
2021-07-15 14:59:32 +00:00
await Modules[command_first_word].function(message) # 将dict传入下游模块
2021-06-10 05:49:19 +00:00
except Exception as e:
traceback.print_exc()
2021-07-15 14:59:32 +00:00
await sendMessage(message, '执行命令时发生错误,请报告管理员:\n' + str(e))
#for regex in Modules['regex']: # 遍历正则模块列表
# check_command_enable = database.check_enable_modules(senderId[Group].id,
# regex) # 检查群组是否打开模块
# if check_command_enable:
# await Modules['regex'][regex](senderId) # 将整条dict传入下游正则模块