mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 17:47:06 +08:00
feat: Enhance legacy API with message chain support and new context handling
- Updated `LegacyContext.send_message` to handle rich message chains using `send_chain`. - Introduced `LegacyStar` class for backward compatibility with legacy plugins. - Added `register` decorator for legacy plugin metadata. - Enhanced `MessageChain` class with `to_payload` and `is_plain_text_only` methods. - Updated `AstrMessageEvent.send` method to utilize `send_chain` for rich messages. - Implemented `send_chain` method in `PlatformClient` for sending complex message structures. - Added capability routing for `platform.send_chain`. - Introduced tests for new functionality, ensuring compatibility with legacy plugins and message handling.
This commit is contained in:
@@ -1,8 +1,43 @@
|
||||
"""旧版配置对象兼容类型。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
class AstrBotConfig(dict):
|
||||
"""兼容旧版 ``AstrBotConfig``。
|
||||
|
||||
旧版实现本身就是 ``dict`` 的薄封装,兼容层保持这一行为。
|
||||
旧版实现本身就是 ``dict`` 的薄封装。compat 层额外补上
|
||||
``save_config()``,以支持文档里的插件配置用法。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*args: Any,
|
||||
save_path: str | Path | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self._save_path = Path(save_path) if save_path is not None else None
|
||||
|
||||
@property
|
||||
def save_path(self) -> Path | None:
|
||||
return self._save_path
|
||||
|
||||
def bind_save_path(self, save_path: str | Path | None) -> "AstrBotConfig":
|
||||
self._save_path = Path(save_path) if save_path is not None else None
|
||||
return self
|
||||
|
||||
def save_config(self, save_path: str | Path | None = None) -> None:
|
||||
path = Path(save_path) if save_path is not None else self._save_path
|
||||
if path is None:
|
||||
raise RuntimeError("AstrBotConfig 未绑定保存路径")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(
|
||||
json.dumps(self, ensure_ascii=False, indent=2, sort_keys=True),
|
||||
encoding="utf-8",
|
||||
)
|
||||
self._save_path = path
|
||||
|
||||
Reference in New Issue
Block a user