迁移napcat插件至built_in
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
from .config import global_config
|
||||
|
||||
__all__ = [
|
||||
"global_config",
|
||||
]
|
||||
@@ -1,26 +0,0 @@
|
||||
from maim_message import Router, RouteConfig, TargetConfig
|
||||
from .config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from .send_handler import send_handler
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
|
||||
route_config = RouteConfig(
|
||||
route_config={
|
||||
global_config.maibot_server.platform_name: TargetConfig(
|
||||
url=f"ws://{global_config.maibot_server.host}:{global_config.maibot_server.port}/ws",
|
||||
token=None,
|
||||
)
|
||||
}
|
||||
)
|
||||
router = Router(route_config)
|
||||
|
||||
|
||||
async def mmc_start_com():
|
||||
logger.info("正在连接MaiBot")
|
||||
router.register_class_handler(send_handler.handle_message)
|
||||
await router.run()
|
||||
|
||||
|
||||
async def mmc_stop_com():
|
||||
await router.stop()
|
||||
@@ -149,7 +149,7 @@ class CycleProcessor:
|
||||
logger.info(f"{self.log_prefix} 开始第{self.context.cycle_counter}次思考")
|
||||
|
||||
if ENABLE_S4U:
|
||||
await send_typing()
|
||||
await send_typing(self.context.chat_stream.user_info.user_id)
|
||||
|
||||
loop_start_time = time.time()
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ class CycleDetail:
|
||||
self.loop_action_info = loop_info["loop_action_info"]
|
||||
|
||||
|
||||
async def send_typing():
|
||||
async def send_typing(user_id):
|
||||
"""
|
||||
发送打字状态指示
|
||||
|
||||
@@ -139,6 +139,11 @@ async def send_typing():
|
||||
group_info=group_info,
|
||||
)
|
||||
|
||||
from plugin_system.core.event_manager import event_manager
|
||||
from src.plugins.built_in.napcat_adapter_plugin.event_types import NapcatEvent
|
||||
# 设置正在输入状态
|
||||
await event_manager.trigger_event(NapcatEvent.PERSONAL.SET_INPUT_STATUS,user_id=user_id,event_type=1)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="typing", stream_id=chat.stream_id, storage_message=False
|
||||
)
|
||||
|
||||
@@ -39,8 +39,9 @@ class EmojiAction(BaseAction):
|
||||
llm_judge_prompt = """
|
||||
判定是否需要使用表情动作的条件:
|
||||
1. 用户明确要求使用表情包
|
||||
2. 这是一个适合表达强烈情绪的场合
|
||||
3. 不要发送太多表情包,如果你已经发送过多个表情包则回答"否"
|
||||
2. 这是一个适合表达情绪的场合
|
||||
3. 发表情包能使当前对话更有趣
|
||||
4. 不要发送太多表情包,如果你已经发送过多个表情包则回答"否"
|
||||
|
||||
请回答"是"或"否"。
|
||||
"""
|
||||
|
||||
@@ -1746,3 +1746,32 @@ class SetGroupSignHandler(BaseEventHandler):
|
||||
else:
|
||||
logger.error("事件 napcat_set_group_sign 请求失败!")
|
||||
return HandlerResult(False, False, {"status": "error"})
|
||||
|
||||
# ===PERSONAL===
|
||||
class SetInputStatusHandler(BaseEventHandler):
|
||||
handler_name: str = "napcat_set_input_status_handler"
|
||||
handler_description: str = "设置输入状态"
|
||||
weight: int = 100
|
||||
intercept_message: bool = False
|
||||
init_subscribe = [NapcatEvent.PERSONAL.SET_INPUT_STATUS]
|
||||
|
||||
async def execute(self, params: dict):
|
||||
raw = params.get("raw", {})
|
||||
user_id = params.get("user_id", "")
|
||||
event_type = params.get("event_type", 0)
|
||||
|
||||
if params.get("raw", ""):
|
||||
user_id = raw.get("user_id", "")
|
||||
event_type = raw.get("event_type", 0)
|
||||
|
||||
if not user_id or event_type is None:
|
||||
logger.error("事件 napcat_set_input_status 缺少必要参数: user_id 或 event_type")
|
||||
return HandlerResult(False, False, {"status": "error"})
|
||||
|
||||
payload = {"user_id": str(user_id), "event_type": int(event_type)}
|
||||
response = await send_handler.send_message_to_napcat(action="set_input_status", params=payload)
|
||||
if response.get("status", "") == "ok":
|
||||
return HandlerResult(True, True, response)
|
||||
else:
|
||||
logger.error("事件 napcat_set_input_status 请求失败!")
|
||||
return HandlerResult(False, False, {"status": "error"})
|
||||
@@ -1816,3 +1816,27 @@ class NapcatEvent:
|
||||
"""
|
||||
|
||||
class FILE(Enum): ...
|
||||
|
||||
class PERSONAL(Enum):
|
||||
SET_INPUT_STATUS = "napcat_set_input_status"
|
||||
"""
|
||||
设置输入状态
|
||||
|
||||
Args:
|
||||
user_id (Optional[str|int]): 用户id(必需)
|
||||
event_type (Optional[int]): 输入状态id(必需)
|
||||
raw (Optional[dict]): 原始请求体
|
||||
|
||||
Returns:
|
||||
dict: {
|
||||
"status": "ok",
|
||||
"retcode": 0,
|
||||
"data": {
|
||||
"result": 0,
|
||||
"errMsg": "string"
|
||||
},
|
||||
"message": "string",
|
||||
"wording": "string",
|
||||
"echo": "string"
|
||||
}
|
||||
"""
|
||||
@@ -8,6 +8,7 @@ from typing import List
|
||||
|
||||
from src.plugin_system import BasePlugin, BaseEventHandler, register_plugin, EventType, ConfigField
|
||||
from src.plugin_system.core.event_manager import event_manager
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
@@ -17,7 +18,6 @@ from .src.recv_handler.meta_event_handler import meta_event_handler
|
||||
from .src.recv_handler.notice_handler import notice_handler
|
||||
from .src.recv_handler.message_sending import message_send_instance
|
||||
from .src.send_handler import send_handler
|
||||
from .src.config import global_config
|
||||
from .src.config.features_config import features_manager
|
||||
from .src.config.migrate_features import auto_migrate_features
|
||||
from .src.mmc_com_layer import mmc_start_com, router, mmc_stop_com
|
||||
@@ -134,13 +134,14 @@ async def message_process():
|
||||
logger.debug(f"清理消息队列时出错: {e}")
|
||||
|
||||
|
||||
async def napcat_server():
|
||||
async def napcat_server(plugin_config: dict):
|
||||
"""启动 Napcat WebSocket 连接(支持正向和反向连接)"""
|
||||
mode = global_config.napcat_server.mode
|
||||
# 使用插件系统配置API获取配置
|
||||
mode = config_api.get_plugin_config(plugin_config, "napcat_server.mode")
|
||||
logger.info(f"正在启动 adapter,连接模式: {mode}")
|
||||
|
||||
try:
|
||||
await websocket_manager.start_connection(message_recv)
|
||||
await websocket_manager.start_connection(message_recv, plugin_config)
|
||||
except Exception as e:
|
||||
logger.error(f"启动 WebSocket 连接失败: {e}")
|
||||
raise
|
||||
@@ -240,9 +241,18 @@ class LauchNapcatAdapterHandler(BaseEventHandler):
|
||||
logger.info("功能管理器初始化完成")
|
||||
logger.info("开始启动Napcat Adapter")
|
||||
message_send_instance.maibot_router = router
|
||||
# 设置插件配置
|
||||
message_send_instance.set_plugin_config(self.plugin_config)
|
||||
# 设置chunker的插件配置
|
||||
chunker.set_plugin_config(self.plugin_config)
|
||||
# 设置response_pool的插件配置
|
||||
from .src.response_pool import set_plugin_config as set_response_pool_config
|
||||
set_response_pool_config(self.plugin_config)
|
||||
# 设置send_handler的插件配置
|
||||
send_handler.set_plugin_config(self.plugin_config)
|
||||
# 创建单独的异步任务,防止阻塞主线程
|
||||
asyncio.create_task(napcat_server())
|
||||
asyncio.create_task(mmc_start_com())
|
||||
asyncio.create_task(napcat_server(self.plugin_config))
|
||||
asyncio.create_task(mmc_start_com(self.plugin_config))
|
||||
asyncio.create_task(message_process())
|
||||
asyncio.create_task(check_timeout_response())
|
||||
|
||||
@@ -278,9 +288,50 @@ class NapcatAdapterPlugin(BasePlugin):
|
||||
"name": ConfigField(type=str, default="napcat_adapter_plugin", description="插件名称"),
|
||||
"version": ConfigField(type=str, default="1.0.0", description="插件版本"),
|
||||
"enabled": ConfigField(type=bool, default=False, description="是否启用插件"),
|
||||
},
|
||||
"inner": {
|
||||
"version": ConfigField(type=str, default="0.2.1", description="配置版本号,请勿修改"),
|
||||
},
|
||||
"nickname": {
|
||||
"nickname": ConfigField(type=str, default="", description="昵称配置(目前未使用)"),
|
||||
},
|
||||
"napcat_server": {
|
||||
"mode": ConfigField(type=str, default="reverse", description="连接模式:reverse=反向连接(作为服务器), forward=正向连接(作为客户端)", choices=["reverse", "forward"]),
|
||||
"host": ConfigField(type=str, default="localhost", description="主机地址"),
|
||||
"port": ConfigField(type=int, default=8095, description="端口号"),
|
||||
"url": ConfigField(type=str, default="", description="正向连接时的完整WebSocket URL,如 ws://localhost:8080/ws (仅在forward模式下使用)"),
|
||||
"access_token": ConfigField(type=str, default="", description="WebSocket 连接的访问令牌,用于身份验证(可选)"),
|
||||
"heartbeat_interval": ConfigField(type=int, default=30, description="心跳间隔时间(按秒计)"),
|
||||
},
|
||||
"maibot_server": {
|
||||
"host": ConfigField(type=str, default="localhost", description="麦麦在.env文件中设置的主机地址,即HOST字段"),
|
||||
"port": ConfigField(type=int, default=8000, description="麦麦在.env文件中设置的端口,即PORT字段"),
|
||||
"platform_name": ConfigField(type=str, default="napcat", description="平台名称,用于消息路由"),
|
||||
},
|
||||
"voice": {
|
||||
"use_tts": ConfigField(type=bool, default=False, description="是否使用tts语音(请确保你配置了tts并有对应的adapter)"),
|
||||
},
|
||||
"slicing": {
|
||||
"max_frame_size": ConfigField(type=int, default=64, description="WebSocket帧的最大大小,单位为字节,默认64KB"),
|
||||
"delay_ms": ConfigField(type=int, default=10, description="切片发送间隔时间,单位为毫秒"),
|
||||
},
|
||||
"debug": {
|
||||
"level": ConfigField(type=str, default="INFO", description="日志等级(DEBUG, INFO, WARNING, ERROR, CRITICAL)", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]),
|
||||
}
|
||||
}
|
||||
|
||||
# 配置节描述
|
||||
config_section_descriptions = {
|
||||
"plugin": "插件基本信息",
|
||||
"inner": "内部配置信息(请勿修改)",
|
||||
"nickname": "昵称配置(目前未使用)",
|
||||
"napcat_server": "Napcat连接的ws服务设置",
|
||||
"maibot_server": "连接麦麦的ws服务设置",
|
||||
"voice": "发送语音设置",
|
||||
"slicing": "WebSocket消息切片设置",
|
||||
"debug": "调试设置"
|
||||
}
|
||||
|
||||
def register_events(self):
|
||||
# 注册事件
|
||||
for e in event_types.NapcatEvent.ON_RECEIVED:
|
||||
@@ -0,0 +1,2 @@
|
||||
# 配置已迁移到插件系统,此文件不再需要
|
||||
# 所有配置访问应通过插件系统的 config_api 进行
|
||||
@@ -9,7 +9,7 @@ import uuid
|
||||
import asyncio
|
||||
import time
|
||||
from typing import List, Dict, Any, Optional, Union
|
||||
from .config import global_config
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
@@ -20,7 +20,15 @@ class MessageChunker:
|
||||
"""消息切片器,用于处理大消息的分片发送"""
|
||||
|
||||
def __init__(self):
|
||||
self.max_chunk_size = global_config.slicing.max_frame_size * 1024
|
||||
self.max_chunk_size = 64 * 1024 # 默认值,将在设置配置时更新
|
||||
self.plugin_config = None
|
||||
|
||||
def set_plugin_config(self, plugin_config: dict):
|
||||
"""设置插件配置"""
|
||||
self.plugin_config = plugin_config
|
||||
if plugin_config:
|
||||
max_frame_size = config_api.get_plugin_config(plugin_config, "slicing.max_frame_size", 64)
|
||||
self.max_chunk_size = max_frame_size * 1024
|
||||
|
||||
def should_chunk_message(self, message: Union[str, Dict[str, Any]]) -> bool:
|
||||
"""判断消息是否需要切片"""
|
||||
@@ -0,0 +1,44 @@
|
||||
from maim_message import Router, RouteConfig, TargetConfig
|
||||
from src.common.logger import get_logger
|
||||
from .send_handler import send_handler
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
|
||||
router = None
|
||||
|
||||
|
||||
def create_router(plugin_config: dict):
|
||||
"""创建路由器实例"""
|
||||
global router
|
||||
platform_name = config_api.get_plugin_config(plugin_config, "maibot_server.platform_name", "napcat")
|
||||
host = config_api.get_plugin_config(plugin_config, "maibot_server.host", "localhost")
|
||||
port = config_api.get_plugin_config(plugin_config, "maibot_server.port", 8000)
|
||||
|
||||
route_config = RouteConfig(
|
||||
route_config={
|
||||
platform_name: TargetConfig(
|
||||
url=f"ws://{host}:{port}/ws",
|
||||
token=None,
|
||||
)
|
||||
}
|
||||
)
|
||||
router = Router(route_config)
|
||||
return router
|
||||
|
||||
|
||||
async def mmc_start_com(plugin_config: dict = None):
|
||||
"""启动MaiBot连接"""
|
||||
logger.info("正在连接MaiBot")
|
||||
if plugin_config:
|
||||
create_router(plugin_config)
|
||||
|
||||
if router:
|
||||
router.register_class_handler(send_handler.handle_message)
|
||||
await router.run()
|
||||
|
||||
|
||||
async def mmc_stop_com():
|
||||
"""停止MaiBot连接"""
|
||||
if router:
|
||||
await router.stop()
|
||||
@@ -5,8 +5,7 @@ from ...CONSTS import PLUGIN_NAME
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
|
||||
from ..config import global_config
|
||||
from ..config.features_config import features_manager
|
||||
from src.plugin_system.core.config_manager import config_api
|
||||
from ..message_buffer import SimpleMessageBuffer
|
||||
from ..utils import (
|
||||
get_group_info,
|
||||
@@ -90,21 +89,21 @@ class MessageHandler:
|
||||
|
||||
# 使用新的权限管理器检查权限
|
||||
if group_id:
|
||||
if not features_manager.is_group_allowed(group_id):
|
||||
if not config_api.get_plugin_config(PLUGIN_NAME, f"features.group_allowed.{group_id}", True):
|
||||
logger.warning("群聊不在聊天权限范围内,消息被丢弃")
|
||||
return False
|
||||
else:
|
||||
if not features_manager.is_private_allowed(user_id):
|
||||
if not config_api.get_plugin_config(PLUGIN_NAME, f"features.private_allowed.{user_id}", True):
|
||||
logger.warning("私聊不在聊天权限范围内,消息被丢弃")
|
||||
return False
|
||||
|
||||
# 检查全局禁止名单
|
||||
if not ignore_global_list and features_manager.is_user_banned(user_id):
|
||||
if not ignore_global_list and config_api.get_plugin_config(PLUGIN_NAME, f"features.user_banned.{user_id}", False):
|
||||
logger.warning("用户在全局黑名单中,消息被丢弃")
|
||||
return False
|
||||
|
||||
# 检查QQ官方机器人
|
||||
if features_manager.is_qq_bot_banned() and group_id and not ignore_bot:
|
||||
if config_api.get_plugin_config(PLUGIN_NAME, "features.qq_bot_banned", False) and group_id and not ignore_bot:
|
||||
logger.debug("开始判断是否为机器人")
|
||||
member_info = await get_member_info(self.get_server_connection(), group_id, user_id)
|
||||
if member_info:
|
||||
@@ -149,7 +148,7 @@ class MessageHandler:
|
||||
|
||||
# 发送者用户信息
|
||||
user_info: UserInfo = UserInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
user_id=sender_info.get("user_id"),
|
||||
user_nickname=sender_info.get("nickname"),
|
||||
user_cardname=sender_info.get("card"),
|
||||
@@ -175,7 +174,7 @@ class MessageHandler:
|
||||
nickname = fetched_member_info.get("nickname") if fetched_member_info else None
|
||||
# 发送者用户信息
|
||||
user_info: UserInfo = UserInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
user_id=sender_info.get("user_id"),
|
||||
user_nickname=nickname,
|
||||
user_cardname=None,
|
||||
@@ -192,7 +191,7 @@ class MessageHandler:
|
||||
group_name = fetched_group_info.get("group_name")
|
||||
|
||||
group_info: GroupInfo = GroupInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
group_id=raw_message.get("group_id"),
|
||||
group_name=group_name,
|
||||
)
|
||||
@@ -210,7 +209,7 @@ class MessageHandler:
|
||||
|
||||
# 发送者用户信息
|
||||
user_info: UserInfo = UserInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
user_id=sender_info.get("user_id"),
|
||||
user_nickname=sender_info.get("nickname"),
|
||||
user_cardname=sender_info.get("card"),
|
||||
@@ -223,7 +222,7 @@ class MessageHandler:
|
||||
group_name = fetched_group_info.get("group_name")
|
||||
|
||||
group_info: GroupInfo = GroupInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
group_id=raw_message.get("group_id"),
|
||||
group_name=group_name,
|
||||
)
|
||||
@@ -233,12 +232,12 @@ class MessageHandler:
|
||||
return None
|
||||
|
||||
additional_config: dict = {}
|
||||
if global_config.voice.use_tts:
|
||||
if config_api.get_plugin_config(PLUGIN_NAME, "voice.use_tts"):
|
||||
additional_config["allow_tts"] = True
|
||||
|
||||
# 消息信息
|
||||
message_info: BaseMessageInfo = BaseMessageInfo(
|
||||
platform=global_config.maibot_server.platform_name,
|
||||
platform=config_api.get_plugin_config(PLUGIN_NAME, "maibot_server.platform_name"),
|
||||
message_id=message_id,
|
||||
time=message_time,
|
||||
user_info=user_info,
|
||||
@@ -260,14 +259,14 @@ class MessageHandler:
|
||||
return None
|
||||
|
||||
# 检查是否需要使用消息缓冲
|
||||
if features_manager.is_message_buffer_enabled():
|
||||
if config_api.get_plugin_config(PLUGIN_NAME, "features.message_buffer_enabled", False):
|
||||
# 检查消息类型是否启用缓冲
|
||||
message_type = raw_message.get("message_type")
|
||||
should_use_buffer = False
|
||||
|
||||
if message_type == "group" and features_manager.is_message_buffer_group_enabled():
|
||||
if message_type == "group" and config_api.get_plugin_config(PLUGIN_NAME, "features.message_buffer_group_enabled", False):
|
||||
should_use_buffer = True
|
||||
elif message_type == "private" and features_manager.is_message_buffer_private_enabled():
|
||||
elif message_type == "private" and config_api.get_plugin_config(PLUGIN_NAME, "features.message_buffer_private_enabled", False):
|
||||
should_use_buffer = True
|
||||
|
||||
if should_use_buffer:
|
||||
@@ -2,7 +2,7 @@ import asyncio
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from ..message_chunker import chunker
|
||||
from ..config import global_config
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
from maim_message import MessageBase, Router
|
||||
@@ -14,10 +14,15 @@ class MessageSending:
|
||||
"""
|
||||
|
||||
maibot_router: Router = None
|
||||
plugin_config = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def set_plugin_config(self, plugin_config: dict):
|
||||
"""设置插件配置"""
|
||||
self.plugin_config = plugin_config
|
||||
|
||||
async def message_send(self, message_base: MessageBase) -> bool:
|
||||
"""
|
||||
发送消息(Ada -> MMC 方向,需要实现切片)
|
||||
@@ -52,9 +57,10 @@ class MessageSending:
|
||||
return False
|
||||
|
||||
# 使用配置中的延迟时间
|
||||
if i < len(chunks) - 1:
|
||||
delay_seconds = global_config.slicing.delay_ms / 1000.0
|
||||
logger.debug(f"切片发送延迟: {global_config.slicing.delay_ms}毫秒")
|
||||
if i < len(chunks) - 1 and self.plugin_config:
|
||||
delay_ms = config_api.get_plugin_config(self.plugin_config, "slicing.delay_ms", 10)
|
||||
delay_seconds = delay_ms / 1000.0
|
||||
logger.debug(f"切片发送延迟: {delay_ms}毫秒")
|
||||
await asyncio.sleep(delay_seconds)
|
||||
|
||||
logger.debug("所有切片发送完成")
|
||||
@@ -1,13 +1,20 @@
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Dict
|
||||
from .config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
|
||||
response_dict: Dict = {}
|
||||
response_time_dict: Dict = {}
|
||||
plugin_config = None
|
||||
|
||||
|
||||
def set_plugin_config(config: dict):
|
||||
"""设置插件配置"""
|
||||
global plugin_config
|
||||
plugin_config = config
|
||||
|
||||
|
||||
async def get_response(request_id: str, timeout: int = 10) -> dict:
|
||||
@@ -38,11 +45,17 @@ async def check_timeout_response() -> None:
|
||||
while True:
|
||||
cleaned_message_count: int = 0
|
||||
now_time = time.time()
|
||||
|
||||
# 获取心跳间隔配置
|
||||
heartbeat_interval = 30 # 默认值
|
||||
if plugin_config:
|
||||
heartbeat_interval = config_api.get_plugin_config(plugin_config, "napcat_server.heartbeat_interval", 30)
|
||||
|
||||
for echo_id, response_time in list(response_time_dict.items()):
|
||||
if now_time - response_time > global_config.napcat_server.heartbeat_interval:
|
||||
if now_time - response_time > heartbeat_interval:
|
||||
cleaned_message_count += 1
|
||||
response_dict.pop(echo_id)
|
||||
response_time_dict.pop(echo_id)
|
||||
logger.warning(f"响应消息 {echo_id} 超时,已删除")
|
||||
logger.info(f"已删除 {cleaned_message_count} 条超时响应消息")
|
||||
await asyncio.sleep(global_config.napcat_server.heartbeat_interval)
|
||||
await asyncio.sleep(heartbeat_interval)
|
||||
@@ -12,9 +12,9 @@ from maim_message import (
|
||||
MessageBase,
|
||||
)
|
||||
from typing import Dict, Any, Tuple, Optional
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
from . import CommandType
|
||||
from .config import global_config
|
||||
from .response_pool import get_response
|
||||
from src.common.logger import get_logger
|
||||
|
||||
@@ -28,6 +28,11 @@ from .config.features_config import features_manager
|
||||
class SendHandler:
|
||||
def __init__(self):
|
||||
self.server_connection: Optional[Server.ServerConnection] = None
|
||||
self.plugin_config = None
|
||||
|
||||
def set_plugin_config(self, plugin_config: dict):
|
||||
"""设置插件配置"""
|
||||
self.plugin_config = plugin_config
|
||||
|
||||
async def set_server_connection(self, server_connection: Server.ServerConnection) -> None:
|
||||
"""设置Napcat连接"""
|
||||
@@ -354,7 +359,11 @@ class SendHandler:
|
||||
|
||||
def handle_voice_message(self, encoded_voice: str) -> dict:
|
||||
"""处理语音消息"""
|
||||
if not global_config.voice.use_tts:
|
||||
use_tts = False
|
||||
if self.plugin_config:
|
||||
use_tts = config_api.get_plugin_config(self.plugin_config, "voice.use_tts", False)
|
||||
|
||||
if not use_tts:
|
||||
logger.warning("未启用语音消息处理")
|
||||
return {}
|
||||
if not encoded_voice:
|
||||
@@ -2,9 +2,9 @@ import asyncio
|
||||
import websockets as Server
|
||||
from typing import Optional, Callable, Any
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.apis import config_api
|
||||
|
||||
logger = get_logger("napcat_adapter")
|
||||
from .config import global_config
|
||||
|
||||
|
||||
class WebSocketManager:
|
||||
@@ -16,10 +16,12 @@ class WebSocketManager:
|
||||
self.is_running = False
|
||||
self.reconnect_interval = 5 # 重连间隔(秒)
|
||||
self.max_reconnect_attempts = 10 # 最大重连次数
|
||||
self.plugin_config = None
|
||||
|
||||
async def start_connection(self, message_handler: Callable[[Server.ServerConnection], Any]) -> None:
|
||||
async def start_connection(self, message_handler: Callable[[Server.ServerConnection], Any], plugin_config: dict) -> None:
|
||||
"""根据配置启动 WebSocket 连接"""
|
||||
mode = global_config.napcat_server.mode
|
||||
self.plugin_config = plugin_config
|
||||
mode = config_api.get_plugin_config(plugin_config, "napcat_server.mode")
|
||||
|
||||
if mode == "reverse":
|
||||
await self._start_reverse_connection(message_handler)
|
||||
@@ -30,8 +32,8 @@ class WebSocketManager:
|
||||
|
||||
async def _start_reverse_connection(self, message_handler: Callable[[Server.ServerConnection], Any]) -> None:
|
||||
"""启动反向连接(作为服务器)"""
|
||||
host = global_config.napcat_server.host
|
||||
port = global_config.napcat_server.port
|
||||
host = config_api.get_plugin_config(self.plugin_config, "napcat_server.host")
|
||||
port = config_api.get_plugin_config(self.plugin_config, "napcat_server.port")
|
||||
|
||||
logger.info(f"正在启动反向连接模式,监听地址: ws://{host}:{port}")
|
||||
|
||||
@@ -68,9 +70,10 @@ class WebSocketManager:
|
||||
connect_kwargs = {"max_size": 2**26}
|
||||
|
||||
# 如果配置了访问令牌,添加到请求头
|
||||
if global_config.napcat_server.access_token:
|
||||
access_token = config_api.get_plugin_config(self.plugin_config, "napcat_server.access_token")
|
||||
if access_token:
|
||||
connect_kwargs["additional_headers"] = {
|
||||
"Authorization": f"Bearer {global_config.napcat_server.access_token}"
|
||||
"Authorization": f"Bearer {access_token}"
|
||||
}
|
||||
logger.info("已添加访问令牌到连接请求头")
|
||||
|
||||
@@ -112,15 +115,14 @@ class WebSocketManager:
|
||||
|
||||
def _get_forward_url(self) -> str:
|
||||
"""获取正向连接的 URL"""
|
||||
config = global_config.napcat_server
|
||||
|
||||
# 如果配置了完整的 URL,直接使用
|
||||
if config.url:
|
||||
return config.url
|
||||
url = config_api.get_plugin_config(self.plugin_config, "napcat_server.url")
|
||||
if url:
|
||||
return url
|
||||
|
||||
# 否则根据 host 和 port 构建 URL
|
||||
host = config.host
|
||||
port = config.port
|
||||
host = config_api.get_plugin_config(self.plugin_config, "napcat_server.host")
|
||||
port = config_api.get_plugin_config(self.plugin_config, "napcat_server.port")
|
||||
return f"ws://{host}:{port}"
|
||||
|
||||
async def stop_connection(self) -> None:
|
||||
Reference in New Issue
Block a user