mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
✨ ParseTargetException可以携带prompt参数 (#479)
* ✨ ParseTargetException可以携带prompt参数 * ✨ 丰富prompt提示
This commit is contained in:
parent
4bbcd9238c
commit
307739e37a
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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:
|
||||
|
@ -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}
|
||||
|
@ -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如有错误请输入“取消”重新订阅"
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user