Skip to content

Commit

Permalink
🧑‍💻 add log for auto select bot
Browse files Browse the repository at this point in the history
  • Loading branch information
felinae98 committed Oct 19, 2023
1 parent 25c4f8e commit ce37844
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion nonebot_plugin_saa/auto_select_bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,11 +27,13 @@ def _register_hook():

@driver.on_bot_connect
async def _(bot: Bot):
logger.info(f"connect bot {bot}")
async with BOT_CACHE_LOCK:
await _refresh_bot(bot)

@driver.on_bot_disconnect
async def _(bot: Bot):
logger.info(f"pop bot {bot}")

Check warning on line 36 in nonebot_plugin_saa/auto_select_bot.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/auto_select_bot.py#L36

Added line #L36 was not covered by tests
async with BOT_CACHE_LOCK:
BOT_CACHE.pop(bot, None)

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)] = list(map(lambda x: x.dict(), platform_target_set))
logger.trace(f"current bot-platform_target: {json.dumps(log_info)}")

0 comments on commit ce37844

Please sign in to comment.