icalingua-python-bot/ica-rs/src/main.rs

95 lines
2.9 KiB
Rust
Raw Normal View History

2024-02-18 21:25:42 +08:00
use std::time::Duration;
2024-02-21 21:42:27 +08:00
use futures_util::FutureExt;
use rust_socketio::asynchronous::{Client, ClientBuilder};
use rust_socketio::{Event, Payload, TransportType};
2024-02-20 14:47:53 +08:00
use tracing::info;
2024-02-18 21:25:42 +08:00
mod client;
mod config;
2024-02-18 21:25:42 +08:00
mod data_struct;
mod events;
mod py;
2024-02-20 14:47:53 +08:00
#[allow(non_upper_case_globals)]
pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
login: false,
online_data: None,
rooms: None,
2024-02-20 17:47:45 +08:00
config: None,
2024-02-20 14:47:53 +08:00
};
2024-02-20 20:51:14 +08:00
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
2024-02-21 21:42:27 +08:00
macro_rules! wrap_callback {
($f:expr) => {
|payload: Payload, client: Client| $f(payload, client).boxed()
};
}
macro_rules! wrap_any_callback {
($f:expr) => {
|event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed()
};
}
#[tokio::main]
async fn main() {
2024-02-20 14:47:53 +08:00
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
2024-02-22 19:16:07 +08:00
info!("ica-async-rs v{}", VERSION);
2024-02-20 14:47:53 +08:00
2023-12-11 18:17:55 +08:00
// 从命令行获取 host 和 key
2023-12-24 17:27:15 +08:00
// 从命令行获取配置文件路径
let ica_config = config::IcaConfig::new_from_cli();
2024-02-20 17:47:45 +08:00
unsafe {
ClientStatus.update_config(ica_config.clone());
}
2024-02-20 20:11:25 +08:00
py::init_py(&ica_config);
2024-02-21 21:42:27 +08:00
let socket = ClientBuilder::new(ica_config.host.clone())
.transport_type(TransportType::Websocket)
.on_any(wrap_any_callback!(events::any_event))
.on("requireAuth", wrap_callback!(client::sign_callback))
.on("message", wrap_callback!(events::connect_callback))
.on("authSucceed", wrap_callback!(events::connect_callback))
.on("authFailed", wrap_callback!(events::connect_callback))
.on("onlineData", wrap_callback!(events::get_online_data))
.on("setAllRooms", wrap_callback!(events::update_all_room))
.on("addMessage", wrap_callback!(events::add_message))
.on("deleteMessage", wrap_callback!(events::delete_message))
.connect()
2024-02-21 21:42:27 +08:00
.await
.expect("Connection failed");
2024-02-18 23:17:43 +08:00
info!("Connected");
if ica_config.notice_start {
for room in ica_config.notice_room.iter() {
let startup_msg = crate::data_struct::messages::SendMessage::new(
2024-02-21 21:50:52 +08:00
format!("ica-async-rs bot v{}", VERSION),
room.clone(),
None,
);
std::thread::sleep(Duration::from_secs(1));
info!("发送启动消息到房间: {}", room);
2024-02-21 21:42:27 +08:00
if let Err(e) = socket
.emit("sendMessage", serde_json::to_value(startup_msg).unwrap())
.await
{
info!("启动信息发送失败 房间:{}|e:{}", room, e);
}
}
}
2024-02-18 23:17:43 +08:00
std::thread::sleep(Duration::from_secs(3));
// 等待一个输入
info!("Press any key to exit");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
2024-02-21 21:42:27 +08:00
socket.disconnect().await.expect("Disconnect failed");
2024-02-18 23:17:43 +08:00
info!("Disconnected");
}