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;