From c80e938a780a43a1d5f40f2976ba01e57e8ff7be Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Mon, 6 Jan 2025 20:54:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=90=86=E8=AE=BA=E4=B8=8A=E8=BF=99=E8=BF=98?= =?UTF-8?q?=E6=98=AF0.8.0=EF=BC=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/main.rs | 6 ++++-- ica-rs/src/py/class/ica.rs | 5 +++++ ica-rs/src/py/class/tailchat.rs | 4 ++++ ica-rs/src/py/config.rs | 15 ++++++++++---- ica-rs/src/py/mod.rs | 35 ++++++++++++++++++++++----------- ica-rs/src/tailchat/events.rs | 10 ---------- news.md | 8 ++++++++ 7 files changed, 55 insertions(+), 28 deletions(-) diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index b80e0d4..7395e59 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -30,7 +30,7 @@ pub type StopGetter = tokio::sync::oneshot::Receiver<()>; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const ICA_VERSION: &str = "1.6.5"; -pub const TAILCHAT_VERSION: &str = "1.2.4"; +pub const TAILCHAT_VERSION: &str = "1.2.5"; const HELP_MSG: &str = r#"/bot-rs 展示 rust 侧信息 @@ -46,7 +46,9 @@ const HELP_MSG: &str = r#"/bot-rs by shenjackyuanjie"#; /// 获取帮助信息 -pub fn help_msg() -> String { format!("{}\n{}", version_str(), HELP_MSG) } +pub fn help_msg() -> String { + format!("{}\n{}", version_str(), HELP_MSG).replace("", client_id().as_str()) +} static STARTUP_TIME: OnceLock = OnceLock::new(); diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index c256ccb..0ada6f1 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -10,6 +10,7 @@ use crate::data_struct::ica::messages::{ }; use crate::data_struct::ica::{MessageId, RoomId, RoomIdTrait, UserId}; use crate::ica::client::{delete_message, send_message, send_poke, send_room_sign_in}; +use crate::py::PyStatus; use crate::MainStatus; #[pyclass] @@ -250,6 +251,10 @@ impl IcaClientPy { #[getter] pub fn get_startup_time(&self) -> SystemTime { crate::start_up_time() } + /// 重新加载插件状态 + /// 返回是否成功 + pub fn reload_plugin_status(&self) -> bool { PyStatus::get_mut().config.reload_from_default() } + 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 e64aaff..e47d301 100644 --- a/ica-rs/src/py/class/tailchat.rs +++ b/ica-rs/src/py/class/tailchat.rs @@ -7,6 +7,7 @@ use tracing::{debug, info, warn}; use crate::data_struct::tailchat::messages::{ReceiveMessage, SendingFile, SendingMessage}; use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId}; +use crate::py::PyStatus; use crate::tailchat::client::send_message; #[pyclass] @@ -72,6 +73,9 @@ impl TailchatClientPy { pub fn get_tailchat_version(&self) -> String { crate::TAILCHAT_VERSION.to_string() } #[getter] pub fn get_startup_time(&self) -> SystemTime { crate::start_up_time() } + /// 重新加载插件状态 + /// 返回是否成功 + pub fn reload_plugin_status(&self) -> bool { PyStatus::get_mut().config.reload_from_default() } #[pyo3(signature = (content, converse_id, group_id = None))] pub fn new_message( diff --git a/ica-rs/src/py/config.rs b/ica-rs/src/py/config.rs index 6e8ca5e..2032076 100644 --- a/ica-rs/src/py/config.rs +++ b/ica-rs/src/py/config.rs @@ -49,14 +49,15 @@ impl PluginConfigFile { Self::from_config_path(&path) } - pub fn reload_from_default(&mut self) { + pub fn reload_from_default(&mut self) -> bool { let new_config = Self::default_init(); if let Err(e) = new_config { event!(Level::ERROR, "从配置文件重加载时遇到错误: {}", e); - return; + return false; } let new_config = new_config.unwrap(); self.data = new_config.data; + true } pub fn from_config_path(path: &Path) -> anyhow::Result { @@ -127,8 +128,13 @@ impl PluginConfigFile { } } - pub fn read_status_from_file(&mut self) { - self.reload_from_default(); + /// 从默认文件读取状态 + /// + /// 返回是否成功 + pub fn read_status_from_default(&mut self) -> bool { + if !self.reload_from_default() { + return false; + } event!(Level::INFO, "同步插件状态"); let plugins = PyStatus::get_mut(); plugins.files.iter_mut().for_each(|(path, status)| { @@ -144,6 +150,7 @@ impl PluginConfigFile { ); status.enabled = config_status; }); + true } pub fn sync_status_to_config(&mut self) { diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 4eb474b..77a91ee 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -3,6 +3,7 @@ pub mod class; pub mod config; use std::ffi::CString; +use std::fmt::Display; use std::path::Path; use std::sync::OnceLock; use std::time::SystemTime; @@ -11,7 +12,7 @@ use std::{collections::HashMap, path::PathBuf}; use colored::Colorize; use pyo3::prelude::*; use pyo3::types::PyTuple; -use tracing::{event, info, span, warn, Level}; +use tracing::{event, span, warn, Level}; use crate::MainStatus; @@ -74,8 +75,8 @@ impl PyStatus { "Python 插件 {{ {} }}", Self::get() .files - .iter() - .map(|(_, v)| v.to_string()) + .values() + .map(|v| v.to_string()) .collect::>() .join("\n") ) @@ -169,9 +170,9 @@ impl PyPlugin { pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) } } -impl ToString for PyPlugin { - fn to_string(&self) -> String { - format!("{}({:?})-{}", self.get_id(), self.file_path, self.enabled) +impl Display for PyPlugin { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}({:?})-{}", self.get_id(), self.file_path, self.enabled) } } @@ -200,17 +201,26 @@ impl TryFrom for PyPlugin { let mut base_path: PathBuf = PathBuf::from(base_path); if !base_path.exists() { - warn!("python 插件路径不存在, 创建: {:?}", base_path); + event!(Level::WARN, "python 插件路径不存在, 创建: {:?}", base_path); std::fs::create_dir_all(&base_path)?; } base_path.push(&config); let config_value = if base_path.exists() { - info!("加载 {:?} 的配置文件 {:?} 中", path, base_path); + event!( + Level::INFO, + "加载 {:?} 的配置文件 {:?} 中", + path, + base_path + ); let content = std::fs::read_to_string(&base_path)?; toml::from_str(&content) } else { - warn!("配置文件 {:?} 不存在, 创建默认配置", base_path); + event!( + Level::WARN, + "配置文件 {:?} 不存在, 创建默认配置", + base_path + ); // 写入默认配置 std::fs::write(base_path, &default)?; toml::from_str(&default) @@ -220,7 +230,7 @@ impl TryFrom for PyPlugin { let py_config = Bound::new(py, class::ConfigDataPy::new(config)); if let Err(e) = py_config { - warn!("添加配置文件信息失败: {:?}", e); + event!(Level::WARN, "添加配置文件信息失败: {:?}", e); return Err(e); } let py_config = py_config.unwrap(); @@ -348,7 +358,8 @@ pub fn load_py_plugins(path: &PathBuf) { } else { event!(Level::WARN, "插件加载目录不存在: {:?}", path); } - plugins.config.read_status_from_file(); + plugins.config.read_status_from_default(); + plugins.config.sync_status_to_config(); event!( Level::INFO, "python 插件目录: {:?} 加载完成, 加载到 {} 个插件", @@ -399,7 +410,7 @@ pub fn init_py() { load_py_plugins(&plugin_path); event!(Level::DEBUG, "python 插件列表: {}", PyStatus::display()); - info!("python inited") + event!(Level::INFO, "python 初始化完成") } pub fn post_py() -> anyhow::Result<()> { diff --git a/ica-rs/src/tailchat/events.rs b/ica-rs/src/tailchat/events.rs index 286c11f..efa0d6d 100644 --- a/ica-rs/src/tailchat/events.rs +++ b/ica-rs/src/tailchat/events.rs @@ -97,16 +97,6 @@ pub async fn on_message(payload: Payload, client: Client, _status: Arc format!("{:?}", d), - // Err(e) => format!("出问题啦 {:?}", e), - // }; - // let reply = message.reply_with(&format!( - // "shenbot 已运行: {}", duration - // )); - // send_message(&client, &reply).await; - // } if MainStatus::global_config().tailchat().admin_list.contains(&message.sender_id) { // admin 区 let client_id = client_id(); diff --git a/news.md b/news.md index b91f35f..40c4ec1 100644 --- a/news.md +++ b/news.md @@ -6,6 +6,7 @@ - 从 `py::PyStatus` 开始进行一个 `static mut` -> `static mut OnceLock` 的改造 - 用于看着更舒服(逃) - 部分重构了一下 读取插件启用状态 的配置文件的代码 +- 现在 `/bot-help` 会直接输出实际的 client id, 而不是给你一个默认的 `` ### ica 1.6.5 @@ -18,6 +19,13 @@ - 或者指定好友 - 目前还是有点问题 - socketio event: `sendGroupPoke` +- 添加了 `reload_plugin_status` api + - 用于重新加载插件状态 + +### tailchat 1.2.5 + +- 添加了 `reload_plugin_status` api + - 用于重新加载插件状态 ## 0.7.4