From 307739e37a9e87d4f42c5a557090b08a5025ac96 Mon Sep 17 00:00:00 2001 From: Azide Date: Sun, 19 May 2024 22:02:23 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20ParseTargetException=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=90=BA=E5=B8=A6prompt=E5=8F=82=E6=95=B0=20(#479)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: ParseTargetException可以携带prompt参数 * :sparkles: 丰富prompt提示 --- nonebot_bison/platform/bilibili.py | 10 ++++++++-- nonebot_bison/platform/ncm.py | 6 ++++-- nonebot_bison/platform/platform.py | 6 +++++- nonebot_bison/platform/weibo.py | 2 +- nonebot_bison/sub_manager/add_sub.py | 6 ++++-- tests/sub_manager/test_add.py | 14 ++++++++++++-- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/nonebot_bison/platform/bilibili.py b/nonebot_bison/platform/bilibili.py index e893659..acd67d6 100644 --- a/nonebot_bison/platform/bilibili.py +++ b/nonebot_bison/platform/bilibili.py @@ -188,10 +188,14 @@ class Bilibili(NewMessage): async def parse_target(cls, target_text: str) -> Target: if re.match(r"\d+", target_text): return Target(target_text) + elif re.match(r"UID:\d+", target_text): + return Target(target_text[4:]) elif m := re.match(r"(?:https?://)?space\.bilibili\.com/(\d+)", target_text): return Target(m.group(1)) else: - raise cls.ParseTargetException() + raise cls.ParseTargetException( + prompt="正确格式:\n1. 用户纯数字id\n2. UID:<用户id>\n3. 用户主页链接: https://space.bilibili.com/xxxx" + ) async def get_sub_list(self, target: Target) -> list[DynRawPost]: params = {"host_uid": target, "offset": 0, "need_top": 0} @@ -511,7 +515,9 @@ class BilibiliBangumi(StatusChange): return Target(m[1]) elif m := re.match(r"(?:https?://)?www\.bilibili\.com/bangumi/media/md(\d+)", target_string): return Target(m[1]) - raise cls.ParseTargetException() + raise cls.ParseTargetException( + prompt="正确格式:\n1. 剧集id\n2. 剧集主页链接 https://www.bilibili.com/bangumi/media/mdxxxx" + ) async def get_status(self, target: Target): res = await self.client.get( diff --git a/nonebot_bison/platform/ncm.py b/nonebot_bison/platform/ncm.py index 031dc93..538518a 100644 --- a/nonebot_bison/platform/ncm.py +++ b/nonebot_bison/platform/ncm.py @@ -44,7 +44,7 @@ class NcmArtist(NewMessage): elif match := re.match(r"(?:https?://)?music\.163\.com/#/artist\?id=(\d+)", target_text): return Target(match.group(1)) else: - raise cls.ParseTargetException() + raise cls.ParseTargetException("正确格式:\n1. 歌手数字ID\n2. https://music.163.com/#/artist?id=xxxx") async def get_sub_list(self, target: Target) -> list[RawPost]: res = await self.client.get( @@ -101,7 +101,9 @@ class NcmRadio(NewMessage): elif match := re.match(r"(?:https?://)?music\.163\.com/#/djradio\?id=(\d+)", target_text): return Target(match.group(1)) else: - raise cls.ParseTargetException() + raise cls.ParseTargetException( + prompt="正确格式:\n1. 电台数字ID\n2. https://music.163.com/#/djradio?id=xxxx" + ) async def get_sub_list(self, target: Target) -> list[RawPost]: res = await self.client.post( diff --git a/nonebot_bison/platform/platform.py b/nonebot_bison/platform/platform.py index 0c902c6..1e64819 100644 --- a/nonebot_bison/platform/platform.py +++ b/nonebot_bison/platform/platform.py @@ -127,7 +127,11 @@ class Platform(metaclass=PlatformABCMeta, base=True): self.ctx = context class ParseTargetException(Exception): - pass + def __init__(self, *args: object, prompt: str | None = None) -> None: + super().__init__(*args) + + self.prompt = prompt + """用户输入提示信息""" @classmethod async def parse_target(cls, target_string: str) -> Target: diff --git a/nonebot_bison/platform/weibo.py b/nonebot_bison/platform/weibo.py index 54c2a52..1b4d181 100644 --- a/nonebot_bison/platform/weibo.py +++ b/nonebot_bison/platform/weibo.py @@ -75,7 +75,7 @@ class Weibo(NewMessage): # 都2202年了应该不会有http了吧,不过还是防一手 return Target(match.group(1)) else: - raise cls.ParseTargetException() + raise cls.ParseTargetException(prompt="正确格式:\n1. 用户数字UID\n2. https://weibo.com/u/xxxx") async def get_sub_list(self, target: Target) -> list[RawPost]: params = {"containerid": "107603" + target} diff --git a/nonebot_bison/sub_manager/add_sub.py b/nonebot_bison/sub_manager/add_sub.py index 7bf7f1e..38dddf0 100644 --- a/nonebot_bison/sub_manager/add_sub.py +++ b/nonebot_bison/sub_manager/add_sub.py @@ -86,8 +86,10 @@ def do_add_sub(add_sub: type[Matcher]): await add_sub.reject("id输入错误") state["id"] = raw_id_text state["name"] = name - except Platform.ParseTargetException: - await add_sub.reject("不能从你的输入中提取出id,请检查你输入的内容是否符合预期") + except Platform.ParseTargetException as e: + await add_sub.reject( + "不能从你的输入中提取出id,请检查你输入的内容是否符合预期" + (f"\n{e.prompt}" if e.prompt else "") + ) else: await add_sub.send( f"即将订阅的用户为:{state['platform']} {state['name']} {state['id']}\n如有错误请输入“取消”重新订阅" diff --git a/tests/sub_manager/test_add.py b/tests/sub_manager/test_add.py index 982938e..0d7c500 100644 --- a/tests/sub_manager/test_add.py +++ b/tests/sub_manager/test_add.py @@ -407,7 +407,12 @@ async def test_add_with_bilibili_target_parser(app: App, init_scheduler): sender=fake_admin_user, ) ctx.receive_event(bot, event_4_err1) - ctx.should_call_send(event_4_err1, BotReply.add_reply_on_target_parse_input_error, True) + ctx.should_call_send( + event_4_err1, + BotReply.add_reply_on_target_parse_input_error + + "\n正确格式:\n1. 用户纯数字id\n2. UID:<用户id>\n3. 用户主页链接: https://space.bilibili.com/xxxx", + True, + ) ctx.should_rejected() event_4_err1 = fake_group_message_event( @@ -417,7 +422,12 @@ async def test_add_with_bilibili_target_parser(app: App, init_scheduler): sender=fake_admin_user, ) ctx.receive_event(bot, event_4_err1) - ctx.should_call_send(event_4_err1, BotReply.add_reply_on_target_parse_input_error, True) + ctx.should_call_send( + event_4_err1, + BotReply.add_reply_on_target_parse_input_error + + "\n正确格式:\n1. 用户纯数字id\n2. UID:<用户id>\n3. 用户主页链接: https://space.bilibili.com/xxxx", + True, + ) ctx.should_rejected() event_4_ok = fake_group_message_event(