mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2024-11-23 12:41:05 +08:00
0.4.6
This commit is contained in:
parent
8c72732671
commit
09aaccf291
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ica-rs"
|
name = "ica-rs"
|
||||||
version = "0.4.5"
|
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
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -7,6 +7,7 @@ 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> {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user