From b6e63d0bba7b2a7251d6e57c0f9ec30927343f68 Mon Sep 17 00:00:00 2001 From: KoishiChan <68314080+KoishiStudio@users.noreply.github.com> Date: Tue, 29 Mar 2022 20:19:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(worker):=20=E4=BF=AE=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=9B=9E=E5=A4=8D0=E6=97=B6=E6=8E=89=E5=85=A5?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_mediawiki/worker.py | 11 ++++++++--- pyproject.toml | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/nonebot_plugin_mediawiki/worker.py b/nonebot_plugin_mediawiki/worker.py index 264e185..22f840d 100644 --- a/nonebot_plugin_mediawiki/worker.py +++ b/nonebot_plugin_mediawiki/worker.py @@ -21,19 +21,24 @@ async def wiki_process(bot: Bot, event: GroupMessageEvent, wiki: Type[Matcher], is_template: bool = False): global titles message = str(event.message).strip() + is_raw = False # 回复0的话这里应该变成True,否则会掉进循环( if message.isdigit(): if 0 <= int(message) < len(titles) - 1: event.message = Message(f"[[{titles[-1]}:{titles[int(message)]}]]") is_template = False # 否则会导致下面发生误判,导致无法匹配 + is_raw = bool(1 - bool(int(message))) else: return - special, result = await wiki_parse(ARTICLE if not is_template else TEMPLATE, is_template, False, bot, event) + try: + special, result = await wiki_parse(ARTICLE if not is_template else TEMPLATE, is_template, is_raw, bot, event) + except TypeError: + return # 这里应当是bot返回选择列表后,用户没有做出有效选择,无害 if special: titles = result[:-1] titles.insert(0, result[-1][1]) titles.append(result[-1][2]) title_list = '\n'.join([f'{i + 1}.{result[i]}' for i in range(len(result) - 1)]) # 最后一个元素是特殊标记 - msg = f"{f'页面“{result[-1][1]}”不存在,下面是推荐的结果' if result[-1][0] else f'页面{result[-1][1]}是消歧义页面'}," \ + msg = f"{f'页面“{result[-1][1]}”不存在,下面是推荐的结果' if result[-1][0] else f'页面“{result[-1][1]}”是消歧义页面'}," \ f"请回复数字来选择你想要查询的条目,或者回复0来根据原标题直接生成链接:\n" \ f"{title_list}" await wiki.reject(msg) @@ -42,7 +47,7 @@ async def wiki_process(bot: Bot, event: GroupMessageEvent, wiki: Type[Matcher], titles = [] -async def wiki_parse(pattern: str, is_template: bool, is_raw: bool, bot: Bot, event: GroupMessageEvent) -> tuple: +async def wiki_parse(pattern: str, is_template: bool, is_raw: bool, bot: Bot, event: GroupMessageEvent) -> tuple | None: msg = str(event.message).strip() msg = utils.unescape(msg) # 将消息处理为正常格式,以防搜索出错 temp_config: Config = Config(event.group_id) diff --git a/pyproject.toml b/pyproject.toml index a69c28d..5b1fa07 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "nonebot-plugin-mediawiki" -version = "0.1.3" +version = "0.1.4" description = "nonebot2 mediawiki 查询插件" authors = ["KoishiChan <68314080+KoishiStudio@users.noreply.github.com>"] license = "AGPL-3.0-or-later"