diff --git a/base_plugins/echo.py b/base_plugins/echo.py new file mode 100644 index 0000000..5a4187c --- /dev/null +++ b/base_plugins/echo.py @@ -0,0 +1,19 @@ +from core.command_manager import matcher +#TODO 把该死的这些给抽象化 +@matcher.command("echo") +async def handle_echo(bot, event, args): + if not args: + reply_msg = "请在指令后输入要回复的内容,例如:/echo 你好" + else: + reply_msg = " ".join(args) + + if event.message_type == "group": + await bot.call_api("send_group_msg", { + "group_id": event.group_id, + "message": reply_msg + }) + else: + await bot.call_api("send_private_msg", { + "user_id": event.user_id, + "message": reply_msg + }) \ No newline at end of file diff --git a/core/ws.py b/core/ws.py index 35b2177..aa8d7c7 100644 --- a/core/ws.py +++ b/core/ws.py @@ -84,11 +84,14 @@ class WS: print(f" 事件分发失败: {e}") async def call_api(self, action: str, params: dict = None): - """公有 API:供插件调用,发送指令并异步等待结果""" - if not self.ws or self.ws.closed: - return {"status": "failed", "msg": "websocket not connected"} + if not self.ws: + return {"status": "failed", "msg": "websocket not initialized"} + + # 检查 websockets 13.x+ 的状态属性 + from websockets.protocol import State + if getattr(self.ws, "state", None) is not State.OPEN: + return {"status": "failed", "msg": "websocket is not open"} - # 创建唯一的 echo ID echo_id = str(uuid.uuid4()) payload = { "action": action, diff --git a/main.py b/main.py index 0c5cce6..76729d7 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ # main.py import asyncio from core import WS - +import base_plugins async def main(): bot = WS() await bot.connect() diff --git a/models/base.py b/models/base.py index e69de29..6440d2a 100644 --- a/models/base.py +++ b/models/base.py @@ -0,0 +1 @@ +#TODO 数据类型 \ No newline at end of file diff --git a/models/sender.py b/models/sender.py index 1d2e708..13f71cb 100644 --- a/models/sender.py +++ b/models/sender.py @@ -1,5 +1,5 @@ from dataclasses import dataclass - +#TODO 数据类型 @dataclass class Sender: user_id: int