修了点pyright错误喵~

This commit is contained in:
ikun-11451
2025-11-29 20:40:21 +08:00
parent 311a161941
commit f0b6a969d5
17 changed files with 194 additions and 37 deletions

View File

@@ -240,6 +240,9 @@ class ChatStream:
async def calculate_focus_energy(self) -> float:
"""异步计算focus_energy"""
if global_config is None:
raise RuntimeError("Global config is not initialized")
try:
# 使用单流上下文管理器获取消息
all_messages = self.context.get_messages(limit=global_config.chat.max_context_size)
@@ -629,6 +632,9 @@ class ChatManager:
# 回退到原始方法(最终方案)
async def _db_save_stream_async(s_data_dict: dict):
if global_config is None:
raise RuntimeError("Global config is not initialized")
async with get_db_session() as session:
user_info_d = s_data_dict.get("user_info")
group_info_d = s_data_dict.get("group_info")

View File

@@ -30,7 +30,7 @@ from __future__ import annotations
import os
import re
import traceback
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, cast
from mofox_wire import MessageEnvelope, MessageRuntime
@@ -55,6 +55,8 @@ PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
def _check_ban_words(text: str, chat: "ChatStream", userinfo) -> bool:
"""检查消息是否包含过滤词"""
if global_config is None:
return False
for word in global_config.message_receive.ban_words:
if word in text:
chat_name = chat.group_info.group_name if chat.group_info else "私聊"
@@ -62,10 +64,10 @@ def _check_ban_words(text: str, chat: "ChatStream", userinfo) -> bool:
logger.info(f"[过滤词识别]消息中含有{word}filtered")
return True
return False
def _check_ban_regex(text: str, chat: "ChatStream", userinfo) -> bool:
"""检查消息是否匹配过滤正则表达式"""
if global_config is None:
return False
for pattern in global_config.message_receive.ban_msgs_regex:
if re.search(pattern, text):
chat_name = chat.group_info.group_name if chat.group_info else "私聊"
@@ -281,8 +283,8 @@ class MessageHandler:
from src.chat.message_receive.chat_stream import get_chat_manager
chat = await get_chat_manager().get_or_create_stream(
platform=platform,
user_info=DatabaseUserInfo.from_dict(user_info) if user_info else None, # type: ignore
group_info=DatabaseGroupInfo.from_dict(group_info) if group_info else None,
user_info=DatabaseUserInfo.from_dict(cast(dict[str, Any], user_info)) if user_info else None, # type: ignore
group_info=DatabaseGroupInfo.from_dict(cast(dict[str, Any], group_info)) if group_info else None,
)
# 将消息信封转换为 DatabaseMessages
@@ -431,8 +433,8 @@ class MessageHandler:
from src.chat.message_receive.chat_stream import get_chat_manager
chat = await get_chat_manager().get_or_create_stream(
platform=platform,
user_info=DatabaseUserInfo.from_dict(user_info) if user_info else None, # type: ignore
group_info=DatabaseGroupInfo.from_dict(group_info) if group_info else None,
user_info=DatabaseUserInfo.from_dict(cast(dict[str, Any], user_info)) if user_info else None, # type: ignore
group_info=DatabaseGroupInfo.from_dict(cast(dict[str, Any], group_info)) if group_info else None,
)
# 将消息信封转换为 DatabaseMessages
@@ -536,6 +538,8 @@ class MessageHandler:
text = message.processed_plain_text or ""
# 获取配置的命令前缀
if global_config is None:
return False, None, True
prefixes = global_config.command.command_prefixes
# 检查是否以任何前缀开头
@@ -704,6 +708,9 @@ class MessageHandler:
async def _preprocess_message(self, message: DatabaseMessages, chat: "ChatStream") -> None:
"""预处理消息:存储、情绪更新等"""
try:
if global_config is None:
return
group_info = chat.group_info
# 检查是否需要处理消息

View File

@@ -256,7 +256,7 @@ async def _process_single_segment(
# 检查消息是否由机器人自己发送
user_info = message_info.get("user_info", {})
user_id_str = str(user_info.get("user_id", ""))
if user_id_str == str(global_config.bot.qq_account):
if global_config and user_id_str == str(global_config.bot.qq_account):
logger.info(f"检测到机器人自身发送的语音消息 (User ID: {user_id_str}),尝试从缓存获取文本。")
if isinstance(seg_data, str):
cached_text = consume_self_voice_text(seg_data)
@@ -299,7 +299,7 @@ async def _process_single_segment(
logger.warning("⚠️ Rust视频处理模块不可用跳过视频分析")
return "[视频]"
if global_config.video_analysis.enable:
if global_config and global_config.video_analysis.enable:
logger.info("已启用视频识别,开始识别")
if isinstance(seg_data, dict):
try:

View File

@@ -3,10 +3,11 @@ import re
import time
import traceback
from collections import deque
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, cast
import orjson
from sqlalchemy import desc, select, update
from sqlalchemy.engine import CursorResult
from src.common.data_models.database_data_model import DatabaseMessages
from src.common.database.core import get_db_session
@@ -343,7 +344,7 @@ class MessageUpdateBatcher:
.where(Messages.message_id == mmc_id)
.values(message_id=qq_id)
)
if result.rowcount > 0:
if cast(CursorResult, result).rowcount > 0:
updated_count += 1
await session.commit()
@@ -571,7 +572,7 @@ class MessageStorage:
result = await session.execute(stmt)
await session.commit()
if result.rowcount > 0:
if cast(CursorResult, result).rowcount > 0:
logger.debug(f"成功更新消息 {message_id} 的interest_value为 {interest_value}")
else:
logger.warning(f"未找到消息 {message_id}无法更新interest_value")
@@ -667,7 +668,7 @@ class MessageStorage:
)
result = await session.execute(update_stmt)
if result.rowcount > 0:
if cast(CursorResult, result).rowcount > 0:
fixed_count += 1
logger.debug(f"修复消息 {msg.message_id} 的interest_value为 {default_interest}")

View File

@@ -133,7 +133,7 @@ class HeartFCSender:
# 将发送的消息写入上下文历史
try:
if chat_stream and chat_stream.context and global_config.chat:
if chat_stream and chat_stream.context and global_config and global_config.chat:
context = chat_stream.context
chat_config = global_config.chat
if chat_config: