This commit is contained in:
2025-12-31 22:15:34 +08:00
parent 2cba589b2e
commit 082d3c9758
5 changed files with 29 additions and 6 deletions

19
base_plugins/echo.py Normal file
View File

@@ -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
})

View File

@@ -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,

View File

@@ -1,7 +1,7 @@
# main.py
import asyncio
from core import WS
import base_plugins
async def main():
bot = WS()
await bot.connect()

View File

@@ -0,0 +1 @@
#TODO 数据类型

View File

@@ -1,5 +1,5 @@
from dataclasses import dataclass
#TODO 数据类型
@dataclass
class Sender:
user_id: int