理论上这还是0.8.0?

This commit is contained in:
shenjack 2025-01-06 20:54:05 +08:00
parent 32958031d2
commit c80e938a78
Signed by: shenjack
GPG Key ID: 7B1134A979775551
7 changed files with 55 additions and 28 deletions

View File

@ -30,7 +30,7 @@ pub type StopGetter = tokio::sync::oneshot::Receiver<()>;
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ICA_VERSION: &str = "1.6.5"; 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 const HELP_MSG: &str = r#"/bot-rs
rust rust
@ -46,7 +46,9 @@ const HELP_MSG: &str = r#"/bot-rs
by shenjackyuanjie"#; 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>", client_id().as_str())
}
static STARTUP_TIME: OnceLock<SystemTime> = OnceLock::new(); static STARTUP_TIME: OnceLock<SystemTime> = OnceLock::new();

View File

@ -10,6 +10,7 @@ use crate::data_struct::ica::messages::{
}; };
use crate::data_struct::ica::{MessageId, RoomId, RoomIdTrait, UserId}; 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::ica::client::{delete_message, send_message, send_poke, send_room_sign_in};
use crate::py::PyStatus;
use crate::MainStatus; use crate::MainStatus;
#[pyclass] #[pyclass]
@ -250,6 +251,10 @@ impl IcaClientPy {
#[getter] #[getter]
pub fn get_startup_time(&self) -> SystemTime { crate::start_up_time() } 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) { pub fn debug(&self, content: String) {
event!(Level::DEBUG, "{}", content); event!(Level::DEBUG, "{}", content);
} }

View File

