From 38cfd2dce75967c6528fbbf0651fda51c95abda8 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 19 Nov 2024 22:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=B8=80=E4=B8=AA=E8=99=9A?= =?UTF-8?q?=E5=BF=83=E6=8E=A5=E5=8F=97=E6=89=B9=E8=AF=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/main.rs | 15 ++++++++++++--- ica-rs/src/py/class/ica.rs | 2 +- ica-rs/src/py/class/tailchat.rs | 3 ++- ica-rs/src/py/mod.rs | 2 +- ica-rs/src/status.rs | 6 ------ 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 3c78e23..06ebc77 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -1,6 +1,7 @@ use std::{ hash::{DefaultHasher, Hash, Hasher}, - time::Duration, + sync::OnceLock, + time::{Duration, SystemTime}, }; mod config; @@ -21,7 +22,6 @@ 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; @@ -48,11 +48,17 @@ by shenjackyuanjie"#; /// 获取帮助信息 pub fn help_msg() -> String { format!("{}\n{}", version_str(), HELP_MSG) } +static STARTUP_TIME: OnceLock = OnceLock::new(); + +pub fn start_up_time() -> SystemTime { + *STARTUP_TIME.get().expect("WTF, why did you panic?") +} + /// 获得当前客户端的 id /// 防止串号 pub fn client_id() -> String { let mut hasher = DefaultHasher::new(); - MainStatus::get_startup_time().hash(&mut hasher); + start_up_time().hash(&mut hasher); let data = hasher.finish(); // 取后6位 format!("{:06}", data % 1_000_000) @@ -96,6 +102,9 @@ macro_rules! async_any_callback_with_state { } fn main() -> anyhow::Result<()> { + let start_up_time = SystemTime::now(); + STARTUP_TIME.set(start_up_time).expect("WTF, why did you panic?"); + tokio::runtime::Builder::new_multi_thread() .enable_all() .thread_name("shenbot-rs") diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index bfdb969..b22b6ca 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -228,7 +228,7 @@ impl IcaClientPy { #[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 get_startup_time(&self) -> SystemTime { crate::start_up_time() } pub fn debug(&self, content: String) { event!(Level::DEBUG, "{}", content); diff --git a/ica-rs/src/py/class/tailchat.rs b/ica-rs/src/py/class/tailchat.rs index 0283bd7..cf9faf7 100644 --- a/ica-rs/src/py/class/tailchat.rs +++ b/ica-rs/src/py/class/tailchat.rs @@ -71,7 +71,8 @@ impl TailchatClientPy { #[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 get_startup_time(&self) -> SystemTime { crate::start_up_time() } + #[pyo3(signature = (content, converse_id, group_id = None))] pub fn new_message( &self, diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 44011ab..56435b4 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -45,7 +45,7 @@ impl PyStatus { pub fn delete_file(path: &PathBuf) -> Option { Self::get_map_mut().remove(path) } pub fn verify_file(path: &PathBuf) -> bool { - Self::get_map().get(path).map_or(false, |plugin| plugin.verifiy()) + Self::get_map().get(path).is_some_and(|plugin| plugin.verifiy()) } pub fn get_map() -> &'static PyPlugins { diff --git a/ica-rs/src/status.rs b/ica-rs/src/status.rs index 4b96b7f..9597c00 100644 --- a/ica-rs/src/status.rs +++ b/ica-rs/src/status.rs @@ -6,7 +6,6 @@ pub struct BotStatus { pub config: Option, pub ica_status: Option, pub tailchat_status: Option, - pub startup_time: Option, } impl BotStatus { @@ -36,14 +35,9 @@ 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 { let ptr = &raw const MAIN_STATUS.config;