From b5dc2426934ca74568fa80e05d74e59a97f40cd2 Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Wed, 15 Nov 2023 22:15:35 +0800 Subject: [PATCH] :sparkles: support target with bot specifier --- nonebot_plugin_saa/auto_select_bot.py | 6 +++++- nonebot_plugin_saa/registries/__init__.py | 1 + nonebot_plugin_saa/registries/platform_send_target.py | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_saa/auto_select_bot.py b/nonebot_plugin_saa/auto_select_bot.py index e29d3667..e2fb7a5e 100644 --- a/nonebot_plugin_saa/auto_select_bot.py +++ b/nonebot_plugin_saa/auto_select_bot.py @@ -4,11 +4,12 @@ import asyncio from typing import Set, Dict, List, Callable, Awaitable +import nonebot from nonebot.adapters import Bot from nonebot import logger, get_bots -from .registries import PlatformTarget, TargetQQGuildDirect from .utils import NoBotFound, SupportedAdapters, extract_adapter_type +from .registries import BotSpecifier, PlatformTarget, TargetQQGuildDirect BOT_CACHE: Dict[Bot, Set[PlatformTarget]] = {} BOT_CACHE_LOCK = asyncio.Lock() @@ -98,6 +99,9 @@ def get_bot(target: PlatformTarget) -> Bot: "\n参见:https://send-anything-anywhere.felinae98.cn/usage/send#发送时自动选择bot" ) + if isinstance(target, BotSpecifier): + return nonebot.get_bot(target.bot_id) + # TODO: 通过更方便的方式判断当前 Target 是否支持 if isinstance(target, TargetQQGuildDirect): raise NotImplementedError("暂不支持私聊") diff --git a/nonebot_plugin_saa/registries/__init__.py b/nonebot_plugin_saa/registries/__init__.py index cb35b3b5..760eb67b 100644 --- a/nonebot_plugin_saa/registries/__init__.py +++ b/nonebot_plugin_saa/registries/__init__.py @@ -5,6 +5,7 @@ from .platform_send_target import SaaTarget as SaaTarget from .platform_send_target import get_target as get_target from .platform_send_target import sender_map as sender_map +from .platform_send_target import BotSpecifier as BotSpecifier from .platform_send_target import TargetQQGroup as TargetQQGroup from .platform_send_target import PlatformTarget as PlatformTarget from .platform_send_target import extract_target as extract_target diff --git a/nonebot_plugin_saa/registries/platform_send_target.py b/nonebot_plugin_saa/registries/platform_send_target.py index 3419e161..aa519226 100644 --- a/nonebot_plugin_saa/registries/platform_send_target.py +++ b/nonebot_plugin_saa/registries/platform_send_target.py @@ -13,6 +13,7 @@ Awaitable, ) +from pydantic import BaseModel from nonebot.params import Depends from nonebot.adapters import Bot, Event @@ -42,6 +43,10 @@ def arg_dict(self, bot: Bot): return convert_to_arg_map[(self.platform_type, adapter_type)](self) +class BotSpecifier(BaseModel): + bot_id: str + + class TargetQQGroup(PlatformTarget): """QQ群