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