plug provided handlers in start_bot
This commit is contained in:
parent
866a028de1
commit
67ad3c2acd
@ -9,7 +9,7 @@ use teloxide::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bot_handler::script_handler,
|
bot_handler::{script_handler, BotHandler},
|
||||||
db::{bots::BotInstance, DbError, DB},
|
db::{bots::BotInstance, DbError, DB},
|
||||||
mongodb_storage::MongodbStorage,
|
mongodb_storage::MongodbStorage,
|
||||||
BotController, BotError, BotResult,
|
BotController, BotError, BotResult,
|
||||||
@ -47,11 +47,23 @@ pub async fn create_bot(db: &mut DB, token: &str) -> BotResult<BotInstance> {
|
|||||||
Ok(bi)
|
Ok(bi)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start_bot(bi: BotInstance, db: &mut DB) -> BotResult<BotInfo> {
|
pub async fn start_bot(
|
||||||
|
bi: BotInstance,
|
||||||
|
db: &mut DB,
|
||||||
|
plug_handlers: Vec<BotHandler>,
|
||||||
|
) -> BotResult<BotInfo> {
|
||||||
let mut db = db.clone().with_name(bi.name.clone());
|
let mut db = db.clone().with_name(bi.name.clone());
|
||||||
let controller = BotController::with_db(db.clone(), &bi.token, &bi.script).await?;
|
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 {
|
let info = BotInfo {
|
||||||
name: bi.name.clone(),
|
name: bi.name.clone(),
|
||||||
@ -80,6 +92,7 @@ pub async fn start_bot(bi: BotInstance, db: &mut DB) -> BotResult<BotInfo> {
|
|||||||
pub async fn spawn_bot_thread(
|
pub async fn spawn_bot_thread(
|
||||||
bc: BotController,
|
bc: BotController,
|
||||||
db: &mut DB,
|
db: &mut DB,
|
||||||
|
handler: BotHandler,
|
||||||
) -> BotResult<JoinHandle<BotResult<()>>> {
|
) -> BotResult<JoinHandle<BotResult<()>>> {
|
||||||
let state_mgr = MongodbStorage::from_db(db, Json)
|
let state_mgr = MongodbStorage::from_db(db, Json)
|
||||||
.await
|
.await
|
||||||
@ -87,8 +100,6 @@ pub async fn spawn_bot_thread(
|
|||||||
let thread = std::thread::spawn(move || -> BotResult<()> {
|
let thread = std::thread::spawn(move || -> BotResult<()> {
|
||||||
let state_mgr = state_mgr;
|
let state_mgr = state_mgr;
|
||||||
|
|
||||||
let handler = script_handler(bc.rc);
|
|
||||||
|
|
||||||
let rt = tokio::runtime::Builder::new_current_thread()
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|||||||
@ -16,6 +16,7 @@ use db::application::Application;
|
|||||||
use db::bots::BotInstance;
|
use db::bots::BotInstance;
|
||||||
use db::callback_info::CallbackInfo;
|
use db::callback_info::CallbackInfo;
|
||||||
use db::message_forward::MessageForward;
|
use db::message_forward::MessageForward;
|
||||||
|
use handlers::admin::admin_handler;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use message_answerer::MessageAnswerer;
|
use message_answerer::MessageAnswerer;
|
||||||
@ -184,7 +185,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let state_mgr = MongodbStorage::open(config.db_url.clone().as_ref(), "gongbot", Json).await?;
|
let state_mgr = MongodbStorage::open(config.db_url.clone().as_ref(), "gongbot", Json).await?;
|
||||||
|
|
||||||
for bi in BotInstance::get_all(&mut bc.db).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);
|
println!("Started bot: {}", info.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user