From 8dd5c0b7d416b6ee1b628c9b77dda4afbed8e187 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 23 Jul 2024 23:27:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E7=AE=97=E5=8A=A0=E4=B8=8A=E6=88=91?= =?UTF-8?q?=E5=BF=83=E5=BF=83=E5=BF=B5=E5=BF=B5=E7=9A=84=20uptime=20?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E7=9B=AE=E5=89=8D=E8=AE=B0=E5=BD=95=E6=98=AF?= =?UTF-8?q?9d=E4=B8=8D=E5=B4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当然实际上我估计能更长, 如果不怎么更新我估计一个月都没问题(不是) --- Cargo.lock | 26 +++++++++++++------------- ica-rs/Cargo.toml | 2 +- ica-rs/ica_typing.py | 9 +++++++++ ica-rs/plugins/base.py | 11 +++++++++++ ica-rs/src/ica.rs | 2 +- ica-rs/src/ica/events.rs | 1 + ica-rs/src/main.rs | 5 +++-- ica-rs/src/py/class/ica.rs | 4 ++++ ica-rs/src/py/class/tailchat.rs | 4 ++++ ica-rs/src/status.rs | 6 ++++++ ica-rs/src/tailchat.rs | 2 +- news.md | 11 +++++++++++ 12 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05a8198..5536af4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -522,9 +522,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -1061,15 +1061,15 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.21.2" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5e00b96a521718e08e03b1a622f01c8a8deb50719335de3f60b3b3950f069d8" +checksum = "831e8e819a138c36e212f3af3fd9eeffed6bf1510a805af35b0edee5ffa59433" dependencies = [ "cfg-if", "indoc", "libc", "memoffset", - "parking_lot", + "once_cell", "portable-atomic", "pyo3-build-config", "pyo3-ffi", @@ -1079,9 +1079,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.21.2" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7883df5835fafdad87c0d888b266c8ec0f4c9ca48a5bed6bbb592e8dedee1b50" +checksum = "1e8730e591b14492a8945cdff32f089250b05f5accecf74aeddf9e8272ce1fa8" dependencies = [ "once_cell", "target-lexicon", @@ -1089,9 +1089,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.21.2" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01be5843dc60b916ab4dad1dca6d20b9b4e6ddc8e15f50c47fe6d85f1fb97403" +checksum = "5e97e919d2df92eb88ca80a037969f44e5e70356559654962cbb3316d00300c6" dependencies = [ "libc", "pyo3-build-config", @@ -1099,9 +1099,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.21.2" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77b34069fc0682e11b31dbd10321cbf94808394c56fd996796ce45217dfac53c" +checksum = "eb57983022ad41f9e683a599f2fd13c3664d7063a3ac5714cae4b7bee7d3f206" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1111,9 +1111,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.21.2" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08260721f32db5e1a5beae69a55553f56b99bd0e1c3e6e0a5e8851a9d0f5a85c" +checksum = "ec480c0c51ddec81019531705acac51bcdbeae563557c982aa8263bb96880372" dependencies = [ "heck", "proc-macro2", diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 6276fb2..8fbffab 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -41,7 +41,7 @@ colored = "2.1" # runtime tokio = { version = "1.37", features = ["full"] } futures-util = "0.3.30" -pyo3 = { version = "0.21.2", features = ["experimental-async"] } +pyo3 = { version = "0.22.2", features = ["experimental-async", "py-clone"] } anyhow = { version = "1.0", features = ["backtrace"] } # async 这玩意以后在搞 # pyo3-async = "0.3.2" diff --git a/ica-rs/ica_typing.py b/ica-rs/ica_typing.py index 8e36e30..340d1ab 100644 --- a/ica-rs/ica_typing.py +++ b/ica-rs/ica_typing.py @@ -1,6 +1,7 @@ # Python 兼容版本 3.8+ from typing import Callable, Tuple, NewType, Optional, Union +from datetime import datetime """ ica.rs @@ -161,6 +162,10 @@ class IcaClient: def version(self) -> str: ... @property + def startup_time(self) -> datetime: + """请注意, 此时刻为 UTC 时刻""" + ... + @property def ica_version(self) -> str: """shenbot ica 的版本号""" ... @@ -261,6 +266,10 @@ class TailchatClient: def tailchat_version(self) -> str: """tailchat 的版本号""" ... + @property + def startup_time(self) -> datetime: + """请注意, 此时刻为 UTC 时刻""" + ... def debug(self, message: str) -> None: """向日志中输出调试信息""" def info(self, message: str) -> None: diff --git a/ica-rs/plugins/base.py b/ica-rs/plugins/base.py index 3791cc8..3d9356f 100644 --- a/ica-rs/plugins/base.py +++ b/ica-rs/plugins/base.py @@ -1,6 +1,7 @@ import io import psutil import platform +from datetime import datetime, timezone from typing import TYPE_CHECKING, TypeVar from PIL import (Image, ImageDraw, ImageFont) @@ -57,6 +58,11 @@ def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: reply = msg.reply_with(datas) reply.set_img(local_env_image(), "image/png", False) client.send_message(reply) + elif msg.content == "/bot-uptime": + uptime = client.startup_time + up_delta = datetime.now(timezone.utc) - uptime + reply = msg.reply_with(f"Bot 运行时间: {up_delta}") + client.send_message(reply) def on_tailchat_message(msg: TailchatReciveMessage, client: TailchatClient) -> None: @@ -69,3 +75,8 @@ def on_tailchat_message(msg: TailchatReciveMessage, client: TailchatClient) -> N reply = msg.reply_with(datas) reply.set_img(local_env_image(), "just_img.png") client.send_message(reply) + elif msg.content == "/bot-uptime": + uptime = client.startup_time + up_delta = datetime.now(timezone.utc) - uptime + reply = msg.reply_with(f"Bot 运行时间: {up_delta}") + client.send_message(reply) diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index 27923ba..365939a 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -10,7 +10,7 @@ use crate::config::IcaConfig; use crate::error::{ClientResult, IcaError}; use crate::StopGetter; -const ICA_PROTOCOL_VERSION: &str = "2.12.6"; +const ICA_PROTOCOL_VERSION: &str = "2.12.9"; pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientResult<(), IcaError> { let span = span!(Level::INFO, "Icalingua Client"); diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index 36516d0..1892605 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -123,6 +123,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { "messageSuccess", "messageFailed", "setAllChatGroups", + "handleRequest", // 处理验证消息 (加入请求之类的) // 忽略的 "notify", "syncRead", // 同步已读 diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 5646409..4a6ff12 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -18,6 +18,7 @@ pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus { config: None, ica_status: None, tailchat_status: None, + startup_time: None, }; pub type MainStatus = status::BotStatus; @@ -25,8 +26,8 @@ pub type MainStatus = status::BotStatus; pub type StopGetter = tokio::sync::oneshot::Receiver<()>; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const ICA_VERSION: &str = "1.6.0"; -pub const TAILCHAT_VERSION: &str = "1.2.0"; +pub const ICA_VERSION: &str = "1.6.1"; +pub const TAILCHAT_VERSION: &str = "1.2.1"; #[macro_export] macro_rules! async_callback_with_state { diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index 32c70c2..12eb2bb 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -1,3 +1,5 @@ +use std::time::SystemTime; + use pyo3::prelude::*; use rust_socketio::asynchronous::Client; use tokio::runtime::Runtime; @@ -215,6 +217,8 @@ impl IcaClientPy { pub fn get_version(&self) -> String { crate::VERSION.to_string() } #[getter] pub fn get_ica_version(&self) -> String { crate::ICA_VERSION.to_string() } + #[getter] + pub fn get_startup_time(&self) -> SystemTime { crate::MainStatus::get_startup_time() } pub fn debug(&self, content: String) { debug!("{}", content); diff --git a/ica-rs/src/py/class/tailchat.rs b/ica-rs/src/py/class/tailchat.rs index 7e50162..4b11675 100644 --- a/ica-rs/src/py/class/tailchat.rs +++ b/ica-rs/src/py/class/tailchat.rs @@ -1,3 +1,5 @@ +use std::time::SystemTime; + use pyo3::prelude::*; use rust_socketio::asynchronous::Client; @@ -64,6 +66,8 @@ impl TailchatClientPy { pub fn get_version(&self) -> String { crate::VERSION.to_string() } #[getter] pub fn get_tailchat_version(&self) -> String { crate::TAILCHAT_VERSION.to_string() } + #[getter] + pub fn get_startup_time(&self) -> SystemTime { crate::MainStatus::get_startup_time() } pub fn debug(&self, content: String) { debug!("{}", content); } diff --git a/ica-rs/src/status.rs b/ica-rs/src/status.rs index 7893c25..b884bc0 100644 --- a/ica-rs/src/status.rs +++ b/ica-rs/src/status.rs @@ -6,6 +6,7 @@ pub struct BotStatus { pub config: Option, pub ica_status: Option, pub tailchat_status: Option, + pub startup_time: Option, } impl BotStatus { @@ -35,9 +36,14 @@ impl BotStatus { online_status: ica::OnlineData::default(), }); MAIN_STATUS.config = Some(config); + MAIN_STATUS.startup_time = Some(std::time::SystemTime::now()); } } + pub fn get_startup_time() -> std::time::SystemTime { + unsafe { MAIN_STATUS.startup_time.unwrap() } + } + pub fn global_config() -> &'static BotConfig { unsafe { MAIN_STATUS.config.as_ref().unwrap() } } pub fn global_ica_status() -> &'static ica::MainStatus { diff --git a/ica-rs/src/tailchat.rs b/ica-rs/src/tailchat.rs index ff047d0..27fcd0d 100644 --- a/ica-rs/src/tailchat.rs +++ b/ica-rs/src/tailchat.rs @@ -37,7 +37,7 @@ pub async fn start_tailchat( let client = reqwest_ClientBuilder::new().default_headers(header_map.clone()).build()?; let status = match client - .post(&format!("{}/api/openapi/bot/login", config.host)) + .post(format!("{}/api/openapi/bot/login", config.host)) .body(json! {{"appId": config.app_id, "token": token}}.to_string()) .send() .await diff --git a/news.md b/news.md index f909da9..b08d07c 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,16 @@ # 更新日志 +## 0.6.9 + +我决定立即发布 0.6.9 + +- 添加了 `Client.startup_time() -> datetime` 方法 + - 用于获取 bot 启动时间 + - 这样就可以经常吹我 bot 跑了多久了 ( ˘•ω•˘ ) + - 顺手加上了 `/bot-uptime` 命令 + - 可以获取 bot 运行时间 + - 谢谢 GitHub Copilot 的帮助 + ## 0.6.8 - 修复了一堆拼写错误