mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
🎨 use Arg to replace assert
This commit is contained in:
parent
4846d32e2e
commit
9f1730093c
@ -37,8 +37,6 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
|
||||
@add_sub.got("platform", MessageTemplate("{_prompt}"), [handle_cancel])
|
||||
async def parse_platform(state: T_State, platform: str = ArgPlainText()) -> None:
|
||||
if not isinstance(state["platform"], Message):
|
||||
return
|
||||
if platform == "全部":
|
||||
message = "全部平台\n" + "\n".join(
|
||||
[
|
||||
@ -152,9 +150,9 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
state["tags"] = raw_tags_text.split()
|
||||
|
||||
@add_sub.handle()
|
||||
async def add_sub_process(state: T_State):
|
||||
user = cast(PlatformTarget, state.get("target_user_info"))
|
||||
assert isinstance(user, PlatformTarget)
|
||||
async def add_sub_process(
|
||||
state: T_State, user: PlatformTarget = Arg("target_user_info")
|
||||
):
|
||||
try:
|
||||
await config.add_subscribe(
|
||||
user=user,
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import Type
|
||||
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import EventPlainText
|
||||
from nonebot.params import Arg, EventPlainText
|
||||
from nonebot.typing import T_State
|
||||
from nonebot_plugin_saa import MessageFactory, PlatformTarget
|
||||
|
||||
@ -18,50 +18,49 @@ def do_del_sub(del_sub: Type[Matcher]):
|
||||
del_sub.handle()(ensure_user_info(del_sub))
|
||||
|
||||
@del_sub.handle()
|
||||
async def send_list(state: T_State):
|
||||
user_info = state["target_user_info"]
|
||||
assert isinstance(user_info, PlatformTarget)
|
||||
try:
|
||||
sub_list = await config.list_subscribe(user_info)
|
||||
assert sub_list
|
||||
except AssertionError:
|
||||
async def send_list(
|
||||
state: T_State, user_info: PlatformTarget = Arg("target_user_info")
|
||||
):
|
||||
sub_list = await config.list_subscribe(user_info)
|
||||
if not sub_list:
|
||||
await del_sub.finish("暂无已订阅账号\n请使用“添加订阅”命令添加订阅")
|
||||
else:
|
||||
res = "订阅的帐号为:\n"
|
||||
state["sub_table"] = {}
|
||||
for index, sub in enumerate(sub_list, 1):
|
||||
state["sub_table"][index] = {
|
||||
"platform_name": sub.target.platform_name,
|
||||
"target": sub.target.target,
|
||||
}
|
||||
res += "{} {} {} {}\n".format(
|
||||
index,
|
||||
sub.target.platform_name,
|
||||
sub.target.target_name,
|
||||
sub.target.target,
|
||||
)
|
||||
platform = platform_manager[sub.target.platform_name]
|
||||
if platform.categories:
|
||||
res += " [{}]".format(
|
||||
", ".join(
|
||||
map(
|
||||
lambda x: platform.categories[Category(x)],
|
||||
sub.categories,
|
||||
)
|
||||
res = "订阅的帐号为:\n"
|
||||
state["sub_table"] = {}
|
||||
for index, sub in enumerate(sub_list, 1):
|
||||
state["sub_table"][index] = {
|
||||
"platform_name": sub.target.platform_name,
|
||||
"target": sub.target.target,
|
||||
}
|
||||
res += "{} {} {} {}\n".format(
|
||||
index,
|
||||
sub.target.platform_name,
|
||||
sub.target.target_name,
|
||||
sub.target.target,
|
||||
)
|
||||
platform = platform_manager[sub.target.platform_name]
|
||||
if platform.categories:
|
||||
res += " [{}]".format(
|
||||
", ".join(
|
||||
map(
|
||||
lambda x: platform.categories[Category(x)],
|
||||
sub.categories,
|
||||
)
|
||||
)
|
||||
if platform.enable_tag:
|
||||
res += " {}".format(", ".join(sub.tags))
|
||||
res += "\n"
|
||||
res += "请输入要删除的订阅的序号\n输入'取消'中止"
|
||||
await MessageFactory(await parse_text(res)).send()
|
||||
)
|
||||
if platform.enable_tag:
|
||||
res += " {}".format(", ".join(sub.tags))
|
||||
res += "\n"
|
||||
res += "请输入要删除的订阅的序号\n输入'取消'中止"
|
||||
await MessageFactory(await parse_text(res)).send()
|
||||
|
||||
@del_sub.receive(parameterless=[handle_cancel])
|
||||
async def do_del(state: T_State, index_str: str = EventPlainText()):
|
||||
async def do_del(
|
||||
state: T_State,
|
||||
index_str: str = EventPlainText(),
|
||||
user_info: PlatformTarget = Arg("target_user_info"),
|
||||
):
|
||||
try:
|
||||
index = int(index_str)
|
||||
user_info = state["target_user_info"]
|
||||
assert isinstance(user_info, PlatformTarget)
|
||||
await config.del_subscribe(user_info, **state["sub_table"][index])
|
||||
except Exception as e:
|
||||
await del_sub.reject("删除错误")
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import Type
|
||||
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.params import Arg
|
||||
from nonebot_plugin_saa import MessageFactory, PlatformTarget
|
||||
|
||||
from ..config import config
|
||||
@ -15,9 +15,7 @@ def do_query_sub(query_sub: Type[Matcher]):
|
||||
query_sub.handle()(ensure_user_info(query_sub))
|
||||
|
||||
@query_sub.handle()
|
||||
async def _(state: T_State):
|
||||
user_info = state["target_user_info"]
|
||||
assert isinstance(user_info, PlatformTarget)
|
||||
async def _(user_info: PlatformTarget = Arg("target_user_info")):
|
||||
sub_list = await config.list_subscribe(user_info)
|
||||
res = "订阅的帐号为:\n"
|
||||
for sub in sub_list:
|
||||
|
Loading…
x
Reference in New Issue
Block a user