mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 05:39:54 +08:00
test! passed!
This commit is contained in:
parent
974368166b
commit
e5fde06837
|
@ -50,3 +50,7 @@ class IcaClient:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def send_message(client: "IcaClient", message: SendMessage) -> bool:
|
async def send_message(client: "IcaClient", message: SendMessage) -> bool:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def on_message(msg: NewMessage, client: IcaClient) -> None:
|
||||||
|
...
|
||||||
|
|
|
@ -43,6 +43,8 @@ pub async fn add_message(payload: Payload, client: Client) {
|
||||||
let reply = message.reply_with(&format!("ica-async-rs pong v{}", VERSION));
|
let reply = message.reply_with(&format!("ica-async-rs pong v{}", VERSION));
|
||||||
send_message(&client, &reply).await;
|
send_message(&client, &reply).await;
|
||||||
}
|
}
|
||||||
|
// python 插件
|
||||||
|
py::new_message_py(&message, &client).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,7 @@ impl IcaStatusPy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
#[pyo3(name = "NewMessage")]
|
#[pyo3(name = "NewMessage")]
|
||||||
pub struct NewMessagePy {
|
pub struct NewMessagePy {
|
||||||
|
@ -184,3 +185,11 @@ impl IcaClientPy {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IcaClientPy {
|
||||||
|
pub fn new(client: &Client) -> Self {
|
||||||
|
Self {
|
||||||
|
client: client.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ use tracing::{debug, info, warn};
|
||||||
use crate::config::IcaConfig;
|
use crate::config::IcaConfig;
|
||||||
use crate::data_struct::messages::NewMessage;
|
use crate::data_struct::messages::NewMessage;
|
||||||
|
|
||||||
|
use self::class::{IcaClientPy, NewMessagePy};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PyStatus {
|
pub struct PyStatus {
|
||||||
pub files: Option<HashMap<PathBuf, (Option<SystemTime>, Py<PyAny>)>>,
|
pub files: Option<HashMap<PathBuf, (Option<SystemTime>, Py<PyAny>)>>,
|
||||||
|
@ -147,7 +149,60 @@ pub fn init_py(config: &IcaConfig) {
|
||||||
info!("python inited")
|
info!("python inited")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #[pyfunction]
|
||||||
|
// pub fn call_new_message(py: Python, func: PyA,msg: NewMessagePy, client: IcaClientPy) -> PyResult<&PyAny> {
|
||||||
|
// pyo3_asyncio::tokio::future_into_py(py, async move {
|
||||||
|
// let py_sleep = Python::with_gil(|py| {
|
||||||
|
// pyo3_asyncio::tokio::into_future(
|
||||||
|
// // py.import("asyncio")?.call_method1("sleep", (1,))?
|
||||||
|
// )
|
||||||
|
// })?;
|
||||||
|
|
||||||
|
// py_sleep.await?;
|
||||||
|
|
||||||
|
// Ok(Python::with_gil(|py| py.None()))
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
/// 执行 new message 的 python 插件
|
/// 执行 new message 的 python 插件
|
||||||
pub fn new_message_py(message: &NewMessage, client: &Client) {
|
pub async fn new_message_py(message: &NewMessage, client: &Client) {
|
||||||
|
let plugins = PyStatus::get_files();
|
||||||
|
for (_, (_, py_module)) in plugins.iter() {
|
||||||
|
Python::with_gil(|py| {
|
||||||
let msg = class::NewMessagePy::new(message);
|
let msg = class::NewMessagePy::new(message);
|
||||||
|
let client = class::IcaClientPy::new(client);
|
||||||
|
let args = (("message", msg), ("client", client));
|
||||||
|
// py.run("message.reply_with('')", None, Some(locals))
|
||||||
|
// .unwrap();
|
||||||
|
let async_py_func = py_module.getattr(py, "on_message");
|
||||||
|
match async_py_func {
|
||||||
|
Ok(async_py_func) => {
|
||||||
|
// pyo3_asyncio::tokio::future_into_py(py, async move {
|
||||||
|
// let call = pyo3_asyncio::tokio::into_future({
|
||||||
|
// async_py_func.call1(py, args)
|
||||||
|
// })
|
||||||
|
// .unwrap();
|
||||||
|
// Ok(call.await?)
|
||||||
|
// });
|
||||||
|
let future = pyo3_asyncio::tokio::into_future(
|
||||||
|
async_py_func.as_ref(py).call1(args).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
match future.await {
|
||||||
|
Ok(_) => {
|
||||||
|
info!("python 插件执行成功");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("python 插件执行失败: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("failed to get on_message function: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user