Compare commits

...

5 Commits

Author SHA1 Message Date
09aaccf291
0.4.6 2024-02-22 15:06:16 +08:00
8c72732671
更新一下内置插件 2024-02-22 14:38:37 +08:00
2d47285b76
update log 2024-02-22 14:31:10 +08:00
cfcac8f1c9
0.4.5 2024-02-22 14:30:56 +08:00
42eec7532b
移除一个多余 use 2024-02-22 14:14:57 +08:00
9 changed files with 58 additions and 25 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ica-rs" name = "ica-rs"
version = "0.4.4" version = "0.4.6"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -55,6 +55,9 @@ class NewMessage:
@property @property
def is_from_self(self) -> bool: def is_from_self(self) -> bool:
... ...
@property
def is_reply(self) -> bool:
...
class IcaClient: class IcaClient:

View File

@ -9,7 +9,7 @@ else:
_version_ = "1.0.0" _version_ = "1.0.0"
def on_message(msg: NewMessage, client: IcaClient) -> None: def on_message(msg: NewMessage, client: IcaClient) -> None:
if not msg.is_from_self: if not (msg.is_from_self or msg.is_reply):
if msg.content == "/bot-rs-py": if msg.content == "/bot":
reply = msg.reply_with(f"ica-async-rs-sync-py {_version_}") reply = msg.reply_with(f"ica-async-rs-sync-py {_version_}")
client.send_message(reply) client.send_message(reply)

View File

@ -81,6 +81,6 @@ def bmcl(msg: NewMessage, client: IcaClient) -> None:
def on_message(msg: NewMessage, client: IcaClient) -> None: def on_message(msg: NewMessage, client: IcaClient) -> None:
if not msg.is_from_self: if not (msg.is_from_self or msg.is_reply):
if msg.content == "/bmcl-rs": if msg.content == "/bmcl":
bmcl(msg, client) bmcl(msg, client)

View File

@ -194,6 +194,13 @@ impl NewMessage {
} }
} }
pub fn output(&self) -> String {
format!(
"Room: {}, Sender: {}|{}, Content: {}",
self.room_id, self.sender_id, self.sender_name, self.content
)
}
/// 作为回复消息使用 /// 作为回复消息使用
pub fn as_reply(&self) -> ReplyMessage { pub fn as_reply(&self) -> ReplyMessage {
ReplyMessage { ReplyMessage {

View File

@ -30,13 +30,7 @@ pub async fn add_message(payload: Payload, client: Client) {
if let Payload::Text(values) = payload { if let Payload::Text(values) = payload {
if let Some(value) = values.first() { if let Some(value) = values.first() {
let message = NewMessage::new_from_json(value); let message = NewMessage::new_from_json(value);
info!("add_message {}", format!("{:#?}", message).cyan()); info!("add_message {}", message.output().cyan());
// if message.is_reply() {
// return;
// }
// if message.is_from_self() {
// return;
// }
// 就在这里处理掉最基本的消息 // 就在这里处理掉最基本的消息
// 之后的处理交给插件 // 之后的处理交给插件
if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {

View File

@ -139,6 +139,10 @@ impl NewMessagePy {
pub fn get_is_from_self(&self) -> bool { pub fn get_is_from_self(&self) -> bool {
self.msg.is_from_self() self.msg.is_from_self()
} }
#[getter]
pub fn get_is_reply(&self) -> bool {
self.msg.is_reply()
}
} }
impl NewMessagePy { impl NewMessagePy {

View File

@ -3,10 +3,11 @@ pub mod class;
use std::time::SystemTime; use std::time::SystemTime;
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf};
use pyo3::{prelude::*, types::IntoPyDict}; use pyo3::prelude::*;
use rust_socketio::asynchronous::Client; use rust_socketio::asynchronous::Client;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use crate::client::IcalinguaStatus;
use crate::config::IcaConfig; use crate::config::IcaConfig;
use crate::data_struct::messages::NewMessage; use crate::data_struct::messages::NewMessage;
@ -21,7 +22,6 @@ impl PyStatus {
match PYSTATUS.files.as_ref() { match PYSTATUS.files.as_ref() {
Some(files) => files, Some(files) => files,
None => { None => {
debug!("No files in py status");
PYSTATUS.files = Some(HashMap::new()); PYSTATUS.files = Some(HashMap::new());
PYSTATUS.files.as_ref().unwrap() PYSTATUS.files.as_ref().unwrap()
} }
@ -34,10 +34,8 @@ impl PyStatus {
match PYSTATUS.files.as_mut() { match PYSTATUS.files.as_mut() {
Some(files) => { Some(files) => {
files.insert(path, (changed_time, py_module)); files.insert(path, (changed_time, py_module));
debug!("Added file to py status, {:?}", files);
} }
None => { None => {
warn!("No files in py status, creating new");
let mut files = HashMap::new(); let mut files = HashMap::new();
files.insert(path, (changed_time, py_module)); files.insert(path, (changed_time, py_module));
PYSTATUS.files = Some(files); PYSTATUS.files = Some(files);
@ -127,28 +125,47 @@ pub fn load_py_plugins(path: &PathBuf) {
} }
pub fn verify_plugins() { pub fn verify_plugins() {
let plugins = PyStatus::get_files(); let mut need_reload_files: Vec<PathBuf> = Vec::new();
for (path, _) in plugins.iter() { let plugin_path = IcalinguaStatus::get_config().py_plugin_path.as_ref().unwrap().to_owned();
if !PyStatus::verify_file(path) { for entry in std::fs::read_dir(&plugin_path).unwrap() {
info!("file changed: {:?}", path); if let Ok(entry) = entry {
if let Ok((changed_time, content)) = load_py_file(path) { let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "py" {
if !PyStatus::verify_file(&path) {
need_reload_files.push(path);
}
}
}
}
}
if need_reload_files.is_empty() {
return;
}
info!("file change list: {:?}", need_reload_files);
for reload_file in need_reload_files {
match load_py_file(&reload_file) {
Ok((changed_time, content)) => {
let py_module = Python::with_gil(|py| -> Py<PyAny> { let py_module = Python::with_gil(|py| -> Py<PyAny> {
let module: Py<PyAny> = PyModule::from_code( let module: Py<PyAny> = PyModule::from_code(
py, py,
&content, &content,
&path.to_string_lossy(), &reload_file.to_string_lossy(),
&path.to_string_lossy(), &reload_file.to_string_lossy(),
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
) )
.unwrap() .unwrap()
.into(); .into();
module module
}); });
PyStatus::add_file(path.clone(), changed_time, py_module); PyStatus::add_file(reload_file.clone(), changed_time, py_module);
},
Err(e) => {
warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e);
} }
} }
} }
} }
pub fn get_change_time(path: &PathBuf) -> Option<SystemTime> { pub fn get_change_time(path: &PathBuf) -> Option<SystemTime> {

View File

@ -1,5 +1,13 @@
# 更新日志 # 更新日志
## 0.4.6
现在更适合部署了
## 0.4.5
添加 `is_reply` api 到 `NewMessagePy`
## 0.4.4 ## 0.4.4
现在正式支持 Python 插件了 现在正式支持 Python 插件了