@ -7,6 +7,7 @@ use tracing::{debug, info, warn};
use crate::data_struct::tailchat::messages::{ReceiveMessage, SendingFile, SendingMessage}; use crate::data_struct::tailchat::messages::{ReceiveMessage, SendingFile, SendingMessage};
use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId}; use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
use crate::py::PyStatus;
use crate::tailchat::client::send_message; use crate::tailchat::client::send_message;
#[pyclass] #[pyclass]
@ -72,6 +73,9 @@ impl TailchatClientPy {
pub fn get_tailchat_version(&self) -> String { crate::TAILCHAT_VERSION.to_string() } pub fn get_tailchat_version(&self) -> String { crate::TAILCHAT_VERSION.to_string() }
#[getter] #[getter]
pub fn get_startup_time(&self) -> SystemTime { crate::start_up_time() } 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))] #[pyo3(signature = (content, converse_id, group_id = None))]
pub fn new_message( pub fn new_message(

View File

@ -49,14 +49,15 @@ impl PluginConfigFile {
Self::from_config_path(&path) 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(); let new_config = Self::default_init();
if let Err(e) = new_config { if let Err(e) = new_config {
event!(Level::ERROR, "从配置文件重加载时遇到错误: {}", e); event!(Level::ERROR, "从配置文件重加载时遇到错误: {}", e);
return; return false;
} }
let new_config = new_config.unwrap(); let new_config = new_config.unwrap();
self.data = new_config.data; self.data = new_config.data;
true
} }
pub fn from_config_path(path: &Path) -> anyhow::Result<Self> { pub fn from_config_path(path: &Path) -> anyhow::Result<Self> {
@ -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, "同步插件状态"); event!(Level::INFO, "同步插件状态");
let plugins = PyStatus::get_mut(); let plugins = PyStatus::get_mut();
plugins.files.iter_mut().for_each(|(path, status)| { plugins.files.iter_mut().for_each(|(path, status)| {
@ -144,6 +150,7 @@ impl PluginConfigFile {
); );
status.enabled = config_status; status.enabled = config_status;
}); });
true
} }
pub fn sync_status_to_config(&mut self) { pub fn sync_status_to_config(&mut self) {

View File

@ -3,6 +3,7 @@ pub mod class;
pub mod config; pub mod config;
use std::ffi::CString; use std::ffi::CString;
use std::fmt::Display;
use std::path::Path; use std::path::Path;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::time::SystemTime; use std::time::SystemTime;
@ -11,7 +12,7 @@ use std::{collections::HashMap, path::PathBuf};
use colored::Colorize; use colored::Colorize;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyTuple; use pyo3::types::PyTuple;
use tracing::{event, info, span, warn, Level}; use tracing::{event, span, warn, Level};
use crate::MainStatus; use crate::MainStatus;
@ -74,8 +75,8 @@ impl PyStatus {
"Python 插件 {{ {} }}", "Python 插件 {{ {} }}",
Self::get() Self::get()
.files .files
.iter() .values()
.map(|(_, v)| v.to_string()) .map(|v| v.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n") .join("\n")
) )
@ -169,9 +170,9 @@ impl PyPlugin {
pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) } pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) }
} }
impl ToString for PyPlugin { impl Display for PyPlugin {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format!("{}({:?})-{}", self.get_id(), self.file_path, self.enabled) write!(f, "{}({:?})-{}", self.get_id(), self.file_path, self.enabled)
} }
} }
@ -200,17 +201,26 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
let mut base_path: PathBuf = PathBuf::from(base_path); let mut base_path: PathBuf = PathBuf::from(base_path);
if !base_path.exists() { if !base_path.exists() {
warn!("python 插件路径不存在, 创建: {:?}", base_path); event!(Level::WARN, "python 插件路径不存在, 创建: {:?}", base_path);
std::fs::create_dir_all(&base_path)?; std::fs::create_dir_all(&base_path)?;
} }
base_path.push(&config); base_path.push(&config);
let config_value = if base_path.exists() { 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)?; let content = std::fs::read_to_string(&base_path)?;
toml::from_str(&content) toml::from_str(&content)
} else { } else {
warn!("配置文件 {:?} 不存在, 创建默认配置", base_path); event!(
Level::WARN,
"配置文件 {:?} 不存在, 创建默认配置",
base_path
);
// 写入默认配置 // 写入默认配置
std::fs::write(base_path, &default)?; std::fs::write(base_path, &default)?;
toml::from_str(&default) toml::from_str(&default)
@ -220,7 +230,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
let py_config = let py_config =
Bound::new(py, class::ConfigDataPy::new(config)); Bound::new(py, class::ConfigDataPy::new(config));
if let Err(e) = py_config { if let Err(e) = py_config {
warn!("添加配置文件信息失败: {:?}", e); event!(Level::WARN, "添加配置文件信息失败: {:?}", e);
return Err(e); return Err(e);
} }
let py_config = py_config.unwrap(); let py_config = py_config.unwrap();
@ -348,7 +358,8 @@ pub fn load_py_plugins(path: &PathBuf) {
} else { } else {
event!(Level::WARN, "插件加载目录不存在: {:?}", path); event!(Level::WARN, "插件加载目录不存在: {:?}", path);
} }
plugins.config.read_status_from_file(); plugins.config.read_status_from_default();
plugins.config.sync_status_to_config();
event!( event!(
Level::INFO, Level::INFO,
"python 插件目录: {:?} 加载完成, 加载到 {} 个插件", "python 插件目录: {:?} 加载完成, 加载到 {} 个插件",
@ -399,7 +410,7 @@ pub fn init_py() {
load_py_plugins(&plugin_path); load_py_plugins(&plugin_path);
event!(Level::DEBUG, "python 插件列表: {}", PyStatus::display()); event!(Level::DEBUG, "python 插件列表: {}", PyStatus::display());
info!("python inited") event!(Level::INFO, "python 初始化完成")
} }
pub fn post_py() -> anyhow::Result<()> { pub fn post_py() -> anyhow::Result<()> {

View File

@ -97,16 +97,6 @@ pub async fn on_message(payload: Payload, client: Client, _status: Arc<BotStatus
let reply = message.reply_with(&help_msg()); let reply = message.reply_with(&help_msg());
send_message(&client, &reply).await; send_message(&client, &reply).await;
} }
// else if message.content == "/bot-uptime" {
// let duration = match start_up_time().elapsed() {
// Ok(d) => 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) { if MainStatus::global_config().tailchat().admin_list.contains(&message.sender_id) {
// admin 区 // admin 区
let client_id = client_id(); let client_id = client_id();

View File

@ -6,6 +6,7 @@
- 从 `py::PyStatus` 开始进行一个 `static mut` -> `static mut OnceLock` 的改造 - 从 `py::PyStatus` 开始进行一个 `static mut` -> `static mut OnceLock` 的改造
- 用于看着更舒服(逃) - 用于看着更舒服(逃)
- 部分重构了一下 读取插件启用状态 的配置文件的代码 - 部分重构了一下 读取插件启用状态 的配置文件的代码
- 现在 `/bot-help` 会直接输出实际的 client id, 而不是给你一个默认的 `<client-id>`
### ica 1.6.5 ### ica 1.6.5
@ -18,6 +19,13 @@
- 或者指定好友 - 或者指定好友
- 目前还是有点问题 - 目前还是有点问题
- socketio event: `sendGroupPoke` - socketio event: `sendGroupPoke`
- 添加了 `reload_plugin_status` api
- 用于重新加载插件状态
### tailchat 1.2.5
- 添加了 `reload_plugin_status` api
- 用于重新加载插件状态
## 0.7.4 ## 0.7.4