Archived
1
0
Fork 0
This commit is contained in:
yzhh 2022-08-23 15:36:07 +08:00
parent 2f389a2744
commit a9138424a4
4 changed files with 43 additions and 27 deletions

View file

@ -11,9 +11,13 @@ class Pattern:
class Template:
def __init__(self, args: List[Pattern], priority: int = 1):
self.args = args
self.args_ = args
self.priority = priority
@property
def args(self):
return self.args_
def __str__(self):
return 'Template({})'.format(self.args)
@ -201,10 +205,10 @@ def parse_argv(argv: List[str], templates: List[Template]) -> MatchedResult:
parsed_argv = {}
original_template = template
afters = []
template.args = [x for x in template.args if not isinstance(x, DescPattern)]
if not template.args:
args = [x for x in template.args if not isinstance(x, DescPattern)]
if not args:
continue
for a in template.args: # optional first
for a in args: # optional first
if isinstance(a, OptionalPattern):
if a.flag is None:
afters.append(a.args[0])
@ -221,7 +225,7 @@ def parse_argv(argv: List[str], templates: List[Template]) -> MatchedResult:
sub_argv = argv_copy[index_flag + 1: index_flag + len_t_args + 1]
parsed_argv[a.flag] = Optional(parse_argv(sub_argv, a.args).args, flagged=True)
del argv_copy[index_flag: index_flag + len_t_args + 1]
for a in template.args:
for a in args:
if isinstance(a, ArgumentPattern):
if a.name.startswith('<'):
if len(argv_copy) > 0:
@ -230,7 +234,7 @@ def parse_argv(argv: List[str], templates: List[Template]) -> MatchedResult:
else:
parsed_argv[a.name] = False
elif a.name == '...':
if len(template.args) - 1 == template.args.index(a):
if len(args) - 1 == args.index(a):
afters.append(Template([a]))
else:
raise InvalidTemplatePattern('... must be the last argument')
@ -257,7 +261,7 @@ def parse_argv(argv: List[str], templates: List[Template]) -> MatchedResult:
if parsed_argv[sub_args.name]:
argv_copy.remove(sub_args.name)
if argv_copy:
template_arguments = [arg for arg in template.args if isinstance(arg, ArgumentPattern)]
template_arguments = [arg for arg in args if isinstance(arg, ArgumentPattern)]
if template_arguments:
if isinstance(template_arguments[-1], ArgumentPattern):
if template_arguments[-1].name.startswith('<'): # if last arg is variable

View file

