use super::{Bot, BotBuilder, BotConfig, MemoryStateStore, Router, Scheduler, StateStore};
use crate::client::Client;
use crate::events::Result;
pub struct BotApp {
router: Router,
scheduler: Scheduler,
state: Box<dyn StateStore>,
config: BotConfig,
}
impl Default for BotApp {
fn default() -> Self {
Self {
router: Router::new(),
scheduler: Scheduler::new(),
state: Box::new(MemoryStateStore::new()),
config: BotConfig::default(),
}
}
}
impl BotApp {
pub fn new() -> Self {
Self::default()
}
pub fn with_router(mut self, router: Router) -> Self {
self.router = router;
self
}
pub fn with_scheduler(mut self, scheduler: Scheduler) -> Self {
self.scheduler = scheduler;
self
}
pub fn with_state_store(mut self, store: impl StateStore + 'static) -> Self {
self.state = Box::new(store);
self
}
pub fn with_config(mut self, config: BotConfig) -> Self {
self.config = config;
self
}
pub fn into_bot(self, client: Client) -> Bot {
BotBuilder::new(client)
.with_router(self.router)
.with_scheduler(self.scheduler)
.with_boxed_state_store(self.state)
.with_config(self.config)
.build()
}
pub fn run_sync(self, client: Client) -> Result<()> {
let mut bot = self.into_bot(client);
bot.run()
}
#[cfg(feature = "async")]
pub fn into_async_bot(self, client: crate::async_api::AsyncClient) -> super::AsyncBot {
super::AsyncBotBuilder::new(client)
.with_router(self.router)
.with_scheduler(self.scheduler)
.with_boxed_state_store(self.state)
.with_config(super::AsyncBotConfig)
.build()
}
#[cfg(feature = "async")]
pub async fn run_async(self, client: crate::async_api::AsyncClient) -> Result<()> {
let mut bot = self.into_async_bot(client);
bot.run().await
}
}