From 83bd81f856231c92e56dcb5e520300baf115c71c Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Thu, 19 Oct 2023 19:39:08 +0800 Subject: [PATCH] :technologist: add log for auto select bot --- nonebot_plugin_saa/auto_select_bot.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_saa/auto_select_bot.py b/nonebot_plugin_saa/auto_select_bot.py index 61bb797b..e29d3667 100644 --- a/nonebot_plugin_saa/auto_select_bot.py +++ b/nonebot_plugin_saa/auto_select_bot.py @@ -1,10 +1,11 @@ """ 提供获取 Bot 的方法 """ +import json import random import asyncio from typing import Set, Dict, List, Callable, Awaitable -from nonebot import get_bots from nonebot.adapters import Bot +from nonebot import logger, get_bots from .registries import PlatformTarget, TargetQQGuildDirect from .utils import NoBotFound, SupportedAdapters, extract_adapter_type @@ -26,11 +27,13 @@ def _register_hook(): @driver.on_bot_connect async def _(bot: Bot): + logger.info(f"refresh bot platform target cache {bot}") async with BOT_CACHE_LOCK: await _refresh_bot(bot) @driver.on_bot_disconnect async def _(bot: Bot): + logger.info(f"pop bot {bot}") async with BOT_CACHE_LOCK: BOT_CACHE.pop(bot, None) @@ -72,6 +75,7 @@ async def _refresh_bot(bot: Bot): if list_targets := list_targets_map.get(adapter_name): targets = await list_targets(bot) BOT_CACHE[bot] = set(targets) + _info_current() async def refresh_bots(): @@ -103,6 +107,14 @@ def get_bot(target: PlatformTarget) -> Bot: if target in targets: bots.append(bot) if not bots: + _info_current() raise NoBotFound() return random.choice(bots) + + +def _info_current(): + log_info = {} + for bot, platform_target_set in BOT_CACHE.items(): + log_info[str(bot)] = [target.dict() for target in platform_target_set] + logger.trace(f"current bot-platform_target: {json.dumps(log_info)}")