use std::{fmt::Debug, sync::Arc}; use euphoxide_client::{MultiClient, MultiClientConfig, MultiClientEvent, ServerConfig}; use jiff::Timestamp; use log::error; use tokio::sync::mpsc; use crate::command::Commands; #[non_exhaustive] pub struct Bot { pub server_config: ServerConfig, pub commands: Arc>, pub state: Arc, pub clients: MultiClient, pub start_time: Timestamp, } impl Bot { pub fn new_simple(commands: Commands, event_tx: mpsc::Sender) -> Self { Self::new( ServerConfig::default(), MultiClientConfig::default(), commands, (), event_tx, ) } } impl Bot { pub fn new( server_config: ServerConfig, clients_config: MultiClientConfig, commands: Commands, state: S, event_tx: mpsc::Sender, ) -> Self { Self { server_config, commands: Arc::new(commands), state: Arc::new(state), clients: MultiClient::new_with_config(clients_config, event_tx), start_time: Timestamp::now(), } } } impl Bot where S: Send + Sync + 'static, E: Debug + 'static, { pub fn handle_event(&self, event: MultiClientEvent) { let bot = self.clone(); tokio::task::spawn(async move { if let Err(err) = bot.commands.on_event(event, &bot).await { error!("while handling event: {err:#?}"); } }); } } impl Clone for Bot { fn clone(&self) -> Self { Self { server_config: self.server_config.clone(), commands: self.commands.clone(), state: self.state.clone(), clients: self.clients.clone(), start_time: self.start_time, } } }