mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 08:49:55 +08:00
一些小的重构
This commit is contained in:
parent
5d93e38237
commit
15b3e328d4
|
@ -102,7 +102,6 @@ struct InnerRoom {
|
||||||
// pub download_path: Option<String>,
|
// pub download_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// ```json
|
/// ```json
|
||||||
/// {
|
/// {
|
||||||
/// "comment": "问题:从哪里了解到的本群\n答案:aaa",
|
/// "comment": "问题:从哪里了解到的本群\n答案:aaa",
|
||||||
|
|
|
@ -82,7 +82,7 @@ impl std::fmt::Display for PyPluginError {
|
||||||
}
|
}
|
||||||
PyPluginError::FuncCallError(py_err, name, module) => {
|
PyPluginError::FuncCallError(py_err, name, module) => {
|
||||||
write!(f, "插件内函数调用错误: {:#?}|{} in {}", py_err, name, module)
|
write!(f, "插件内函数调用错误: {:#?}|{} in {}", py_err, name, module)
|
||||||
},
|
}
|
||||||
PyPluginError::PluginNotStopped => {
|
PyPluginError::PluginNotStopped => {
|
||||||
write!(f, "插件未停止")
|
write!(f, "插件未停止")
|
||||||
}
|
}
|
||||||
|
|
|
@ -184,12 +184,16 @@ pub async fn join_request(payload: Payload, _client: Client) {
|
||||||
match serde_json::from_value::<JoinRequestRoom>(value.clone()) {
|
match serde_json::from_value::<JoinRequestRoom>(value.clone()) {
|
||||||
Ok(join_room) => {
|
Ok(join_room) => {
|
||||||
event!(Level::INFO, "{}", format!("收到加群申请 {:?}", join_room).on_blue());
|
event!(Level::INFO, "{}", format!("收到加群申请 {:?}", join_room).on_blue());
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
event!(Level::WARN, "呼叫 shenjack! JoinRequestRoom 的 serde 没写好! {}\nraw: {:#?}", e, value)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
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) {
|
pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
||||||
let handled = vec![
|
let handled = vec![
|
||||||
|
|
|
@ -147,8 +147,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let result = rt
|
let result = rt.block_on(inner_main());
|
||||||
.block_on(inner_main());
|
|
||||||
|
|
||||||
event!(Level::INFO, "shenbot-rs v{} exiting", VERSION);
|
event!(Level::INFO, "shenbot-rs v{} exiting", VERSION);
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use tracing::{event, info, warn, Level};
|
||||||
|
|
||||||
use crate::data_struct::{ica, tailchat};
|
use crate::data_struct::{ica, tailchat};
|
||||||
use crate::error::PyPluginError;
|
use crate::error::PyPluginError;
|
||||||
|
use crate::py::func::events_func;
|
||||||
use crate::py::{class, PyPlugin, PyStatus};
|
use crate::py::{class, PyPlugin, PyStatus};
|
||||||
use crate::MainStatus;
|
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 {
|
macro_rules! call_py_func {
|
||||||
($args:expr, $plugin:expr, $plugin_path:expr, $func_name:expr, $client:expr) => {
|
($args:expr, $plugin:expr, $plugin_path:expr, $func_name:expr, $client:expr) => {
|
||||||
tokio::spawn(async move {
|
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 msg = class::ica::NewMessagePy::new(message);
|
||||||
let client = class::ica::IcaClientPy::new(client);
|
let client = class::ica::IcaClientPy::new(client);
|
||||||
let args = (msg, 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);
|
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 msg_id = msg_id.clone();
|
||||||
let client = class::ica::IcaClientPy::new(client);
|
let client = class::ica::IcaClientPy::new(client);
|
||||||
let args = (msg_id.clone(), 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);
|
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 msg = class::tailchat::TailchatReceiveMessagePy::from_recive_message(message);
|
||||||
let client = class::tailchat::TailchatClientPy::new(client);
|
let client = class::tailchat::TailchatClientPy::new(client);
|
||||||
let args = (msg, 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);
|
PY_TASKS.lock().await.push_tailchat_new_message(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,9 +74,7 @@ impl IcaStatusPy {
|
||||||
/// 获取所有管理员
|
/// 获取所有管理员
|
||||||
///
|
///
|
||||||
/// 添加自 2.0.1
|
/// 添加自 2.0.1
|
||||||
pub fn get_admins(&self) -> Vec<UserId> {
|
pub fn get_admins(&self) -> Vec<UserId> { MainStatus::global_config().ica().admin_list.clone() }
|
||||||
MainStatus::global_config().ica().admin_list.clone()
|
|
||||||
}
|
|
||||||
#[getter]
|
#[getter]
|
||||||
/// 获取所有被屏蔽的人
|
/// 获取所有被屏蔽的人
|
||||||
///
|
///
|
||||||
|
@ -86,7 +84,6 @@ impl IcaStatusPy {
|
||||||
pub fn get_filtered(&self) -> Vec<UserId> {
|
pub fn get_filtered(&self) -> Vec<UserId> {
|
||||||
MainStatus::global_config().ica().filter_list.clone()
|
MainStatus::global_config().ica().filter_list.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for IcaStatusPy {
|
impl Default for IcaStatusPy {
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub const DEFAULT_CONFIG: &str = r#"
|
||||||
[plugins]
|
[plugins]
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
impl PluginConfigFile {
|
impl PluginConfigFile {
|
||||||
pub fn from_str(data: &str) -> Result<Self, TomlError> {
|
pub fn from_str(data: &str) -> Result<Self, TomlError> {
|
||||||
let mut data = DocumentMut::from_str(data)?;
|
let mut data = DocumentMut::from_str(data)?;
|
||||||
|
|
21
ica-rs/src/py/func.rs
Normal file
21
ica-rs/src/py/func.rs
Normal 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";
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod call;
|
pub mod call;
|
||||||
pub mod class;
|
pub mod class;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod func;
|
||||||
|
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
@ -18,8 +19,7 @@ use tracing::{event, span, warn, Level};
|
||||||
use crate::error::PyPluginError;
|
use crate::error::PyPluginError;
|
||||||
use crate::MainStatus;
|
use crate::MainStatus;
|
||||||
|
|
||||||
const REQUIRE_CONFIG_FUNC_NAME: &str = "require_config";
|
use func::config_func;
|
||||||
const ON_CONFIG_FUNC_NAME: &str = "on_config";
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PyStatus {
|
pub struct PyStatus {
|
||||||
|
@ -245,13 +245,13 @@ fn set_str_cfg_default_plugin(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 给到 on config
|
// 给到 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() {
|
if !attr.is_callable() {
|
||||||
event!(
|
event!(
|
||||||
Level::WARN,
|
Level::WARN,
|
||||||
"Python 插件 {:?} 的 {} 函数不是 Callable",
|
"Python 插件 {:?} 的 {} 函数不是 Callable",
|
||||||
path,
|
path,
|
||||||
ON_CONFIG_FUNC_NAME
|
config_func::ON_CONFIG
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -261,7 +261,7 @@ fn set_str_cfg_default_plugin(
|
||||||
Level::WARN,
|
Level::WARN,
|
||||||
"Python 插件 {:?} 的 {} 函数返回了一个报错 {}",
|
"Python 插件 {:?} 的 {} 函数返回了一个报错 {}",
|
||||||
path,
|
path,
|
||||||
ON_CONFIG_FUNC_NAME,
|
config_func::ON_CONFIG,
|
||||||
e
|
e
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -315,13 +315,13 @@ fn set_bytes_cfg_default_plugin(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 给到 on config
|
// 给到 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() {
|
if !attr.is_callable() {
|
||||||
event!(
|
event!(
|
||||||
Level::WARN,
|
Level::WARN,
|
||||||
"Python 插件 {:?} 的 {} 函数不是 Callable",
|
"Python 插件 {:?} 的 {} 函数不是 Callable",
|
||||||
path,
|
path,
|
||||||
ON_CONFIG_FUNC_NAME
|
config_func::ON_CONFIG
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,7 @@ fn set_bytes_cfg_default_plugin(
|
||||||
Level::WARN,
|
Level::WARN,
|
||||||
"Python 插件 {:?} 的 {} 函数返回了一个报错 {}",
|
"Python 插件 {:?} 的 {} 函数返回了一个报错 {}",
|
||||||
path,
|
path,
|
||||||
ON_CONFIG_FUNC_NAME,
|
config_func::ON_CONFIG,
|
||||||
e
|
e
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -352,7 +352,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
|
||||||
};
|
};
|
||||||
Python::with_gil(|py| {
|
Python::with_gil(|py| {
|
||||||
let module = py_module.bind(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() {
|
match config_func.call0() {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
if config.is_instance_of::<PyTuple>() {
|
if config.is_instance_of::<PyTuple>() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user