From c59757824bee8a046ab7d10d8694e84a5fb878f0 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 8 Nov 2024 22:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E6=A0=B7=E4=B8=8D=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/sr_info.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/plugins/sr_info.py b/plugins/sr_info.py index eafef63..fb1d2aa 100644 --- a/plugins/sr_info.py +++ b/plugins/sr_info.py @@ -15,6 +15,20 @@ CMD_PREFIX = "/sr" LAST_CMD = f"{CMD_PREFIX} last" LAST_SHIP_CMD = f"{CMD_PREFIX} last ship" LAST_SAVE_CMD = f"{CMD_PREFIX} last save" +INFO_CMD = f"{CMD_PREFIX} info" # info xxxxx(int) + + +def data_type_fmt(data_type: str) -> str: + if data_type == "ship": + return "飞船" + elif data_type == "save": + return "存档" + elif data_type == "none": + return "无数据" + elif data_type == "unknown": + return "未知" + else: + return f"未知类型: {data_type}" def format_data_size(data_bytes: float) -> str: @@ -42,13 +56,36 @@ def last_data(path: str) -> str: ship = data["data"] if path == "data": - data_type = "飞船" if ship["save_type"] == "ship" else "存档" - d_type_str = f"类型: {data_type}" + d_type_str = f"类型: {data_type_fmt(ship['save_type'])}" else: d_type_str = "" return f"ID: {ship['save_id']}\n{d_type_str}\n数据长度: {format_data_size(ship['len'])}\nblake3 hash: {ship['blake_hash']}" +def get_ship_info(msg: IcaNewMessage, client: IcaClient): + if len(msg.content) <= len(INFO_CMD) + 1: + client.send_message(msg.reply_with("参数不足")) + return + ship_id = msg.content[len(INFO_CMD) + 1:] + if not ship_id.isdigit(): + client.send_message(msg.reply_with("ID 必须是数字")) + return + try: + res = requests.get(f"{API_URL}/info/{ship_id}", timeout=5) + data = res.json() + except (requests.RequestException, requests.Timeout) as e: + client.send_and_warn(msg.reply_with(f"请求中出现问题: {e} {res}")) + return + + if data["code"] != 200: + client.send_and_warn(msg.reply_with(f"请求失败: {data['msg']}")) + return + + ship = data["data"] + formatted = f"ID: {ship['save_id']}\n类型: {data_type_fmt(ship['save_type'])}\n数据长度: {format_data_size(ship['len'])}\nblake3 hash: {ship['blake_hash']}" + client.send_message(msg.reply_with(formatted)) + + def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: if msg.is_from_self or not msg.is_room_msg: return @@ -62,3 +99,5 @@ def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: client.send_message(msg.reply_with(last_data("save"))) elif msg.content == LAST_CMD: client.send_message(msg.reply_with(last_data("data"))) + elif msg.content.startswith(INFO_CMD): + get_ship_info(msg, client)