mirror of
https://github.com/Nezumi-2711/astrbot_plugin_qq_group_daily_analysis.git
synced 2026-09-22 13:38:43 +00:00
[refactor] (bot_manager) 重构,保证逻辑清晰,删除过度设计的部分
This commit is contained in:
+51
-82
@@ -3,8 +3,7 @@ Bot实例管理模块
|
||||
统一管理bot实例的获取、设置和使用
|
||||
"""
|
||||
|
||||
from typing import Optional, Dict, Any
|
||||
from astrbot.api import logger
|
||||
from typing import Dict, Any
|
||||
|
||||
class BotManager:
|
||||
"""Bot实例管理器 - 统一管理所有bot相关操作"""
|
||||
@@ -24,20 +23,17 @@ class BotManager:
|
||||
"""设置bot实例"""
|
||||
if bot_instance:
|
||||
self._bot_instance = bot_instance
|
||||
|
||||
logger.info(f"Bot实例已设置: {type(bot_instance).__name__}")
|
||||
else:
|
||||
logger.warning("尝试设置空的bot实例")
|
||||
# 自动提取QQ号
|
||||
if not self._bot_qq_id:
|
||||
bot_qq_id = self._extract_bot_qq_id(bot_instance)
|
||||
if bot_qq_id:
|
||||
self._bot_qq_id = str(bot_qq_id)
|
||||
|
||||
|
||||
def set_bot_qq_id(self, bot_qq_id: str):
|
||||
"""设置bot QQ号"""
|
||||
if bot_qq_id:
|
||||
self._bot_qq_id = str(bot_qq_id)
|
||||
logger.info(f"Bot QQ号已设置: {self._bot_qq_id}")
|
||||
|
||||
else:
|
||||
logger.warning("尝试设置空的bot QQ号")
|
||||
|
||||
|
||||
def get_bot_instance(self):
|
||||
@@ -56,69 +52,35 @@ class BotManager:
|
||||
"""检查是否准备好进行自动分析"""
|
||||
return self.has_bot_instance() and self.has_bot_qq_id()
|
||||
|
||||
def is_ready_for_manual_analysis(self) -> bool:
|
||||
"""检查是否准备好进行手动分析"""
|
||||
return self.has_bot_instance()
|
||||
|
||||
async def auto_discover_bot_instance(self) -> Optional[Any]:
|
||||
async def auto_discover_bot_instance(self):
|
||||
"""自动发现可用的bot实例"""
|
||||
try:
|
||||
if not self._context:
|
||||
logger.warning("未设置AstrBot上下文,无法自动发现bot实例")
|
||||
return None
|
||||
|
||||
# 通过platform_manager获取平台实例
|
||||
if hasattr(self._context, 'platform_manager') and hasattr(self._context.platform_manager, 'platform_insts'):
|
||||
platforms = self._context.platform_manager.platform_insts
|
||||
for platform in platforms:
|
||||
# 对于aiocqhttp适配器,bot实例在get_client()方法中
|
||||
if hasattr(platform, 'get_client'):
|
||||
bot_client = platform.get_client()
|
||||
if bot_client:
|
||||
logger.info(f"自动发现bot实例: {type(bot_client).__name__}")
|
||||
self.set_bot_instance(bot_client)
|
||||
return bot_client
|
||||
# 也检查是否直接有bot属性
|
||||
elif hasattr(platform, 'bot') and platform.bot:
|
||||
logger.info(f"自动发现bot实例: {type(platform.bot).__name__}")
|
||||
self.set_bot_instance(platform.bot)
|
||||
return platform.bot
|
||||
|
||||
logger.warning("未找到可用的bot实例")
|
||||
if not self._context or not hasattr(self._context, 'platform_manager'):
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"自动发现bot实例失败: {e}")
|
||||
return None
|
||||
|
||||
async def initialize_from_config(self) -> bool:
|
||||
"""从配置初始化bot管理器"""
|
||||
try:
|
||||
# 获取配置的bot QQ号
|
||||
bot_qq_id = self.config_manager.get_bot_qq_id()
|
||||
if bot_qq_id:
|
||||
self.set_bot_qq_id(bot_qq_id)
|
||||
else:
|
||||
logger.warning("配置中未找到bot QQ号")
|
||||
|
||||
# 自动发现bot实例
|
||||
await self.auto_discover_bot_instance()
|
||||
|
||||
self._is_initialized = True
|
||||
|
||||
if self.is_ready_for_auto_analysis():
|
||||
logger.info("Bot管理器初始化完成,可进行自动分析")
|
||||
return True
|
||||
elif self.has_bot_instance():
|
||||
logger.info("Bot管理器初始化完成,可进行手动分析")
|
||||
return True
|
||||
else:
|
||||
logger.warning("Bot管理器初始化完成,但功能受限")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Bot管理器初始化失败: {e}")
|
||||
return False
|
||||
platforms = getattr(self._context.platform_manager, 'platform_insts', [])
|
||||
for platform in platforms:
|
||||
# 获取bot实例
|
||||
bot_client = None
|
||||
if hasattr(platform, 'get_client'):
|
||||
bot_client = platform.get_client()
|
||||
elif hasattr(platform, 'bot'):
|
||||
bot_client = platform.bot
|
||||
|
||||
if bot_client:
|
||||
self.set_bot_instance(bot_client)
|
||||
return bot_client
|
||||
return None
|
||||
|
||||
async def initialize_from_config(self):
|
||||
"""从配置初始化bot管理器"""
|
||||
# 设置配置的bot QQ号
|
||||
bot_qq_id = self.config_manager.get_bot_qq_id()
|
||||
if bot_qq_id:
|
||||
self.set_bot_qq_id(bot_qq_id)
|
||||
|
||||
# 自动发现bot实例
|
||||
await self.auto_discover_bot_instance()
|
||||
self._is_initialized = True
|
||||
|
||||
def get_status_info(self) -> Dict[str, Any]:
|
||||
"""获取bot管理器状态信息"""
|
||||
@@ -126,28 +88,35 @@ class BotManager:
|
||||
"has_bot_instance": self.has_bot_instance(),
|
||||
"has_bot_qq_id": self.has_bot_qq_id(),
|
||||
"bot_qq_id": self._bot_qq_id,
|
||||
"bot_instance_type": type(self._bot_instance).__name__ if self._bot_instance else None,
|
||||
"ready_for_auto_analysis": self.is_ready_for_auto_analysis(),
|
||||
"ready_for_manual_analysis": self.is_ready_for_manual_analysis(),
|
||||
"is_initialized": self._is_initialized
|
||||
"ready_for_auto_analysis": self.is_ready_for_auto_analysis()
|
||||
}
|
||||
|
||||
def update_from_event(self, event):
|
||||
"""从事件更新bot实例(用于手动命令)"""
|
||||
if hasattr(event, 'bot') and event.bot:
|
||||
self.set_bot_instance(event.bot)
|
||||
# 如果没有QQ号,尝试使用配置的QQ号
|
||||
if not self._bot_qq_id:
|
||||
config_qq_id = self.config_manager.get_bot_qq_id()
|
||||
if config_qq_id:
|
||||
self.set_bot_qq_id(config_qq_id)
|
||||
return True
|
||||
return False
|
||||
|
||||
def _extract_bot_qq_id(self, bot_instance):
|
||||
"""从bot实例中提取QQ号"""
|
||||
# 尝试多种方式获取bot QQ号
|
||||
if hasattr(bot_instance, 'self_id') and bot_instance.self_id:
|
||||
return str(bot_instance.self_id)
|
||||
elif hasattr(bot_instance, 'qq') and bot_instance.qq:
|
||||
return str(bot_instance.qq)
|
||||
elif hasattr(bot_instance, 'user_id') and bot_instance.user_id:
|
||||
return str(bot_instance.user_id)
|
||||
return None
|
||||
|
||||
def validate_for_message_fetching(self, group_id: str) -> tuple[bool, str]:
|
||||
def validate_for_message_fetching(self, group_id: str) -> bool:
|
||||
"""验证是否可以进行消息获取"""
|
||||
if not self.has_bot_instance():
|
||||
return False, f"群 {group_id}: 没有可用的bot实例"
|
||||
|
||||
if not group_id:
|
||||
return False, "无效的群组ID"
|
||||
|
||||
return True, "验证通过"
|
||||
return self.has_bot_instance() and bool(group_id)
|
||||
|
||||
def should_filter_bot_message(self, sender_id: str) -> bool:
|
||||
"""判断是否应该过滤bot自己的消息"""
|
||||
|
||||
@@ -38,16 +38,12 @@ class MessageHandler:
|
||||
try:
|
||||
# 验证参数
|
||||
if self.bot_manager:
|
||||
is_valid, error_msg = self.bot_manager.validate_for_message_fetching(group_id)
|
||||
if not is_valid:
|
||||
logger.error(error_msg)
|
||||
if not self.bot_manager.validate_for_message_fetching(group_id):
|
||||
logger.error(f"群 {group_id} 验证失败")
|
||||
return []
|
||||
else:
|
||||
if not group_id:
|
||||
logger.error(f"群 {group_id} 无效的群组ID")
|
||||
return []
|
||||
if not bot_instance:
|
||||
logger.info(f"群 {group_id} 自动分析未获取到 bot 实例,跳过 Bot 消息获取")
|
||||
if not group_id or not bot_instance:
|
||||
logger.error(f"群 {group_id} 参数无效")
|
||||
return []
|
||||
|
||||
# 计算时间范围
|
||||
|
||||
Reference in New Issue
Block a user