From 15b3e328d4e59134ba59b3f2a552b647e8ad2b32 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Mon, 7 Apr 2025 19:40:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B0=8F=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/data_struct/ica/all_rooms.rs | 1 - ica-rs/src/error.rs | 2 +- ica-rs/src/ica/events.rs | 21 ++++++++++++--------- ica-rs/src/main.rs | 3 +-- ica-rs/src/py/call.rs | 12 ++++-------- ica-rs/src/py/class/ica.rs | 5 +---- ica-rs/src/py/config.rs | 1 + ica-rs/src/py/func.rs | 21 +++++++++++++++++++++ ica-rs/src/py/mod.rs | 18 +++++++++--------- 9 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 ica-rs/src/py/func.rs diff --git a/ica-rs/src/data_struct/ica/all_rooms.rs b/ica-rs/src/data_struct/ica/all_rooms.rs index 3b0e1c3..107ea59 100644 --- a/ica-rs/src/data_struct/ica/all_rooms.rs +++ b/ica-rs/src/data_struct/ica/all_rooms.rs @@ -102,7 +102,6 @@ struct InnerRoom { // pub download_path: Option, } - /// ```json /// { /// "comment": "问题:从哪里了解到的本群\n答案:aaa", diff --git a/ica-rs/src/error.rs b/ica-rs/src/error.rs index e55d1e8..4ee813d 100644 --- a/ica-rs/src/error.rs +++ b/ica-rs/src/error.rs @@ -82,7 +82,7 @@ impl std::fmt::Display for PyPluginError { } PyPluginError::FuncCallError(py_err, name, module) => { write!(f, "插件内函数调用错误: {:#?}|{} in {}", py_err, name, module) - }, + } PyPluginError::PluginNotStopped => { write!(f, "插件未停止") } diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index 48968c5..a116118 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -181,15 +181,19 @@ pub async fn failed_message(payload: Payload, _client: Client) { pub async fn join_request(payload: Payload, _client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { - match serde_json::from_value::(value.clone()) { - Ok(join_room) => { - event!(Level::INFO, "{}", format!("收到加群申请 {:?}", join_room).on_blue()); - }, - Err(e) => { - event!(Level::WARN, "呼叫 shenjack! JoinRequestRoom 的 serde 没写好! {}\nraw: {:#?}", e, value) - }, + match serde_json::from_value::(value.clone()) { + Ok(join_room) => { + event!(Level::INFO, "{}", format!("收到加群申请 {:?}", join_room).on_blue()); } - + Err(e) => { + event!( + Level::WARN, + "呼叫 shenjack! JoinRequestRoom 的 serde 没写好! {}\nraw: {:#?}", + e, + value + ) + } + } } } } @@ -206,7 +210,6 @@ pub async fn fetch_messages(client: &Client, room: RoomId) { } } - /// 所有 pub async fn any_event(event: Event, payload: Payload, _client: Client) { let handled = vec![ diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index f6cd464..5fc4e6a 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -147,8 +147,7 @@ fn main() -> anyhow::Result<()> { .build() .unwrap(); - let result = rt - .block_on(inner_main()); + let result = rt.block_on(inner_main()); event!(Level::INFO, "shenbot-rs v{} exiting", VERSION); diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index 9d2fb70..7a80c78 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -8,6 +8,7 @@ use tracing::{event, info, warn, Level}; use crate::data_struct::{ica, tailchat}; use crate::error::PyPluginError; +use crate::py::func::events_func; use crate::py::{class, PyPlugin, PyStatus}; use crate::MainStatus; @@ -174,11 +175,6 @@ pub fn verify_and_reload_plugins() { } } -pub const ICA_NEW_MESSAGE_FUNC: &str = "on_ica_message"; -pub const ICA_DELETE_MESSAGE_FUNC: &str = "on_ica_delete_message"; - -pub const TAILCHAT_NEW_MESSAGE_FUNC: &str = "on_tailchat_message"; - macro_rules! call_py_func { ($args:expr, $plugin:expr, $plugin_path:expr, $func_name:expr, $client:expr) => { tokio::spawn(async move { @@ -224,7 +220,7 @@ pub async fn ica_new_message_py(message: &ica::messages::NewMessage, client: &Cl let msg = class::ica::NewMessagePy::new(message); let client = class::ica::IcaClientPy::new(client); let args = (msg, client); - let task = call_py_func!(args, plugin, path, ICA_NEW_MESSAGE_FUNC, client); + let task = call_py_func!(args, plugin, path, events_func::ICA_NEW_MESSAGE, client); PY_TASKS.lock().await.push_ica_new_message(task); } } @@ -237,7 +233,7 @@ pub async fn ica_delete_message_py(msg_id: ica::MessageId, client: &Client) { let msg_id = msg_id.clone(); let client = class::ica::IcaClientPy::new(client); let args = (msg_id.clone(), client); - let task = call_py_func!(args, plugin, path, ICA_DELETE_MESSAGE_FUNC, client); + let task = call_py_func!(args, plugin, path, events_func::ICA_DELETE_MESSAGE, client); PY_TASKS.lock().await.push_ica_delete_message(task); } } @@ -253,7 +249,7 @@ pub async fn tailchat_new_message_py( let msg = class::tailchat::TailchatReceiveMessagePy::from_recive_message(message); let client = class::tailchat::TailchatClientPy::new(client); let args = (msg, client); - let task = call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client); + let task = call_py_func!(args, plugin, path, events_func::TAILCHAT_NEW_MESSAGE, client); PY_TASKS.lock().await.push_tailchat_new_message(task); } } diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index db67531..cc85ab6 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -74,9 +74,7 @@ impl IcaStatusPy { /// 获取所有管理员 /// /// 添加自 2.0.1 - pub fn get_admins(&self) -> Vec { - MainStatus::global_config().ica().admin_list.clone() - } + pub fn get_admins(&self) -> Vec { MainStatus::global_config().ica().admin_list.clone() } #[getter] /// 获取所有被屏蔽的人 /// @@ -86,7 +84,6 @@ impl IcaStatusPy { pub fn get_filtered(&self) -> Vec { MainStatus::global_config().ica().filter_list.clone() } - } impl Default for IcaStatusPy { diff --git a/ica-rs/src/py/config.rs b/ica-rs/src/py/config.rs index 15a9d24..518d7ec 100644 --- a/ica-rs/src/py/config.rs +++ b/ica-rs/src/py/config.rs @@ -27,6 +27,7 @@ pub const DEFAULT_CONFIG: &str = r#" [plugins] "#; +#[allow(unused)] impl PluginConfigFile { pub fn from_str(data: &str) -> Result { let mut data = DocumentMut::from_str(data)?; diff --git a/ica-rs/src/py/func.rs b/ica-rs/src/py/func.rs new file mode 100644 index 0000000..5b28af8 --- /dev/null +++ b/ica-rs/src/py/func.rs @@ -0,0 +1,21 @@ +pub mod events_func { + + /// icalingua 的 加群请求 + /// + /// added: 2.0.1 + pub const ICA_JOIN_REQUEST: &str = "on_ica_join_request"; + /// icalingua 的 新消息 + pub const ICA_NEW_MESSAGE: &str = "on_ica_message"; + /// icalingua 的 消息撤回 + pub const ICA_DELETE_MESSAGE: &str = "on_ica_delete_message"; + + /// tailchat 的 新消息 + pub const TAILCHAT_NEW_MESSAGE: &str = "on_tailchat_message"; +} + +pub mod config_func { + /// 请求配置用的函数 + pub const REQUIRE_CONFIG: &str = "require_config"; + /// 接受配置用的函数 + pub const ON_CONFIG: &str = "on_config"; +} diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 74763c3..839db97 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -1,6 +1,7 @@ pub mod call; pub mod class; pub mod config; +pub mod func; use std::ffi::CString; use std::fmt::Display; @@ -18,8 +19,7 @@ use tracing::{event, span, warn, Level}; use crate::error::PyPluginError; use crate::MainStatus; -const REQUIRE_CONFIG_FUNC_NAME: &str = "require_config"; -const ON_CONFIG_FUNC_NAME: &str = "on_config"; +use func::config_func; #[derive(Debug, Clone)] pub struct PyStatus { @@ -245,13 +245,13 @@ fn set_str_cfg_default_plugin( } // 给到 on config - if let Ok(attr) = module.getattr(intern!(module.py(), ON_CONFIG_FUNC_NAME)) { + if let Ok(attr) = module.getattr(intern!(module.py(), config_func::ON_CONFIG)) { if !attr.is_callable() { event!( Level::WARN, "Python 插件 {:?} 的 {} 函数不是 Callable", path, - ON_CONFIG_FUNC_NAME + config_func::ON_CONFIG ); return Ok(()); } @@ -261,7 +261,7 @@ fn set_str_cfg_default_plugin( Level::WARN, "Python 插件 {:?} 的 {} 函数返回了一个报错 {}", path, - ON_CONFIG_FUNC_NAME, + config_func::ON_CONFIG, e ); } @@ -315,13 +315,13 @@ fn set_bytes_cfg_default_plugin( } // 给到 on config - if let Ok(attr) = module.getattr(intern!(module.py(), ON_CONFIG_FUNC_NAME)) { + if let Ok(attr) = module.getattr(intern!(module.py(), config_func::ON_CONFIG)) { if !attr.is_callable() { event!( Level::WARN, "Python 插件 {:?} 的 {} 函数不是 Callable", path, - ON_CONFIG_FUNC_NAME + config_func::ON_CONFIG ); return Ok(()); } @@ -331,7 +331,7 @@ fn set_bytes_cfg_default_plugin( Level::WARN, "Python 插件 {:?} 的 {} 函数返回了一个报错 {}", path, - ON_CONFIG_FUNC_NAME, + config_func::ON_CONFIG, e ); } @@ -352,7 +352,7 @@ impl TryFrom for PyPlugin { }; Python::with_gil(|py| { let module = py_module.bind(py); - if let Ok(config_func) = call::get_func(module, REQUIRE_CONFIG_FUNC_NAME) { + if let Ok(config_func) = call::get_func(module, config_func::REQUIRE_CONFIG) { match config_func.call0() { Ok(config) => { if config.is_instance_of::() {