mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
♻️ adjust card
This commit is contained in:
parent
7ef225a87e
commit
3ccdcc844b
@ -1,12 +1,11 @@
|
||||
import contextlib
|
||||
from typing import Type, cast
|
||||
|
||||
from nonebot.adapters import Message, MessageTemplate
|
||||
from nonebot.adapters.onebot.v11 import Message as OB11Message
|
||||
from nonebot.adapters.onebot.v11.utils import unescape
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Arg, ArgPlainText
|
||||
from nonebot.typing import T_State
|
||||
from nonebot_plugin_saa import PlatformTarget
|
||||
from nonebot_plugin_saa import PlatformTarget, SupportedAdapters, Text
|
||||
|
||||
from ..apis import check_sub_target
|
||||
from ..config import config
|
||||
@ -75,13 +74,28 @@ def do_add_sub(add_sub: Type[Matcher]):
|
||||
try:
|
||||
if raw_id_text == "查询":
|
||||
url = "https://nonebot-bison.netlify.app/usage/#%E6%89%80%E6%94%AF%E6%8C%81%E5%B9%B3%E5%8F%B0%E7%9A%84-uid"
|
||||
title = "Bison所支持的平台UID"
|
||||
content = "查询相关平台的uid格式或获取方式"
|
||||
image = "https://s3.bmp.ovh/imgs/2022/03/ab3cc45d83bd3dd3.jpg"
|
||||
getId_share = f"[CQ:share,url={url},title={title},content={content},image={image}]" # 缩短字符串格式长度,以及方便后续修改为消息段格式
|
||||
await add_sub.reject(OB11Message(getId_share))
|
||||
msg = Text(url)
|
||||
with contextlib.suppress(ImportError):
|
||||
from nonebot.adapters.onebot.v11 import MessageSegment
|
||||
|
||||
title = "Bison所支持的平台UID"
|
||||
content = "查询相关平台的uid格式或获取方式"
|
||||
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
|
||||
),
|
||||
)
|
||||
await msg.reject()
|
||||
platform = platform_manager[state["platform"]]
|
||||
raw_id_text = await platform.parse_target(unescape(raw_id_text))
|
||||
with contextlib.suppress(ImportError):
|
||||
from nonebot.adapters.onebot.v11 import Message
|
||||
from nonebot.adapters.onebot.v11.utils import unescape
|
||||
|
||||
if isinstance(raw_id, Message):
|
||||
raw_id_text = unescape(raw_id_text)
|
||||
raw_id_text = await platform.parse_target(raw_id_text)
|
||||
name = await check_sub_target(state["platform"], raw_id_text)
|
||||
if not name:
|
||||
await add_sub.reject("id输入错误")
|
||||
|
@ -2,10 +2,16 @@ import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from nonebug.app import App
|
||||
from nonebug_saa import should_send_saa
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from .platforms.utils import get_json
|
||||
from .utils import BotReply, fake_admin_user, fake_group_message_event
|
||||
from .utils import (
|
||||
BotReply,
|
||||
add_reply_on_id_input_search,
|
||||
fake_admin_user,
|
||||
fake_group_message_event,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@ -335,7 +341,13 @@ async def test_platform_name_err(app: App):
|
||||
async def test_add_with_get_id(app: App):
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import Message, MessageSegment
|
||||
from nonebot_plugin_saa import TargetQQGroup
|
||||
from nonebot_plugin_saa import (
|
||||
Custom,
|
||||
MessageFactory,
|
||||
SupportedAdapters,
|
||||
TargetQQGroup,
|
||||
Text,
|
||||
)
|
||||
|
||||
from nonebot_bison.config import config
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
@ -384,19 +396,19 @@ async def test_add_with_get_id(app: App):
|
||||
message=Message("查询"), sender=fake_admin_user
|
||||
)
|
||||
ctx.receive_event(bot, event_4_query)
|
||||
ctx.should_rejected()
|
||||
ctx.should_call_send(
|
||||
event_4_query,
|
||||
Message([MessageSegment(*BotReply.add_reply_on_id_input_search())]),
|
||||
True,
|
||||
should_send_saa(
|
||||
ctx,
|
||||
MessageFactory(
|
||||
Text(add_reply_on_id_input_search).overwrite(
|
||||
SupportedAdapters.onebot_v11,
|
||||
BotReply.add_reply_on_id_input_search_ob11(),
|
||||
)
|
||||
),
|
||||
bot,
|
||||
event=event_4_query,
|
||||
)
|
||||
"""
|
||||
关于:Message([MessageSegment(*BotReply.add_reply_on_id_input_search())])
|
||||
鬼知道为什么要在这里这样写,
|
||||
没有[]的话assert不了(should_call_send使用[MessageSegment(...)]的格式进行比较)
|
||||
不在这里MessageSegment()的话也assert不了(指不能让add_reply_on_id_input_search直接返回一个MessageSegment对象)
|
||||
amen
|
||||
"""
|
||||
ctx.should_rejected()
|
||||
|
||||
event_abort = fake_group_message_event(
|
||||
message=Message("取消"), sender=Sender(card="", nickname="test", role="admin")
|
||||
)
|
||||
@ -407,6 +419,7 @@ async def test_add_with_get_id(app: App):
|
||||
True,
|
||||
)
|
||||
ctx.should_finished()
|
||||
await ctx.run()
|
||||
subs = await config.list_subscribe(TargetQQGroup(group_id=10000))
|
||||
assert len(subs) == 0
|
||||
|
||||
|
@ -75,6 +75,8 @@ from nonebot.adapters.onebot.v11.event import Sender
|
||||
fake_admin_user = Sender(nickname="test", role="admin")
|
||||
fake_superuser = Sender(user_id=10001, nickname="superuser")
|
||||
|
||||
add_reply_on_id_input_search = "https://nonebot-bison.netlify.app/usage/#%E6%89%80%E6%94%AF%E6%8C%81%E5%B9%B3%E5%8F%B0%E7%9A%84-uid"
|
||||
|
||||
|
||||
class BotReply:
|
||||
@staticmethod
|
||||
@ -102,20 +104,19 @@ class BotReply:
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def add_reply_on_id_input_search():
|
||||
search_url = "https://nonebot-bison.netlify.app/usage/#%E6%89%80%E6%94%AF%E6%8C%81%E5%B9%B3%E5%8F%B0%E7%9A%84-uid"
|
||||
def add_reply_on_id_input_search_ob11():
|
||||
from nonebot.adapters.onebot.v11 import Message, MessageSegment
|
||||
|
||||
search_title = "Bison所支持的平台UID"
|
||||
search_content = "查询相关平台的uid格式或获取方式"
|
||||
search_image = "https://s3.bmp.ovh/imgs/2022/03/ab3cc45d83bd3dd3.jpg"
|
||||
type = "share"
|
||||
data = {
|
||||
"url": search_url,
|
||||
"title": search_title,
|
||||
"content": search_content,
|
||||
"image": search_image,
|
||||
}
|
||||
msg = [type, data]
|
||||
return msg
|
||||
|
||||
return MessageSegment.share(
|
||||
url=add_reply_on_id_input_search,
|
||||
title=search_title,
|
||||
content=search_content,
|
||||
image=search_image,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def add_reply_on_target_confirm(platform, name, id):
|
||||
|
Loading…
x
Reference in New Issue
Block a user