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

107 lines
2.7 KiB
Rust
Raw Normal View History

2024-02-18 21:25:42 +08:00
use std::time::Duration;
mod config;
2024-03-13 01:17:50 +08:00
mod data_struct;
mod error;
mod py;
2024-03-14 01:12:08 +08:00
mod status;
2024-03-30 12:52:49 +08:00
#[cfg(feature = "ica")]
mod ica;
#[cfg(feature = "tailchat")]
mod tailchat;
2024-03-13 01:20:41 +08:00
use config::BotConfig;
use tracing::{event, info, span, Level};
2024-03-14 01:12:08 +08:00
pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus {
2024-02-20 17:47:45 +08:00
config: None,
2024-03-14 01:12:08 +08:00
ica_status: None,
2024-03-30 16:59:06 +08:00
tailchat_status: None,
2024-02-20 14:47:53 +08:00
};
2024-03-14 01:12:08 +08:00
pub type MainStatus = status::BotStatus;
pub type StopGetter = tokio::sync::oneshot::Receiver<()>;
2024-02-20 20:51:14 +08:00
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
2024-06-12 01:09:20 +08:00
pub const ICA_VERSION: &str = "1.6.0";
pub const TAILCHAT_VERSION: &str = "1.1.0";
2024-02-20 20:51:14 +08:00
/// usage:
/// #[derive(Clone)]
/// struct BotState(String);
///
/// async fn some_event_with_state(payload: Payload, client: Client, state: Arc<BotState>) {
/// // do something
///
// macro_rules! wrap_callback_with_state {
// ($f:expr, $state:tt) => {};
// }
2024-03-13 01:17:50 +08:00
2024-03-12 00:16:12 +08:00
#[tokio::main]
async fn main() {
// -d -> debug
// none -> info
let level = {
let args = std::env::args();
2024-06-03 23:52:16 +08:00
let args = args.collect::<Vec<String>>();
if args.contains(&"-d".to_string()) {
Level::DEBUG
2024-06-03 23:52:16 +08:00
} else if args.contains(&"-t".to_string()) {
Level::TRACE
} else {
Level::INFO
}
};
tracing_subscriber::fmt().with_max_level(level).init();
let span = span!(Level::INFO, "Shenbot Main");
let _enter = span.enter();
event!(Level::INFO, "shenbot-async-rs v{} starting", VERSION);
2024-03-12 00:16:12 +08:00
2024-03-13 01:17:50 +08:00
let bot_config = BotConfig::new_from_cli();
MainStatus::static_init(bot_config);
let bot_config = MainStatus::global_config();
py::init_py();
2024-03-12 00:16:12 +08:00
// 准备一个用于停止 socket 的变量
event!(Level::INFO, "启动 ICA");
let (ica_send, ica_recv) = tokio::sync::oneshot::channel::<()>();
2024-03-13 01:17:50 +08:00
if bot_config.check_ica() {
event!(Level::INFO, "启动 ica");
2024-03-12 00:16:12 +08:00
let config = bot_config.ica();
tokio::spawn(async move {
ica::start_ica(&config, ica_recv).await.unwrap();
});
} else {
event!(Level::INFO, "未启用 ica");
}
2024-03-30 16:59:06 +08:00
let (tailchat_send, tailchat_recv) = tokio::sync::oneshot::channel::<()>();
if bot_config.check_tailchat() {
event!(Level::INFO, "启动 Tailchat");
let config = bot_config.tailchat();
tokio::spawn(async move {
tailchat::start_tailchat(config, tailchat_recv).await.unwrap();
});
} else {
event!(Level::INFO, "未启用 Tailchat");
}
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
ica_send.send(()).ok();
2024-03-30 16:59:06 +08:00
tailchat_send.send(()).ok();
2024-03-13 01:17:50 +08:00
2024-02-18 23:17:43 +08:00
info!("Disconnected");
}