Archived
1
0
Fork 0

new command parser is working now

This commit is contained in:
yzhh 2021-07-20 00:12:29 +08:00
parent 63440959c4
commit 47b081aef0
7 changed files with 86 additions and 32 deletions

2
bot.py
View file

@ -11,7 +11,7 @@ from graia.application.message.chain import MessageChain
from core.broadcast import bcc, app
from core.elements import MsgInfo
from core.loader import Modules
from core.parser.parser import parser
from core.parser.message import parser
from core.utils import load_prompt as lp
cache_path = os.path.abspath('./cache/')

View file

@ -1,10 +1,11 @@
from core.elements import Module
from .loader import ModulesManager
def command(
bind_prefix,
alias=None,
help_doc='',
help_doc=None,
need_self_process=False,
is_admin_function=False,
is_base_function=False,
@ -22,4 +23,5 @@ def command(
is_superuser_function,
autorun)
ModulesManager.add_module(plugin)
return decorator

View file

@ -39,7 +39,7 @@ class Image:
url=None,
path=None):
if url is not None:
path = asyncio.run(self.get_image())
path = asyncio.run(self.get_image(url))
self.image = path

View file

@ -5,8 +5,8 @@ class Module:
def __init__(self,
function: Callable,
bind_prefix: str,
alias: [str, tuple],
help_doc: str,
alias: [str, list, tuple],
help_doc: [str, list, tuple, None],
need_self_process: bool,
is_admin_function: bool,
is_base_function: bool,

View file

@ -1,5 +1,11 @@
import re
import shlex
from core.docopt import docopt
from core.docopt import docopt, DocoptExit
class InvalidHelpDocTypeError(BaseException):
def __init__(self, *args, **kwargs):
pass
class InvalidCommandFormatError(BaseException):
@ -9,12 +15,47 @@ class InvalidCommandFormatError(BaseException):
class CommandParser:
def __init__(self, args: [str, list, tuple]):
"""
Format: https://github.com/jazzband/docopt-ng#usage-pattern-format
* {} - Detail help information
"""
self.args_raw = args
if isinstance(args, str):
self.args = f'Usage:\n {args}'
elif isinstance(args, (list, tuple)):
self.args = f'Usage:\n ' + '\n '.join(x for x in args)
args = [args]
if isinstance(args, (list, tuple)):
arglst = []
for x in args:
match_detail_help = re.match('(.*){.*}$', x)
if match_detail_help:
x = match_detail_help.group(1)
arglst.append(x)
self.args = f'Usage:\n ' + '\n '.join(y for y in arglst)
else:
raise InvalidCommandFormatError
raise InvalidHelpDocTypeError
def return_formatted_help_doc(self):
args_raw = self.args_raw
if isinstance(args_raw, str):
args_raw = [args_raw]
if isinstance(args_raw, (list, tuple)):
arglst = []
for x in args_raw:
match_detail_help = re.match('(.*){(.*)}$', x)
if match_detail_help:
x = f'{match_detail_help.group(1)} - {match_detail_help.group(2)}'
arglst.append(x)
args = f'用法:\n ' + '\n '.join(y for y in arglst)
else:
raise InvalidHelpDocTypeError
return args
def parse(self, command):
return docopt(self.args, argvs=shlex.split(command)[1:], default_help=False)
split_command = shlex.split(command)
if len(split_command) == 1:
return None
try:
return docopt(self.args, argvs=split_command[1:], default_help=False)
except DocoptExit:
raise InvalidCommandFormatError

View file

@ -6,6 +6,7 @@ from core.loader import Modules, ModulesAliases
from core.logger import Logger
from core.template import sendMessage, Nudge, kwargs_AsDisplay, RemoveDuplicateSpace
from core.utils import remove_ineffective_text
from core.parser.command import CommandParser, InvalidCommandFormatError, InvalidHelpDocTypeError
from database import BotDBUtil
command_prefix = ['~', ''] # 消息前缀
@ -39,8 +40,6 @@ async def parser(message: dict):
command_spilt = command.split(' ')
command_first_word = command_spilt[0]
message['trigger_msg'] = command
if command_first_word == 'ping':
await sendMessage(message, 'pong')
if command_first_word in Modules: # 检查触发命令是否在模块列表中
await Nudge(message)
module = Modules[command_first_word]
@ -54,6 +53,16 @@ async def parser(message: dict):
if not check_command_enable: # 若未开启
await sendMessage(message, f'此模块未启用,请管理员在群内发送~enable {command_first_word}启用本模块。')
return
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'此模块的帮助信息有误,请联系开发者处理。')
await Modules[command_first_word].function(message) # 将dict传入下游模块
except Exception as e:
traceback.print_exc()

View file

@ -4,23 +4,25 @@ from database import BotDBUtil
from core.decorator import command
@command('module', is_base_function=True)
async def config_modules(kwargs: dict):
@command('module',
is_base_function=True,
help_doc='<command> (enable|disable) (<module>|all) {开启或关闭一个/所有模块}')
async def config_modules(message: dict):
"""
~module <enable/disable> <module/all>"""
command = kwargs['trigger_msg'].split(' ')
if not len(command) > 2:
msg = '命令格式错误。'
await sendMessage(kwargs, msg)
return
do = command[1]
command_third_word = command[2]
if message['parsed_msg']['enable']:
do = 'enable'
elif message['parsed_msg']['disable']:
do = 'disable'
command_third_word = message['parsed_msg']['<module>']
if message['parsed_msg']['all']:
command_third_word = 'all'
if command_third_word in ModulesManager.return_modules_alias_map():
command_third_word = ModulesManager.return_modules_alias_map()[command_third_word]
#if not check_permission(message):
# await sendMessage(message, '你没有使用该命令的权限。')
# return
query = BotDBUtil.Module(kwargs)
query = BotDBUtil.Module(message)
msglist = []
if command_third_word == 'all':
for function in ModulesManager.return_modules_list_as_dict():
@ -33,7 +35,7 @@ async def config_modules(kwargs: dict):
if query.disable(command_third_word):
msglist.append(f'成功:关闭模块“{command_third_word}')
if msglist is not None:
await sendMessage(kwargs, '\n'.join(msglist))
await sendMessage(message, '\n'.join(msglist))
"""
async def bot_help(message: dict):