@ -1,3 +1,4 @@
import copy
import difflib
import re
import traceback
@ -414,12 +415,12 @@ async def typo_check(msg: MessageSession, display_prefix, modules, command_first
get_submodules: List[CommandMeta] = module.match_list.get(msg.target.targetFrom)
docs = {} # 根据命令模板的空格数排序命令
for func in get_submodules:
help_doc: List[Template] = func.help_doc
help_doc: List[Template] = copy.deepcopy(func.help_doc)
if not help_doc:
... # todo: ...此处应该有一个处理例外情况的逻辑
for h_ in help_doc:
h_.args = [a for a in h_.args if isinstance(a, ArgumentPattern)]
h_.args_ = [a for a in h_.args if isinstance(a, ArgumentPattern)]
if (len_args := len(h_.args)) not in docs:
docs[len_args] = [h_]
else:

View file

@ -35,8 +35,10 @@ module = on_command('module',
)
@module.handle(['enable (<module>...|all) {开启一个/多个或所有模块}',
'disable (<module>...|all) {关闭一个/多个或所有模块}',
@module.handle(['enable <module>... {开启一个/多个模块}',
'enable all {开启所有模块}',
'disable <module>... {关闭一个/多个模块}',
'disable all {关闭所有模块。}',
'list {查看所有可用模块}'], exclude_from=['QQ|Guild'])
async def _(msg: MessageSession):
if msg.parsed_msg.get('list', False):
@ -44,8 +46,10 @@ async def _(msg: MessageSession):
await config_modules(msg)
@module.handle(['enable (<module>...|all) [-g] {开启一个/多个或所有模块}',
'disable (<module>...|all) [-g] {关闭一个/多个或所有模块}',
@module.handle(['enable <module>... [-g] {开启一个/多个模块}',
'enable all [-g] {开启所有模块}',
'disable <module> [-g] {关闭一个/多个模块}',
'disable all [-g] {关闭所有模块。}',
'list {查看所有可用模块}'], options_desc={'-g': '对频道进行全局操作'},
available_for=['QQ|Guild'])
async def _(msg: MessageSession):
@ -242,7 +246,8 @@ async def _(msg: MessageSession):
appends = [module_.bind_prefix]
doc_ = []
if isinstance(module_, Command):
help_ = CommandParser(module_, msg=msg, bind_prefix=module_.bind_prefix, command_prefixes=msg.prefixes)
help_ = CommandParser(module_, msg=msg, bind_prefix=module_.bind_prefix,
command_prefixes=msg.prefixes)
if module_.desc is not None:
doc_.append(module_.desc)
@ -272,11 +277,12 @@ async def _(msg: MessageSession):
if render:
legacy_help = False
await msg.finish([Image(render),
Plain(f'此处展示的帮助文档仅展示已开启的模块,若需要查看全部模块的帮助文档,请使用{msg.prefixes[0]}module list命令。'
'\n你也可以通过查阅文档获取帮助:'
'\nhttps://bot.teahouse.team/wiki/'
'\n若您有经济实力,欢迎给孩子们在爱发电上打钱:'
'\nhttps://afdian.net/@teahouse')])
Plain(
f'此处展示的帮助文档仅展示已开启的模块,若需要查看全部模块的帮助文档,请使用{msg.prefixes[0]}module list命令。'
'\n你也可以通过查阅文档获取帮助:'
'\nhttps://bot.teahouse.team/wiki/'
'\n若您有经济实力,欢迎给孩子们在爱发电上打钱:'
'\nhttps://afdian.net/@teahouse')])
except Exception:
traceback.print_exc()
if legacy_help:
@ -366,7 +372,8 @@ async def modules_help(msg: MessageSession):
p = on_command('prefix', required_admin=True, base=True)
@p.handle('add <prefix> {设置自定义机器人命令前缀}', 'remove <prefix> {移除自定义机器人命令前缀}', 'reset {重置自定义机器人命令前缀}')
@p.handle('add <prefix> {设置自定义机器人命令前缀}', 'remove <prefix> {移除自定义机器人命令前缀}',
'reset {重置自定义机器人命令前缀}')
async def set_prefix(msg: MessageSession):
options = BotDBUtil.Options(msg)
prefixes = options.get('command_prefix')
@ -395,7 +402,8 @@ async def set_prefix(msg: MessageSession):
ali = on_command('alias', required_admin=True, base=True)
@ali.handle('add <alias> <command> {添加自定义命令别名}', 'remove <alias> {移除自定义命令别名}', 'reset {重置自定义命令别名}')
@ali.handle('add <alias> <command> {添加自定义命令别名}', 'remove <alias> {移除自定义命令别名}',
'reset {重置自定义命令别名}')
async def set_alias(msg: MessageSession):
options = BotDBUtil.Options(msg)
alias = options.get('command_alias')
@ -522,11 +530,11 @@ async def config_gu(msg: MessageSession):
locale = on_command('locale',
base=True,
required_admin=True,
developers=['Dianliang233'],
desc='用于设置机器人运行语言。'
)
base=True,
required_admin=True,
developers=['Dianliang233'],
desc='用于设置机器人运行语言。'
)
@locale.handle(['<lang> {设置机器人运行语言}'])
@ -540,6 +548,7 @@ async def config_gu(msg: MessageSession):
else:
await msg.finish(f"语言格式错误,支持的语言有:{''.join(get_available_locales())}")
su = on_command('superuser', alias=['su'], developers=['OasisAkari', 'Dianliang233'], required_superuser=True)
@ -623,7 +632,8 @@ async def _(msg: MessageSession):
plt.savefig(path)
plt.close()
await msg.finish(
[Plain(f'最近30天的{module_ if module_ is not None else ""}命令调用次数统计(自{str(first_record.timestamp)}开始统计):'),
[Plain(
f'最近30天的{module_ if module_ is not None else ""}命令调用次数统计(自{str(first_record.timestamp)}开始统计):'),
Image(path)])

View file

@ -41,6 +41,7 @@ async def pttimg(msg: MessageSession):
pttimg = 1
elif ptt >= 0:
pttimg = 0
else:
pttimg = 'off'
pttimgr = Image.open(f'{assets_path}/ptt/rating_{str(pttimg)}.png')