diff --git a/src/bot_manager.rs b/src/bot_manager.rs index 4e05966..3cfa0eb 100644 --- a/src/bot_manager.rs +++ b/src/bot_manager.rs @@ -9,7 +9,7 @@ use teloxide::{ }; use crate::{ - bot_handler::script_handler, + bot_handler::{script_handler, BotHandler}, db::{bots::BotInstance, DbError, DB}, mongodb_storage::MongodbStorage, BotController, BotError, BotResult, @@ -47,11 +47,23 @@ pub async fn create_bot(db: &mut DB, token: &str) -> BotResult { Ok(bi) } -pub async fn start_bot(bi: BotInstance, db: &mut DB) -> BotResult { +pub async fn start_bot( + bi: BotInstance, + db: &mut DB, + plug_handlers: Vec, +) -> BotResult { let mut db = db.clone().with_name(bi.name.clone()); let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?; - let thread = spawn_bot_thread(controller.clone(), &mut db).await?; + let handler = script_handler(controller.rc.clone()); + // each handler will be added to dptree::entry() + let handler = plug_handlers + .into_iter() + // as well as the script handler at the end + .chain(std::iter::once(handler)) + .fold(dptree::entry(), |h, plug| h.branch(plug)); + + let thread = spawn_bot_thread(controller.clone(), &mut db, handler).await?; let info = BotInfo { name: bi.name.clone(), @@ -80,6 +92,7 @@ pub async fn start_bot(bi: BotInstance, db: &mut DB) -> BotResult { pub async fn spawn_bot_thread( bc: BotController, db: &mut DB, + handler: BotHandler, ) -> BotResult>> { let state_mgr = MongodbStorage::from_db(db, Json) .await @@ -87,8 +100,6 @@ pub async fn spawn_bot_thread( let thread = std::thread::spawn(move || -> BotResult<()> { let state_mgr = state_mgr; - let handler = script_handler(bc.rc); - let rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build()?; diff --git a/src/main.rs b/src/main.rs index 694bf65..3b24ff5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use db::application::Application; use db::bots::BotInstance; use db::callback_info::CallbackInfo; use db::message_forward::MessageForward; +use handlers::admin::admin_handler; use itertools::Itertools; use log::{error, info, warn}; use message_answerer::MessageAnswerer; @@ -184,7 +185,7 @@ async fn main() -> Result<(), Box> { let state_mgr = MongodbStorage::open(config.db_url.clone().as_ref(), "gongbot", Json).await?; for bi in BotInstance::get_all(&mut bc.db).await? { - let info = start_bot(bi, &mut bc.db).await?; + let info = start_bot(bi, &mut bc.db, vec![admin_handler()]).await?; println!("Started bot: {}", info.name); }