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

114 lines
3.7 KiB
Rust
Raw Normal View History

2024-02-18 21:25:42 +08:00
use std::time::Duration;
2024-03-12 00:16:12 +08:00
use config::{BotConfig, IcaConfig};
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;
2024-03-12 00:16:12 +08:00
mod matrix;
mod py;
2024-02-20 14:47:53 +08:00
#[allow(non_upper_case_globals)]
2024-03-12 00:16:12 +08:00
pub static mut ClientStatus_Global: client::BotStatus = client::BotStatus {
2024-02-20 14:47:53 +08:00
login: false,
2024-02-25 12:01:36 +08:00
current_loaded_messages_count: 0,
2024-02-20 14:47:53 +08:00
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()
};
}
2024-03-12 00:16:12 +08:00
async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::Receiver<()>) {
let socket = ClientBuilder::new(config.host.clone())
2024-02-21 21:42:27 +08:00
.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("messageSuccess", wrap_callback!(events::succes_message))
.on("messageFailed", wrap_callback!(events::failed_message))
2024-02-21 21:42:27 +08:00
.on("onlineData", wrap_callback!(events::get_online_data))
.on("setAllRooms", wrap_callback!(events::update_all_room))
2024-02-25 01:31:39 +08:00
.on("setMessages", wrap_callback!(events::set_messages))
2024-02-21 21:42:27 +08:00
.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");
2024-03-12 00:16:12 +08:00
if config.notice_start {
for room in 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,
);
2024-02-25 18:20:03 +08:00
tokio::time::sleep(Duration::from_secs(1)).await;
info!("发送启动消息到房间: {}", room);
2024-02-25 01:33:56 +08:00
if let Err(e) =
socket.emit("sendMessage", serde_json::to_value(startup_msg).unwrap()).await
2024-02-21 21:42:27 +08:00
{
info!("启动信息发送失败 房间:{}|e:{}", room, e);
}
}
}
2024-03-12 00:16:12 +08:00
// 等待停止信号
stop_reciver.await.ok();
socket.disconnect().await.expect("Disconnect failed");
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
info!("ica-async-rs v{}", VERSION);
// 从命令行获取 host 和 key
// 从命令行获取配置文件路径
let bot_config = config::BotConfig::new_from_cli();
client::BotStatus::update_config(bot_config.clone());
py::init_py(&bot_config);
// 准备一个用于停止 socket 的变量
let (send, recv) = tokio::sync::oneshot::channel::<()>();
if bot_config.ica.is_some() && bot_config.ica().enable {
info!("启动 ica");
let config = bot_config.ica();
tokio::spawn(async move {
start_ica(&config, recv).await;
});
} else {
info!("未启用 ica");
}
2024-02-25 18:20:03 +08:00
tokio::time::sleep(Duration::from_secs(2)).await;
2024-02-18 23:17:43 +08:00
// 等待一个输入
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
2024-03-12 00:16:12 +08:00
// socket.disconnect().await.expect("Disconnect failed");
send.send(()).ok();
2024-02-18 23:17:43 +08:00
info!("Disconnected");
}