mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
🎨 按ruff的检查调整程序代码
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
import contextlib
|
||||
from typing import Type, cast
|
||||
|
||||
from nonebot.adapters import Message, MessageTemplate
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Arg, ArgPlainText
|
||||
from nonebot.typing import T_State
|
||||
from nonebot_plugin_saa import PlatformTarget, SupportedAdapters, Text
|
||||
from nonebot.adapters import Message, MessageTemplate
|
||||
from nonebot_plugin_saa import Text, PlatformTarget, SupportedAdapters
|
||||
|
||||
from ..apis import check_sub_target
|
||||
from ..config import config
|
||||
from ..config.db_config import SubscribeDupException
|
||||
from ..platform import Platform, platform_manager
|
||||
from ..types import Target
|
||||
from ..config import config
|
||||
from ..apis import check_sub_target
|
||||
from ..platform import Platform, platform_manager
|
||||
from ..config.db_config import SubscribeDupException
|
||||
from .utils import common_platform, ensure_user_info, gen_handle_cancel
|
||||
|
||||
|
||||
def do_add_sub(add_sub: Type[Matcher]):
|
||||
def do_add_sub(add_sub: type[Matcher]):
|
||||
handle_cancel = gen_handle_cancel(add_sub, "已中止订阅")
|
||||
|
||||
add_sub.handle()(ensure_user_info(add_sub))
|
||||
@@ -25,12 +24,7 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
state["_prompt"] = (
|
||||
"请输入想要订阅的平台,目前支持,请输入冒号左边的名称:\n"
|
||||
+ "".join(
|
||||
[
|
||||
"{}:{}\n".format(
|
||||
platform_name, platform_manager[platform_name].name
|
||||
)
|
||||
for platform_name in common_platform
|
||||
]
|
||||
[f"{platform_name}: {platform_manager[platform_name].name}\n" for platform_name in common_platform]
|
||||
)
|
||||
+ "要查看全部平台请输入:“全部”\n中止订阅过程请输入:“取消”"
|
||||
)
|
||||
@@ -39,10 +33,7 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
async def parse_platform(state: T_State, platform: str = ArgPlainText()) -> None:
|
||||
if platform == "全部":
|
||||
message = "全部平台\n" + "\n".join(
|
||||
[
|
||||
"{}:{}".format(platform_name, platform.name)
|
||||
for platform_name, platform in platform_manager.items()
|
||||
]
|
||||
[f"{platform_name}: {platform.name}" for platform_name, platform in platform_manager.items()]
|
||||
)
|
||||
await add_sub.reject(message)
|
||||
elif platform == "取消":
|
||||
@@ -57,9 +48,7 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
cur_platform = platform_manager[state["platform"]]
|
||||
if cur_platform.has_target:
|
||||
state["_prompt"] = (
|
||||
("1." + cur_platform.parse_target_promot + "\n2.")
|
||||
if cur_platform.parse_target_promot
|
||||
else ""
|
||||
("1." + cur_platform.parse_target_promot + "\n2.") if cur_platform.parse_target_promot else ""
|
||||
) + "请输入订阅用户的id\n查询id获取方法请回复:“查询”"
|
||||
else:
|
||||
matcher.set_arg("raw_id", None) # type: ignore
|
||||
@@ -81,9 +70,7 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
image = "https://s3.bmp.ovh/imgs/2022/03/ab3cc45d83bd3dd3.jpg"
|
||||
msg.overwrite(
|
||||
SupportedAdapters.onebot_v11,
|
||||
MessageSegment.share(
|
||||
url=url, title=title, content=content, image=image
|
||||
),
|
||||
MessageSegment.share(url=url, title=title, content=content, image=image),
|
||||
)
|
||||
await msg.reject()
|
||||
platform = platform_manager[state["platform"]]
|
||||
@@ -99,14 +86,12 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
await add_sub.reject("id输入错误")
|
||||
state["id"] = raw_id_text
|
||||
state["name"] = name
|
||||
except (Platform.ParseTargetException):
|
||||
except Platform.ParseTargetException:
|
||||
await add_sub.reject("不能从你的输入中提取出id,请检查你输入的内容是否符合预期")
|
||||
else:
|
||||
await add_sub.send(
|
||||
"即将订阅的用户为:{} {} {}\n如有错误请输入“取消”重新订阅".format(
|
||||
state["platform"], state["name"], state["id"]
|
||||
)
|
||||
)
|
||||
f"即将订阅的用户为:{state['platform']} {state['name']} {state['id']}\n如有错误请输入“取消”重新订阅"
|
||||
) # noqa: E501
|
||||
|
||||
@add_sub.handle()
|
||||
async def prepare_get_categories(matcher: Matcher, state: T_State):
|
||||
@@ -125,7 +110,7 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
if platform_manager[state["platform"]].categories:
|
||||
for cat in raw_cats_text.split():
|
||||
if cat not in platform_manager[state["platform"]].reverse_category:
|
||||
await add_sub.reject("不支持 {}".format(cat))
|
||||
await add_sub.reject(f"不支持 {cat}")
|
||||
res.append(platform_manager[state["platform"]].reverse_category[cat])
|
||||
state["cats"] = res
|
||||
|
||||
@@ -135,14 +120,16 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
matcher.set_arg("raw_tags", None) # type: ignore
|
||||
state["tags"] = []
|
||||
return
|
||||
state["_prompt"] = '请输入要订阅/屏蔽的标签(不含#号)\n多个标签请使用空格隔开\n订阅所有标签输入"全部标签"\n具体规则回复"详情"'
|
||||
state["_prompt"] = "请输入要订阅/屏蔽的标签(不含#号)\n" "多个标签请使用空格隔开\n" '订阅所有标签输入"全部标签"\n' '具体规则回复"详情"' # noqa: E501
|
||||
|
||||
@add_sub.got("raw_tags", MessageTemplate("{_prompt}"), [handle_cancel])
|
||||
async def parser_tags(state: T_State, raw_tags: Message = Arg()):
|
||||
raw_tags_text = raw_tags.extract_plain_text()
|
||||
if raw_tags_text == "详情":
|
||||
await add_sub.reject(
|
||||
"订阅标签直接输入标签内容\n屏蔽标签请在标签名称前添加~号\n详见https://nonebot-bison.netlify.app/usage/#%E5%B9%B3%E5%8F%B0%E8%AE%A2%E9%98%85%E6%A0%87%E7%AD%BE-tag"
|
||||
"订阅标签直接输入标签内容\n"
|
||||
"屏蔽标签请在标签名称前添加~号\n"
|
||||
"详见https://nonebot-bison.netlify.app/usage/#%E5%B9%B3%E5%8F%B0%E8%AE%A2%E9%98%85%E6%A0%87%E7%AD%BE-tag"
|
||||
)
|
||||
if raw_tags_text in ["全部标签", "全部", "全标签"]:
|
||||
state["tags"] = []
|
||||
@@ -150,9 +137,7 @@ 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: PlatformTarget = Arg("target_user_info")
|
||||
):
|
||||
async def add_sub_process(state: T_State, user: PlatformTarget = Arg("target_user_info")):
|
||||
try:
|
||||
await config.add_subscribe(
|
||||
user=user,
|
||||
|
||||
@@ -1,26 +1,22 @@
|
||||
from typing import Type
|
||||
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Arg, EventPlainText
|
||||
from nonebot.typing import T_State
|
||||
from nonebot_plugin_saa import MessageFactory, PlatformTarget
|
||||
|
||||
from ..config import config
|
||||
from ..platform import platform_manager
|
||||
from ..types import Category
|
||||
from ..utils import parse_text
|
||||
from ..platform import platform_manager
|
||||
from .utils import ensure_user_info, gen_handle_cancel
|
||||
|
||||
|
||||
def do_del_sub(del_sub: Type[Matcher]):
|
||||
def do_del_sub(del_sub: type[Matcher]):
|
||||
handle_cancel = gen_handle_cancel(del_sub, "删除中止")
|
||||
|
||||
del_sub.handle()(ensure_user_info(del_sub))
|
||||
|
||||
@del_sub.handle()
|
||||
async def send_list(
|
||||
state: T_State, user_info: PlatformTarget = Arg("target_user_info")
|
||||
):
|
||||
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请使用“添加订阅”命令添加订阅")
|
||||
@@ -31,22 +27,10 @@ def do_del_sub(del_sub: Type[Matcher]):
|
||||
"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,
|
||||
)
|
||||
res += f"{index} {sub.target.platform_name} {sub.target.target_name} {sub.target.target}\n"
|
||||
platform = platform_manager[sub.target.platform_name]
|
||||
if platform.categories:
|
||||
res += " [{}]".format(
|
||||
", ".join(
|
||||
map(
|
||||
lambda x: platform.categories[Category(x)],
|
||||
sub.categories,
|
||||
)
|
||||
)
|
||||
)
|
||||
res += " [{}]".format(", ".join(platform.categories[Category(x)] for x in sub.categories))
|
||||
if platform.enable_tag:
|
||||
res += " {}".format(", ".join(sub.tags))
|
||||
res += "\n"
|
||||
@@ -62,7 +46,7 @@ def do_del_sub(del_sub: Type[Matcher]):
|
||||
try:
|
||||
index = int(index_str)
|
||||
await config.del_subscribe(user_info, **state["sub_table"][index])
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
await del_sub.reject("删除错误")
|
||||
else:
|
||||
await del_sub.finish("删除成功")
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
from typing import Type
|
||||
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Arg
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot_plugin_saa import MessageFactory, PlatformTarget
|
||||
|
||||
from ..config import config
|
||||
from ..platform import platform_manager
|
||||
from ..types import Category
|
||||
from ..utils import parse_text
|
||||
from .utils import ensure_user_info
|
||||
from ..platform import platform_manager
|
||||
|
||||
|
||||
def do_query_sub(query_sub: Type[Matcher]):
|
||||
def do_query_sub(query_sub: type[Matcher]):
|
||||
query_sub.handle()(ensure_user_info(query_sub))
|
||||
|
||||
@query_sub.handle()
|
||||
@@ -19,19 +17,10 @@ def do_query_sub(query_sub: Type[Matcher]):
|
||||
sub_list = await config.list_subscribe(user_info)
|
||||
res = "订阅的帐号为:\n"
|
||||
for sub in sub_list:
|
||||
res += "{} {} {}".format(
|
||||
# sub["target_type"], sub["target_name"], sub["target"]
|
||||
sub.target.platform_name,
|
||||
sub.target.target_name,
|
||||
sub.target.target,
|
||||
)
|
||||
res += f"{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 += " [{}]".format(", ".join(platform.categories[Category(x)] for x in sub.categories))
|
||||
if platform.enable_tag:
|
||||
res += " {}".format(", ".join(sub.tags))
|
||||
res += "\n"
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import contextlib
|
||||
from typing import Annotated, Type
|
||||
from typing import Annotated
|
||||
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Depends, EventPlainText, EventToMe
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import Rule
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot_plugin_saa import extract_target
|
||||
from nonebot.params import Depends, EventToMe, EventPlainText
|
||||
|
||||
from ..platform import platform_manager
|
||||
from ..plugin_config import plugin_config
|
||||
@@ -31,7 +31,7 @@ common_platform = [
|
||||
]
|
||||
|
||||
|
||||
def gen_handle_cancel(matcher: Type[Matcher], message: str):
|
||||
def gen_handle_cancel(matcher: type[Matcher], message: str):
|
||||
async def _handle_cancel(text: Annotated[str, EventPlainText()]):
|
||||
if text == "取消":
|
||||
await matcher.finish(message)
|
||||
@@ -39,12 +39,10 @@ def gen_handle_cancel(matcher: Type[Matcher], message: str):
|
||||
return Depends(_handle_cancel)
|
||||
|
||||
|
||||
def ensure_user_info(matcher: Type[Matcher]):
|
||||
def ensure_user_info(matcher: type[Matcher]):
|
||||
async def _check_user_info(state: T_State):
|
||||
if not state.get("target_user_info"):
|
||||
await matcher.finish(
|
||||
"No target_user_info set, this shouldn't happen, please issue"
|
||||
)
|
||||
await matcher.finish("No target_user_info set, this shouldn't happen, please issue")
|
||||
|
||||
return _check_user_info
|
||||
|
||||
|
||||
Reference in New Issue
Block a user