Compare commits

..

No commits in common. "09aaccf291b14c33f0cd4cb761a58162cf662953" and "df0355cad7366df3ed7bb9a8da4863b9b5caaf41" have entirely different histories.

9 changed files with 25 additions and 58 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ica-rs" name = "ica-rs"
version = "0.4.6" version = "0.4.4"
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,9 +55,6 @@ 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 or msg.is_reply): if not msg.is_from_self:
if msg.content == "/bot": if msg.content == "/bot-rs-py":
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 or msg.is_reply): if not msg.is_from_self:
if msg.content == "/bmcl": if msg.content == "/bmcl-rs":
bmcl(msg, client) bmcl(msg, client)

View File

@ -194,13 +194,6 @@ 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,7 +30,13 @@ 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 {}", message.output().cyan()); info!("add_message {}", format!("{:#?}", message).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,10 +139,6 @@ 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,11 +3,10 @@ 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::*; use pyo3::{prelude::*, types::IntoPyDict};
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;
@ -22,6 +21,7 @@ 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,8 +34,10 @@ 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);
@ -125,47 +127,28 @@ pub fn load_py_plugins(path: &PathBuf) {
} }
pub fn verify_plugins() { pub fn verify_plugins() {
let mut need_reload_files: Vec<PathBuf> = Vec::new(); let plugins = PyStatus::get_files();
let plugin_path = IcalinguaStatus::get_config().py_plugin_path.as_ref().unwrap().to_owned(); for (path, _) in plugins.iter() {
for entry in std::fs::read_dir(&plugin_path).unwrap() { if !PyStatus::verify_file(path) {
if let Ok(entry) = entry { info!("file changed: {:?}", path);
let path = entry.path(); if let Ok((changed_time, content)) = load_py_file(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,
&reload_file.to_string_lossy(), &path.to_string_lossy(),
&reload_file.to_string_lossy(), &path.to_string_lossy(),
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
) )
.unwrap() .unwrap()
.into(); .into();
module module
}); });
PyStatus::add_file(reload_file.clone(), changed_time, py_module); PyStatus::add_file(path.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,13 +1,5 @@
# 更新日志 # 更新日志
## 0.4.6
现在更适合部署了
## 0.4.5
添加 `is_reply` api 到 `NewMessagePy`
## 0.4.4 ## 0.4.4
现在正式支持 Python 插件了 现在正式支持 Python 插件了