2024-02-18 21:25:42 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-01-24 18:14:50 +08:00
|
|
|
mod config;
|
2024-03-13 01:17:50 +08:00
|
|
|
mod data_struct;
|
2024-03-16 14:04:30 +08:00
|
|
|
mod error;
|
2024-01-24 18:14:50 +08:00
|
|
|
mod py;
|
2024-03-14 01:12:08 +08:00
|
|
|
mod status;
|
2023-12-11 01:17:25 +08:00
|
|
|
|
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;
|
2024-06-15 01:34:03 +08:00
|
|
|
use tracing::{event, 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-07-23 23:27:08 +08:00
|
|
|
startup_time: None,
|
2024-02-20 14:47:53 +08:00
|
|
|
};
|
|
|
|
|
2024-03-14 01:12:08 +08:00
|
|
|
pub type MainStatus = status::BotStatus;
|
|
|
|
|
2024-03-16 14:04:30 +08:00
|
|
|
pub type StopGetter = tokio::sync::oneshot::Receiver<()>;
|
|
|
|
|
2024-02-20 20:51:14 +08:00
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
2024-07-23 23:27:08 +08:00
|
|
|
pub const ICA_VERSION: &str = "1.6.1";
|
|
|
|
pub const TAILCHAT_VERSION: &str = "1.2.1";
|
2024-02-20 20:51:14 +08:00
|
|
|
|
2024-08-17 21:02:52 +08:00
|
|
|
/// 是否为稳定版本
|
|
|
|
/// 会在 release 的时候设置为 true
|
|
|
|
pub const STABLE: bool = false;
|
|
|
|
|
2024-06-15 00:35:21 +08:00
|
|
|
#[macro_export]
|
2024-06-14 01:19:25 +08:00
|
|
|
macro_rules! async_callback_with_state {
|
|
|
|
($f:expr, $state:expr) => {{
|
|
|
|
use futures_util::FutureExt;
|
|
|
|
let state = $state.clone();
|
|
|
|
move |payload: Payload, client: Client| $f(payload, client, state.clone()).boxed()
|
|
|
|
}};
|
|
|
|
}
|
2024-03-13 01:17:50 +08:00
|
|
|
|
2024-06-15 00:35:35 +08:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! async_any_callback_with_state {
|
|
|
|
($f:expr, $state:expr) => {{
|
|
|
|
use futures_util::FutureExt;
|
|
|
|
let state = $state.clone();
|
|
|
|
move |event: Event, payload: Payload, client: Client| {
|
|
|
|
$f(event, payload, client, state.clone()).boxed()
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2024-03-12 00:16:12 +08:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2024-03-16 14:04:30 +08:00
|
|
|
// -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()) {
|
2024-03-16 14:04:30 +08:00
|
|
|
Level::DEBUG
|
2024-06-03 23:52:16 +08:00
|
|
|
} else if args.contains(&"-t".to_string()) {
|
|
|
|
Level::TRACE
|
2024-03-16 14:04:30 +08:00
|
|
|
} else {
|
|
|
|
Level::INFO
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing_subscriber::fmt().with_max_level(level).init();
|
|
|
|
let span = span!(Level::INFO, "Shenbot Main");
|
|
|
|
let _enter = span.enter();
|
|
|
|
|
2024-08-17 21:02:52 +08:00
|
|
|
event!(Level::INFO, "shenbot-rs v{} starting", VERSION);
|
|
|
|
if !STABLE {
|
|
|
|
event!(Level::WARN, "这是一个开发版本, 有问题记得找 shenjack");
|
|
|
|
}
|
2024-03-12 00:16:12 +08:00
|
|
|
|
2024-03-13 01:17:50 +08:00
|
|
|
let bot_config = BotConfig::new_from_cli();
|
2024-03-15 00:40:22 +08:00
|
|
|
MainStatus::static_init(bot_config);
|
|
|
|
let bot_config = MainStatus::global_config();
|
|
|
|
|
|
|
|
py::init_py();
|
2024-03-12 00:16:12 +08:00
|
|
|
|
|
|
|
// 准备一个用于停止 socket 的变量
|
2024-03-16 14:04:30 +08:00
|
|
|
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() {
|
2024-03-16 14:04:30 +08:00
|
|
|
event!(Level::INFO, "启动 ica");
|
2024-03-12 00:16:12 +08:00
|
|
|
let config = bot_config.ica();
|
|
|
|
tokio::spawn(async move {
|
2024-03-16 14:04:30 +08:00
|
|
|
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
|
|
|
// 等待一个输入
|
2024-06-15 01:34:03 +08:00
|
|
|
event!(Level::INFO, "Press any key to exit");
|
2024-02-18 23:17:43 +08:00
|
|
|
let mut input = String::new();
|
|
|
|
std::io::stdin().read_line(&mut input).unwrap();
|
2024-02-21 21:42:27 +08:00
|
|
|
|
2024-03-16 14:04:30 +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-06-15 01:34:03 +08:00
|
|
|
event!(Level::INFO, "Disconnected");
|
2023-12-11 00:00:01 +08:00
|
|
|
}
|
2024-06-14 01:19:25 +08:00
|
|
|
|
|
|
|
#[allow(dead_code, unused_variables)]
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_macro() {
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
|
|
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
|
|
|
use rust_socketio::Payload;
|
|
|
|
|
|
|
|
/// 一个简单的例子
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct BotState(String);
|
|
|
|
|
|
|
|
/// 一个复杂一些的例子
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct BotState2 {
|
|
|
|
pub name: Arc<RwLock<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn some_event_with_state(payload: Payload, client: Client, state: Arc<BotState>) {
|
|
|
|
// do something with your state
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn some_state_change_event(payload: Payload, client: Client, state: Arc<BotState2>) {
|
|
|
|
if let Payload::Text(text) = payload {
|
|
|
|
if let Some(first_one) = text.first() {
|
|
|
|
let new_name = first_one.as_str().unwrap_or_default();
|
|
|
|
let old_name = state.name.read().await;
|
|
|
|
if new_name != *old_name {
|
|
|
|
// update your name here
|
|
|
|
*state.name.write().await = new_name.to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let state = Arc::new(BotState("hello".to_string()));
|
|
|
|
let state2 = Arc::new(BotState2 {
|
|
|
|
name: Arc::new(RwLock::new("hello".to_string())),
|
|
|
|
});
|
|
|
|
let socket = ClientBuilder::new("http://example.com")
|
|
|
|
.on("message", async_callback_with_state!(some_event_with_state, state))
|
|
|
|
.on("update_status", async_callback_with_state!(some_state_change_event, state2))
|
|
|
|
.connect()
|
|
|
|
.await;
|
|
|
|
}
|