一些小的重构

This commit is contained in:
shenjack-5600u 2025-04-07 19:40:40 +08:00
parent 5d93e38237
commit 15b3e328d4
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F
9 changed files with 50 additions and 34 deletions

View File

@ -102,7 +102,6 @@ struct InnerRoom {
// pub download_path: Option<String>,
}
/// ```json
/// {
/// "comment": "问题:从哪里了解到的本群\n答案aaa",

View File

@ -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, "插件未停止")
}

View File

@ -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::<JoinRequestRoom>(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::<JoinRequestRoom>(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![

View File

@ -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);

View File

@ -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);
}
}

View File

@ -74,9 +74,7 @@ impl IcaStatusPy {
/// 获取所有管理员
///
/// 添加自 2.0.1
pub fn get_admins(&self) -> Vec<UserId> {
MainStatus::global_config().ica().admin_list.clone()
}
pub fn get_admins(&self) -> Vec<UserId> { MainStatus::global_config().ica().admin_list.clone() }
#[getter]
/// 获取所有被屏蔽的人
///
@ -86,7 +84,6 @@ impl IcaStatusPy {
pub fn get_filtered(&self) -> Vec<UserId> {
MainStatus::global_config().ica().filter_list.clone()
}
}
impl Default for IcaStatusPy {

View File

@ -27,6 +27,7 @@ pub const DEFAULT_CONFIG: &str = r#"
[plugins]
"#;
#[allow(unused)]
impl PluginConfigFile {
pub fn from_str(data: &str) -> Result<Self, TomlError> {
let mut data = DocumentMut::from_str(data)?;

21
ica-rs/src/py/func.rs Normal file
View File

@ -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";
}

View File

@ -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<RawPyPlugin> 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::<PyTuple>() {