diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index bc60efc..9a7e897 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.4.5" +version = "0.4.6" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/src/data_struct/messages.rs b/ica-rs/src/data_struct/messages.rs index 7375604..fd320e9 100644 --- a/ica-rs/src/data_struct/messages.rs +++ b/ica-rs/src/data_struct/messages.rs @@ -194,6 +194,13 @@ impl NewMessage { } } + pub fn output(&self) -> String { + format!( + "Room: {}, Sender: {}|{}, Content: {}", + self.room_id, self.sender_id, self.sender_name, self.content + ) + } + /// 作为回复消息使用 pub fn as_reply(&self) -> ReplyMessage { ReplyMessage { diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index 834169d..1f6eb60 100644 --- a/ica-rs/src/events.rs +++ b/ica-rs/src/events.rs @@ -30,13 +30,7 @@ pub async fn add_message(payload: Payload, client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { let message = NewMessage::new_from_json(value); - info!("add_message {}", format!("{:#?}", message).cyan()); - // if message.is_reply() { - // return; - // } - // if message.is_from_self() { - // return; - // } + info!("add_message {}", message.output().cyan()); // 就在这里处理掉最基本的消息 // 之后的处理交给插件 if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 8699213..0d400fc 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -7,6 +7,7 @@ use pyo3::prelude::*; use rust_socketio::asynchronous::Client; use tracing::{debug, info, warn}; +use crate::client::IcalinguaStatus; use crate::config::IcaConfig; use crate::data_struct::messages::NewMessage; @@ -21,7 +22,6 @@ impl PyStatus { match PYSTATUS.files.as_ref() { Some(files) => files, None => { - debug!("No files in py status"); PYSTATUS.files = Some(HashMap::new()); PYSTATUS.files.as_ref().unwrap() } @@ -34,10 +34,8 @@ impl PyStatus { match PYSTATUS.files.as_mut() { Some(files) => { files.insert(path, (changed_time, py_module)); - debug!("Added file to py status, {:?}", files); } None => { - warn!("No files in py status, creating new"); let mut files = HashMap::new(); files.insert(path, (changed_time, py_module)); PYSTATUS.files = Some(files); @@ -127,28 +125,47 @@ pub fn load_py_plugins(path: &PathBuf) { } pub fn verify_plugins() { - let plugins = PyStatus::get_files(); - for (path, _) in plugins.iter() { - if !PyStatus::verify_file(path) { - info!("file changed: {:?}", path); - if let Ok((changed_time, content)) = load_py_file(path) { + let mut need_reload_files: Vec = Vec::new(); + let plugin_path = IcalinguaStatus::get_config().py_plugin_path.as_ref().unwrap().to_owned(); + for entry in std::fs::read_dir(&plugin_path).unwrap() { + if let Ok(entry) = entry { + let path = entry.path(); + if let Some(ext) = path.extension() { + if ext == "py" { + if !PyStatus::verify_file(&path) { + need_reload_files.push(path); + } + } + } + } + } + + if need_reload_files.is_empty() { + return; + } + info!("file change list: {:?}", need_reload_files); + for reload_file in need_reload_files { + match load_py_file(&reload_file) { + Ok((changed_time, content)) => { let py_module = Python::with_gil(|py| -> Py { let module: Py = PyModule::from_code( py, &content, - &path.to_string_lossy(), - &path.to_string_lossy(), + &reload_file.to_string_lossy(), + &reload_file.to_string_lossy(), // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 ) .unwrap() .into(); module }); - PyStatus::add_file(path.clone(), changed_time, py_module); + PyStatus::add_file(reload_file.clone(), changed_time, py_module); + }, + Err(e) => { + warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e); } } } - } pub fn get_change_time(path: &PathBuf) -> Option { diff --git a/news.md b/news.md index e03c690..14af412 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,9 @@ # 更新日志 +## 0.4.6 + +现在更适合部署了 + ## 0.4.5 添加 `is_reply` api 到 `NewMessagePy`