ping! pong!

This commit is contained in:
shenjack 2024-02-20 20:51:14 +08:00
parent 00dc10f395
commit d9184925d2
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 46 additions and 4 deletions

View File

@ -1,10 +1,21 @@
use crate::config::IcaConfig; use crate::config::IcaConfig;
use crate::data_struct::messages::SendMessage;
use crate::data_struct::{all_rooms::Room, online_data::OnlineData}; use crate::data_struct::{all_rooms::Room, online_data::OnlineData};
use ed25519_dalek::{Signature, Signer, SigningKey}; use ed25519_dalek::{Signature, Signer, SigningKey};
use rust_socketio::{Payload, RawClient}; use rust_socketio::{Payload, RawClient};
use serde_json::Value; 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)] #[derive(Debug, Clone)]
pub struct IcalinguaStatus { pub struct IcalinguaStatus {
@ -40,6 +51,10 @@ impl IcalinguaStatus {
self.config = Some(config); self.config = Some(config);
} }
pub fn get_online_data(&self) -> &OnlineData {
self.online_data.as_ref().unwrap()
}
pub fn get_config(&self) -> &IcaConfig { pub fn get_config(&self) -> &IcaConfig {
self.config.as_ref().unwrap() self.config.as_ref().unwrap()
} }

View File

@ -1,5 +1,6 @@
use crate::data_struct::files::MessageFile; use crate::data_struct::files::MessageFile;
use crate::data_struct::{MessageId, RoomId, UserId}; use crate::data_struct::{MessageId, RoomId, UserId};
use crate::ClientStatus;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -215,6 +216,13 @@ impl NewMessage {
self.reply.is_some() 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> { pub fn get_reply(&self) -> Option<&ReplyMessage> {
self.reply.as_ref() self.reply.as_ref()
@ -245,6 +253,10 @@ impl SendMessage {
at: json!([]), at: json!([]),
} }
} }
pub fn as_value(&self) -> JsonValue {
serde_json::to_value(self).unwrap()
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -2,10 +2,11 @@ use colored::Colorize;
use rust_socketio::{Event, Payload, RawClient}; use rust_socketio::{Event, Payload, RawClient};
use tracing::{info, warn}; use tracing::{info, warn};
use crate::client::send_message;
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::NewMessage;
use crate::data_struct::online_data::OnlineData; use crate::data_struct::online_data::OnlineData;
use crate::py; use crate::{py, VERSION};
/// 获取在线数据 /// 获取在线数据
pub fn get_online_data(payload: Payload, _client: RawClient) { 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 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 {}", 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)
}
} }
} }
} }

View File

@ -17,6 +17,8 @@ pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
config: None, config: None,
}; };
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() { fn main() {
tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG) .with_max_level(tracing::Level::DEBUG)
@ -50,7 +52,7 @@ fn main() {
if ica_config.notice_start { if ica_config.notice_start {
for room in ica_config.notice_room.iter() { for room in ica_config.notice_room.iter() {
let startup_msg = crate::data_struct::messages::SendMessage::new( 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(), room.clone(),
None, None,
); );