Skip to content

Commit

Permalink
fix(worker): 修复用户回复0时掉入循环的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
KoishiMoe committed Mar 29, 2022
1 parent 6cbe29b commit b6e63d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions nonebot_plugin_mediawiki/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-mediawiki"
version = "0.1.3"
version = "0.1.4"
description = "nonebot2 mediawiki 查询插件"
authors = ["KoishiChan <[email protected]>"]
license = "AGPL-3.0-or-later"
Expand Down

0 comments on commit b6e63d0

Please sign in to comment.