|
| 1 | +use color_eyre::eyre::Result; |
1 | 2 | use std::sync::Arc;
|
2 | 3 | use walle_core::action::Action;
|
3 |
| -use walle_core::alt::TracingHandler; |
4 |
| -use walle_core::config::AppConfig; |
| 4 | +use walle_core::alt::{ColoredAlt, TracingHandler}; |
| 5 | +use walle_core::config::{AppConfig, WebSocketServer}; |
5 | 6 | use walle_core::event::Event;
|
6 | 7 | use walle_core::obc::AppOBC;
|
7 | 8 | use walle_core::resp::Resp;
|
8 |
| -use walle_core::OneBot; |
| 9 | +use walle_core::WalleResult; |
| 10 | +use walle_core::{ActionHandler, EventHandler, OneBot}; |
| 11 | +use tokio::task::JoinHandle; |
9 | 12 |
|
10 | 13 | #[tokio::main]
|
11 |
| -async fn main() { |
| 14 | +async fn main() -> Result<()> { |
12 | 15 | color_eyre::install()?;
|
13 | 16 | init_log();
|
14 |
| - AppOBC::new(), |
15 |
| - TracingHandler::<Event, Action, Resp>::default(), |
16 |
| - )); |
17 |
| - ob.start(AppConfig::default(), (), true).await.unwrap(); |
18 |
| - // ob.wait_all().await; |
| 17 | + let ob = Arc::new(OneBot::new(AppOBC::new(), MyEventHandler)); |
| 18 | + let mut config = AppConfig::default(); |
| 19 | + config.websocket_rev.clear(); |
| 20 | + let ws_server = WebSocketServer { |
| 21 | + host: std::net::IpAddr::from([0, 0, 0, 0]), |
| 22 | + port: 2456, |
| 23 | + access_token: None, |
| 24 | + }; |
| 25 | + config.websocket_rev.push(ws_server); |
| 26 | + ob.start(config, (), true).await.unwrap(); |
| 27 | + tokio::spawn(async { |
| 28 | + let ob = ob; |
| 29 | + ob.handle_action(action) |
| 30 | + }); |
| 31 | + ob.wait_all().await; |
19 | 32 | ob.shutdown(true).await.ok();
|
| 33 | + Ok(()) |
| 34 | +} |
| 35 | +#[derive(Debug)] |
| 36 | +struct MyEventHandler; |
| 37 | +impl EventHandler<Event, Action, Resp> for MyEventHandler { |
| 38 | + type Config = (); |
| 39 | + async fn start<AH, EH>( |
| 40 | + &self, |
| 41 | + _ob: &Arc<OneBot<AH, EH>>, |
| 42 | + _config: Self::Config, |
| 43 | + ) -> WalleResult<Vec<JoinHandle<()>>> |
| 44 | + where |
| 45 | + AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 46 | + EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 47 | + { |
| 48 | + Ok(vec![]) |
| 49 | + } |
| 50 | + async fn call<AH, EH>( |
| 51 | + &self, |
| 52 | + event: Event, |
| 53 | + _ob: &Arc<OneBot<AH, EH>>, |
| 54 | + ) -> walle_core::WalleResult<()> |
| 55 | + where |
| 56 | + AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 57 | + EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 58 | + { |
| 59 | + tracing::info!("{}", event.colored_alt()); |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + async fn shutdown(&self) { |
| 63 | + tracing::info!("Shutting down TracingHandler") |
| 64 | + } |
| 65 | + |
| 66 | + fn on_onebot_connect<AH, EH>( |
| 67 | + &self, |
| 68 | + _ob: &Arc<OneBot<AH, EH>>, |
| 69 | + ) -> impl futures_util::Future<Output = WalleResult<()>> |
| 70 | + where |
| 71 | + AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 72 | + EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 73 | + { |
| 74 | + async { Ok(()) } |
| 75 | + } |
| 76 | + |
| 77 | + fn on_onebot_disconnect<AH, EH>( |
| 78 | + &self, |
| 79 | + _ob: &Arc<OneBot<AH, EH>>, |
| 80 | + ) -> impl futures_util::Future<Output = WalleResult<()>> |
| 81 | + where |
| 82 | + AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 83 | + EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static, |
| 84 | + { |
| 85 | + async { Ok(()) } |
| 86 | + } |
| 87 | +} |
| 88 | + |
20 | 89 | fn init_log() {
|
21 | 90 | use tracing::Level;
|
22 | 91 | use tracing_subscriber::prelude::*;
|
|
0 commit comments