Skip to content

Commit

Permalink
✨ support target with bot specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
felinae98 committed Nov 16, 2023
1 parent cf926e7 commit b5dc242
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nonebot_plugin_saa/auto_select_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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("暂不支持私聊")
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_saa/registries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions nonebot_plugin_saa/registries/platform_send_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Awaitable,
)

from pydantic import BaseModel
from nonebot.params import Depends
from nonebot.adapters import Bot, Event

Expand Down Expand Up @@ -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群
Expand Down

1 comment on commit b5dc242

@Dreamail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def extract_target(event: Event) -> PlatformTarget:

这个函数还是只有一个参数,adapter-qq获取target的时候会导致参数不足报错,而其他adapter的target没有这个bot_id字段

Please sign in to comment.