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

80 lines
4 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 graia.application import Friend
2021-06-07 13:49:39 +00:00
from graia.application.group import Group
2021-02-01 15:13:11 +00:00
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
2021-06-08 12:53:49 +00:00
from core.template import sendMessage, Nudge, kwargs_GetTrigger, kwargs_AsDisplay, RemoveDuplicateSpace
2021-06-07 13:49:39 +00:00
from core.utils import remove_ineffective_text
from database import BotDB as database
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-02-01 15:13:11 +00:00
async def parser(kwargs: dict):
2021-02-03 07:40:17 +00:00
"""
接收消息必经的预处理器
:param kwargs: 从监听器接收到的dict该dict将会经过此预处理器传入下游
:return: 无返回
"""
2021-06-08 12:53:49 +00:00
display = RemoveDuplicateSpace(kwargs_AsDisplay(kwargs)) # 将消息转换为一般显示形式
2021-06-04 13:53:24 +00:00
if len(display) == 0: # 转换后若为空消息则停止执行
return
2021-06-07 13:49:39 +00:00
trigger = kwargs_GetTrigger(kwargs) # 得到触发者来源
2021-06-04 13:53:24 +00:00
if trigger == 1143754816: # 特殊规则
display = re.sub('^.*:\n', '', display)
if database.check_black_list(trigger): # 检查是否在黑名单
if not database.check_white_list(trigger): # 检查是否在白名单
return # 在黑名单且不在白名单,给我爪巴
if display.find('色图来') != -1: # 双倍快乐给我爬
return
if display[0] in command_prefix: # 检查消息前缀
2021-07-07 16:00:24 +00:00
Logger.info(kwargs)
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-06-04 13:53:24 +00:00
if len(command_list) > 5:
if not database.check_superuser(kwargs):
await sendMessage(kwargs, '你不是本机器人的超级管理员最多只能并排执行5个命令。')
return
for command in command_list:
command_spilt = command.split(' ') # 切割消息
2021-06-04 13:53:24 +00:00
try:
kwargs['trigger_msg'] = command # 触发该命令的消息,去除消息前缀
kwargs['bot_modules'] = Modules
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]
kwargs['trigger_msg'] = command
2021-07-08 15:52:05 +00:00
if command_first_word in Modules: # 检查触发命令是否在模块列表中
await Nudge(kwargs)
plugin = Modules[command_first_word]
if plugin.is_superuser_function:
...
if plugin.is_admin_function:
...
check_command_enable = database.check_enable_modules(kwargs[Group].id,
command_first_word) # 检查群组是否开启模块
#if not check_command_enable: # 若未开启
# await sendMessage(kwargs, f'此模块未启用,请管理员在群内发送~enable {command_first_word}启用本模块。')
# return
await Modules[command_first_word].function(kwargs) # 将dict传入下游模块
2021-06-10 05:49:19 +00:00
except Exception as e:
traceback.print_exc()
await sendMessage(kwargs, '执行命令时发生错误,请报告管理员:\n' + str(e))
2021-06-04 13:53:24 +00:00
# 正则模块部分
if Group in kwargs:
for regex in Modules['regex']: # 遍历正则模块列表
check_command_enable = database.check_enable_modules(kwargs[Group].id,
regex) # 检查群组是否打开模块
if check_command_enable:
await Modules['regex'][regex](kwargs) # 将整条dict传入下游正则模块
if Friend in kwargs:
for regex in Modules['regex']:
await Modules['regex'][regex](kwargs)
return