更新代码,修复了一些bug和改进了功能。

This commit is contained in:
shenjack 2024-02-25 01:32:31 +08:00
parent fe06356bea
commit 8b2a8ee8d2
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 44 additions and 68 deletions

View File

@ -5,7 +5,7 @@ use tracing::{info, warn};
use crate::client::{send_message, IcalinguaStatus}; use crate::client::{send_message, IcalinguaStatus};
use crate::data_struct::all_rooms::Room; use crate::data_struct::all_rooms::Room;
use crate::data_struct::messages::NewMessage; use crate::data_struct::messages::{MessageTrait, NewMessage};
use crate::data_struct::online_data::OnlineData; use crate::data_struct::online_data::OnlineData;
use crate::{py, VERSION}; use crate::{py, VERSION};
@ -26,19 +26,19 @@ pub async fn get_online_data(payload: Payload, _client: Client) {
pub async fn add_message(payload: Payload, client: Client) { 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 = serde_json::from_value(value.clone()).unwrap();
// 检测是否在过滤列表内 // 检测是否在过滤列表内
if IcalinguaStatus::get_config() if IcalinguaStatus::get_config()
.filter_list .filter_list
.contains(&message.sender_id) .contains(&message.msg.sender_id)
{ {
return; return;
} }
info!("add_message {}", message.output().cyan()); info!("add_message {}", message.to_string().cyan());
// info!("add_message {}", format!("{:#?}", message).cyan()); // info!("add_message {}", format!("{:#?}", message).cyan());
// 就在这里处理掉最基本的消息 // 就在这里处理掉最基本的消息
// 之后的处理交给插件 // 之后的处理交给插件
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() {
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;
} }
@ -48,6 +48,19 @@ pub async fn add_message(payload: Payload, client: Client) {
} }
} }
/// 理论上不会用到 (因为依赖一个客户端去请求)
/// 但反正实际上还是我去请求, 所以只是暂时
/// 加载一个房间的所有消息
pub async fn set_messages(payload: Payload, _client: Client) {
if let Payload::Text(values) = payload {
if let Some(value) = values.first() {
let messages: Vec<NewMessage> = serde_json::from_value(value.clone()).unwrap();
let room_id = value["roomId"].as_i64().unwrap();
info!("set_messages {} len: {}", room_id.to_string().cyan(), messages.len());
}
}
}
/// 撤回消息 /// 撤回消息
pub async fn delete_message(payload: Payload, client: Client) { pub async fn delete_message(payload: Payload, client: Client) {
if let Payload::Text(values) = payload { if let Payload::Text(values) = payload {
@ -66,10 +79,8 @@ pub async fn update_all_room(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() {
if let Some(raw_rooms) = value.as_array() { if let Some(raw_rooms) = value.as_array() {
let rooms: Vec<Room> = raw_rooms let rooms: Vec<Room> =
.iter() raw_rooms.iter().map(|room| Room::new_from_json(room)).collect();
.map(|room| Room::new_from_json(room))
.collect();
unsafe { unsafe {
crate::ClientStatus.update_rooms(rooms.clone()); crate::ClientStatus.update_rooms(rooms.clone());
} }
@ -107,6 +118,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
"addMessage", "addMessage",
"deleteMessage", "deleteMessage",
"setAllRooms", "setAllRooms",
"setMessages",
// 也许以后会用到 // 也许以后会用到
"messageSuccess", "messageSuccess",
"messageFailed", "messageFailed",

View File

@ -1,17 +1,15 @@
use std::path::PathBuf; use std::path::PathBuf;
use tracing::{warn, debug};
use pyo3::prelude::*; use pyo3::prelude::*;
use rust_socketio::asynchronous::Client; use rust_socketio::asynchronous::Client;
use tracing::{debug, warn};
use crate::data_struct::MessageId;
use crate::data_struct::messages::NewMessage; use crate::data_struct::messages::NewMessage;
use crate::data_struct::MessageId;
use crate::py::{class, verify_plugins, PyStatus}; use crate::py::{class, verify_plugins, PyStatus};
pub fn get_func<'a>(py_module: &'a PyAny, path: &PathBuf, name: &'a str) -> Option<&'a PyAny> { pub fn get_func<'a>(py_module: &'a PyAny, path: &PathBuf, name: &'a str) -> Option<&'a PyAny> {
// 要处理的情况: // 要处理的情况:
// 1. 有这个函数 // 1. 有这个函数
// 2. 没有这个函数 // 2. 没有这个函数
// 3. 函数不是 Callable // 3. 函数不是 Callable
@ -26,7 +24,7 @@ pub fn get_func<'a>(py_module: &'a PyAny, path: &PathBuf, name: &'a str) -> Opti
warn!("function<{}>: {:#?} in {:?} is not callable", name, func, path); warn!("function<{}>: {:#?} in {:?} is not callable", name, func, path);
None None
} }
}, }
Err(e) => { Err(e) => {
warn!("failed to get function<{}> from {:?}: {:?}", name, path, e); warn!("failed to get function<{}> from {:?}: {:?}", name, path, e);
None None

View File

@ -4,7 +4,7 @@ use tokio::runtime::Runtime;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use crate::client::send_message; use crate::client::send_message;
use crate::data_struct::messages::{NewMessage, ReplyMessage, SendMessage}; use crate::data_struct::messages::{MessageTrait, NewMessage, ReplyMessage, SendMessage};
use crate::data_struct::MessageId; use crate::data_struct::MessageId;
use crate::ClientStatus; use crate::ClientStatus;
@ -15,14 +15,10 @@ pub struct IcaStatusPy {}
#[pymethods] #[pymethods]
impl IcaStatusPy { impl IcaStatusPy {
#[new] #[new]
pub fn py_new() -> Self { pub fn py_new() -> Self { Self {} }
Self {}
}
#[getter] #[getter]
pub fn get_login(&self) -> bool { pub fn get_login(&self) -> bool { unsafe { ClientStatus.login } }
unsafe { ClientStatus.login }
}
#[getter] #[getter]
pub fn get_online(&self) -> bool { pub fn get_online(&self) -> bool {
@ -106,9 +102,7 @@ impl IcaStatusPy {
} }
impl IcaStatusPy { impl IcaStatusPy {
pub fn new() -> Self { pub fn new() -> Self { Self {} }
Self {}
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -124,35 +118,21 @@ impl NewMessagePy {
SendMessagePy::new(self.msg.reply_with(&content)) SendMessagePy::new(self.msg.reply_with(&content))
} }
pub fn __str__(&self) -> String { pub fn __str__(&self) -> String { format!("{:?}", self.msg) }
format!("{:?}", self.msg)
}
#[getter] #[getter]
pub fn get_id(&self) -> MessageId { pub fn get_id(&self) -> MessageId { self.msg.msg_id().clone() }
self.msg.msg_id.clone()
}
#[getter] #[getter]
pub fn get_content(&self) -> String { pub fn get_content(&self) -> String { self.msg.content().clone() }
self.msg.content.clone()
}
#[getter] #[getter]
pub fn get_sender_id(&self) -> i64 { pub fn get_sender_id(&self) -> i64 { self.msg.sender_id() }
self.msg.sender_id
}
#[getter] #[getter]
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] #[getter]
pub fn get_is_reply(&self) -> bool { pub fn get_is_reply(&self) -> bool { self.msg.is_reply() }
self.msg.is_reply()
}
} }
impl NewMessagePy { impl NewMessagePy {
pub fn new(msg: &NewMessage) -> Self { pub fn new(msg: &NewMessage) -> Self { Self { msg: msg.clone() } }
Self { msg: msg.clone() }
}
} }
#[pyclass] #[pyclass]
@ -163,15 +143,11 @@ pub struct ReplyMessagePy {
#[pymethods] #[pymethods]
impl ReplyMessagePy { impl ReplyMessagePy {
pub fn __str__(&self) -> String { pub fn __str__(&self) -> String { format!("{:?}", self.msg) }
format!("{:?}", self.msg)
}
} }
impl ReplyMessagePy { impl ReplyMessagePy {
pub fn new(msg: ReplyMessage) -> Self { pub fn new(msg: ReplyMessage) -> Self { Self { msg } }
Self { msg }
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -183,29 +159,21 @@ pub struct SendMessagePy {
#[pymethods] #[pymethods]
impl SendMessagePy { impl SendMessagePy {
pub fn __str__(&self) -> String { pub fn __str__(&self) -> String { format!("{:?}", self.msg) }
format!("{:?}", self.msg)
}
/// 设置消息内容 /// 设置消息内容
/// 用于链式调用 /// 用于链式调用
pub fn with_content(&mut self, content: String) -> Self { pub fn with_content(&mut self, content: String) -> Self {
self.msg.content = content; self.msg.content = content;
self.clone() self.clone()
} }
#[getter] #[getter]
pub fn get_content(&self) -> String { pub fn get_content(&self) -> String { self.msg.content.clone() }
self.msg.content.clone()
}
#[setter] #[setter]
pub fn set_content(&mut self, content: String) { pub fn set_content(&mut self, content: String) { self.msg.content = content; }
self.msg.content = content;
}
} }
impl SendMessagePy { impl SendMessagePy {
pub fn new(msg: SendMessage) -> Self { pub fn new(msg: SendMessage) -> Self { Self { msg } }
Self { msg }
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -239,9 +207,7 @@ impl IcaClientPy {
} }
#[getter] #[getter]
pub fn get_status(&self) -> IcaStatusPy { pub fn get_status(&self) -> IcaStatusPy { IcaStatusPy::new() }
IcaStatusPy::new()
}
pub fn debug(&self, content: String) { pub fn debug(&self, content: String) {
debug!("{}", content); debug!("{}", content);