From 113a1518d1469de96d490c8064b3cba2b338cdf0 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 18 Aug 2024 02:05:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=86=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- ica-rs/Cargo.toml | 2 +- ica-rs/src/ica/events.rs | 51 +++++++++++++++++------------------ ica-rs/src/main.rs | 5 ++-- ica-rs/src/py/mod.rs | 16 ++++++++--- ica-rs/src/tailchat/events.rs | 50 +++++++++++++++++++++++++++++++++- news.md | 15 ++++++++--- 7 files changed, 103 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 319bcec..9c44947 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -659,7 +659,7 @@ dependencies = [ [[package]] name = "ica-rs" -version = "0.6.11" +version = "0.7.0" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 717b699..b86af18 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.6.11" +version = "0.7.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index 124e248..6d8f5f4 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -9,7 +9,7 @@ use crate::data_struct::ica::all_rooms::Room; use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage}; use crate::data_struct::ica::online_data::OnlineData; use crate::ica::client::send_message; -use crate::{py, MainStatus, ICA_VERSION, VERSION}; +use crate::{py, version_str, MainStatus, VERSION}; /// 获取在线数据 pub async fn get_online_data(payload: Payload, _client: Client) { @@ -22,7 +22,7 @@ pub async fn get_online_data(payload: Payload, _client: Client) { } } -#[allow(clippy::collapsible_if)] +// #[allow(clippy::collapsible_if)] /// 接收消息 pub async fn add_message(payload: Payload, client: Client) { if let Payload::Text(values) = payload { @@ -38,10 +38,7 @@ pub async fn add_message(payload: Payload, client: Client) { // 之后的处理交给插件 if !message.is_from_self() && !message.is_reply() { if message.content() == "/bot-rs" { - let reply = message.reply_with(&format!( - "shenbot v{}\nica-async-rs pong v{}", - VERSION, ICA_VERSION - )); + let reply = message.reply_with(&version_str()); send_message(&client, &reply).await; } else if message.content() == "/bot-ls" { let reply = message.reply_with(&format!( @@ -64,19 +61,20 @@ pub async fn add_message(payload: Payload, client: Client) { content.next(); if let Some(name) = content.next() { let path_name = PathBuf::from(name); - if py::PyStatus::get_map().contains_key(&path_name) { - if py::PyStatus::get_config().get_status(path_name.as_path()) { + match py::PyStatus::get_status(&path_name) { + None => { + let reply = message.reply_with("未找到插件"); + send_message(&client, &reply).await; + } + Some(true) => { let reply = message.reply_with("无变化, 插件已经启用"); send_message(&client, &reply).await; - return; } - py::PyStatus::get_config_mut() - .set_status(path_name.as_path(), true); - let reply = message.reply_with("启用插件完成"); - send_message(&client, &reply).await; - } else { - let reply = message.reply_with("未找到插件"); - send_message(&client, &reply).await; + Some(false) => { + py::PyStatus::set_status(&path_name, true); + let reply = message.reply_with("启用插件完成"); + send_message(&client, &reply).await; + } } } } else if message.content().starts_with("/bot-disable") { @@ -84,19 +82,20 @@ pub async fn add_message(payload: Payload, client: Client) { content.next(); if let Some(name) = content.next() { let path_name = PathBuf::from(name); - if py::PyStatus::get_map().contains_key(&path_name) { - if !py::PyStatus::get_config().get_status(path_name.as_path()) { + match py::PyStatus::get_status(&path_name) { + None => { + let reply = message.reply_with("未找到插件"); + send_message(&client, &reply).await; + } + Some(false) => { let reply = message.reply_with("无变化, 插件已经禁用"); send_message(&client, &reply).await; - return; } - py::PyStatus::get_config_mut() - .set_status(path_name.as_path(), false); - let reply = message.reply_with("已经禁用插件"); - send_message(&client, &reply).await; - } else { - let reply = message.reply_with("未找到插件"); - send_message(&client, &reply).await; + Some(true) => { + py::PyStatus::set_status(&path_name, false); + let reply = message.reply_with("禁用插件完成"); + send_message(&client, &reply).await; + } } } } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 444b0a3..7112425 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -31,11 +31,12 @@ pub const TAILCHAT_VERSION: &str = "1.2.1"; pub fn version_str() -> String { format!( - "shenbot-rs v{}-{} ica v{} tailchat v{}", + "shenbot-rs v{}-{} ica v{}({}) tailchat v{}", VERSION, if STABLE { "" } else { "开发版" }, ICA_VERSION, - TAILCHAT_VERSION + ica::ICA_PROTOCOL_VERSION, + TAILCHAT_VERSION, ) } diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 341bd91..e889a1c 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -95,17 +95,25 @@ impl PyStatus { /// 获取某个插件的状态 /// 以 config 优先 - pub fn get_status(path: &PathBuf) -> Option { - let local_plugin = Self::get_map_mut().get_mut(path).map(|p| p.enabled)?; + pub fn get_status(path: &PathBuf) -> Option { + Self::get_config_mut().sync_status_from_config(); + Self::get_map().get(path).map(|plugin| plugin.enabled) } - pub fn set_status(path: &Path, status: bool) { Self::get_config_mut().set_status(path, status); } + pub fn set_status(path: &Path, status: bool) { + let cfg = Self::get_config_mut(); + cfg.set_status(path, status); + cfg.sync_status_from_config(); + } pub fn display() -> String { let map = Self::get_map(); format!( "Python 插件 {{ {} }}", - map.iter().map(|(k, v)| format!("{:?}-{}", k, v.enabled)).collect::>().join("\n") + map.iter() + .map(|(k, v)| format!("{:?}-{}", k, v.enabled)) + .collect::>() + .join("\n") ) } } diff --git a/ica-rs/src/tailchat/events.rs b/ica-rs/src/tailchat/events.rs index 87b4cfd..3f2cb66 100644 --- a/ica-rs/src/tailchat/events.rs +++ b/ica-rs/src/tailchat/events.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::sync::Arc; use colored::Colorize; @@ -62,7 +63,6 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client, _status: } } -#[allow(clippy::collapsible_if)] pub async fn on_message(payload: Payload, client: Client, _status: Arc) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { @@ -95,6 +95,54 @@ pub async fn on_message(payload: Payload, client: Client, _status: Arc { + let reply = message.reply_with("未找到插件"); + send_message(&client, &reply).await; + } + Some(true) => { + let reply = message.reply_with("无变化, 插件已经启用"); + send_message(&client, &reply).await; + } + Some(false) => { + py::PyStatus::set_status(&path_name, true); + let reply = message.reply_with("启用插件完成"); + send_message(&client, &reply).await; + } + } + } + } else if message.content.starts_with("/bot-disable") { + let mut content = message.content.split_whitespace(); + content.next(); + if let Some(name) = content.next() { + let path_name = PathBuf::from(name); + match py::PyStatus::get_status(&path_name) { + None => { + let reply = message.reply_with("未找到插件"); + send_message(&client, &reply).await; + } + Some(false) => { + let reply = message.reply_with("无变化, 插件已经禁用"); + send_message(&client, &reply).await; + } + Some(true) => { + py::PyStatus::set_status(&path_name, false); + let reply = message.reply_with("禁用插件完成"); + send_message(&client, &reply).await; + } + } + } + } + } } py::call::tailchat_new_message_py(&message, &client).await; } diff --git a/news.md b/news.md index bb22e0c..0be72d3 100644 --- a/news.md +++ b/news.md @@ -1,6 +1,9 @@ # 更新日志 -## 0.6.11 +## 0.7.0 + +> 我决定叫他 0.7.0 +> 因为修改太多了.png - 加入了 禁用/启用 插件功能 - 现在会在插件加载时警告你的插件原来定义了 `CONFIG_DATA` 这一项 @@ -8,8 +11,14 @@ - `get_sender_name` 获取发送人昵称 - `ica` 兼容版本号 `2.12.11` -> `2.12.12` - 加入了 `STABLE` 信息, 用于标记稳定版本 -- 去掉了 `enable_py` 选项 - - 原来这玩意压根没读取过 +- 添加了 `version_str() -> String` 用于方便的获取版本信息 + - 同样在 `py` 侧也有 `version_str` 方法 +- 加入了 `/help` 命令 + - 用于获取帮助信息 +- 加入了 `/bot-ls` + - 用于展示所有插件的信息 +- 加入了 `/bot-enable` 和 `/bot-disable` + - 用于启用/禁用插件 ## 0.6.10