diff --git a/ica-rs/src/client.rs b/ica-rs/src/client.rs index 8faeccd..2087896 100644 --- a/ica-rs/src/client.rs +++ b/ica-rs/src/client.rs @@ -1,10 +1,21 @@ use crate::config::IcaConfig; +use crate::data_struct::messages::SendMessage; use crate::data_struct::{all_rooms::Room, online_data::OnlineData}; use ed25519_dalek::{Signature, Signer, SigningKey}; use rust_socketio::{Payload, RawClient}; use serde_json::Value; -use tracing::debug; +use colored::Colorize; +use tracing::{debug, warn}; + +/// "安全" 的 发送一条消息 +pub fn send_message(client: RawClient, message: SendMessage) { + let value = message.as_value(); + match client.emit("sendMessage", value) { + Ok(_) => debug!("send_message {}", format!("{:#?}", message).cyan()), + Err(e) => warn!("send_message faild:{}", format!("{:#?}", e).red()), + } +} #[derive(Debug, Clone)] pub struct IcalinguaStatus { @@ -40,6 +51,10 @@ impl IcalinguaStatus { self.config = Some(config); } + pub fn get_online_data(&self) -> &OnlineData { + self.online_data.as_ref().unwrap() + } + pub fn get_config(&self) -> &IcaConfig { self.config.as_ref().unwrap() } diff --git a/ica-rs/src/data_struct/messages.rs b/ica-rs/src/data_struct/messages.rs index bb6e078..6a71929 100644 --- a/ica-rs/src/data_struct/messages.rs +++ b/ica-rs/src/data_struct/messages.rs @@ -1,5 +1,6 @@ use crate::data_struct::files::MessageFile; use crate::data_struct::{MessageId, RoomId, UserId}; +use crate::ClientStatus; use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; @@ -215,6 +216,13 @@ impl NewMessage { self.reply.is_some() } + pub fn is_from_self(&self) -> bool { + let qq_id = unsafe { + ClientStatus.get_online_data().qqid + }; + self.sender_id == qq_id + } + /// 获取回复 pub fn get_reply(&self) -> Option<&ReplyMessage> { self.reply.as_ref() @@ -245,6 +253,10 @@ impl SendMessage { at: json!([]), } } + + pub fn as_value(&self) -> JsonValue { + serde_json::to_value(self).unwrap() + } } #[cfg(test)] diff --git a/ica-rs/src/events.rs b/ica-rs/src/events.rs index 2325ae7..ea2807c 100644 --- a/ica-rs/src/events.rs +++ b/ica-rs/src/events.rs @@ -2,10 +2,11 @@ use colored::Colorize; use rust_socketio::{Event, Payload, RawClient}; use tracing::{info, warn}; +use crate::client::send_message; use crate::data_struct::all_rooms::Room; use crate::data_struct::messages::NewMessage; use crate::data_struct::online_data::OnlineData; -use crate::py; +use crate::{py, VERSION}; /// 获取在线数据 pub fn get_online_data(payload: Payload, _client: RawClient) { @@ -24,11 +25,23 @@ pub fn get_online_data(payload: Payload, _client: RawClient) { } /// 接收消息 -pub fn add_message(payload: Payload, _client: RawClient) { +pub fn add_message(payload: Payload, client: RawClient) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { let message = NewMessage::new_from_json(value); info!("add_message {}", format!("{:#?}", message).cyan()); + if message.is_reply() { + return; + } + if message.is_from_self() { + return; + } + // 就在这里处理掉最基本的消息 + // 之后的处理交给插件 + if message.content.eq("/bot ping") { + let reply = message.reply_with(&format!("ica-rs pong v{}", VERSION)); + send_message(client, reply) + } } } } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 8bd6966..e5c46f1 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -17,6 +17,8 @@ pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus { config: None, }; +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + fn main() { tracing_subscriber::fmt() .with_max_level(tracing::Level::DEBUG) @@ -50,7 +52,7 @@ fn main() { if ica_config.notice_start { for room in ica_config.notice_room.iter() { let startup_msg = crate::data_struct::messages::SendMessage::new( - format!("ica-rs bot v{}", env!("CARGO_PKG_VERSION")), + format!("ica-rs bot v{}", VERSION), room.clone(), None, );