From 6dfe9b839dd63901f3fc0a54bec8061845d8d100 Mon Sep 17 00:00:00 2001 From: Akulij Date: Sat, 31 May 2025 10:45:34 +0500 Subject: [PATCH] fix logic: move bot insertion to db to /deploy command, instead of function in bot_manager --- src/admin.rs | 28 +++++++++++++++++++++++----- src/bot_manager.rs | 11 ----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index 709c624..10fbba2 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -1,5 +1,3 @@ -use std::fmt::format; - use itertools::Itertools; use teloxide::{ prelude::*, @@ -7,8 +5,8 @@ use teloxide::{ }; use crate::{ - bot_manager::create_bot, - db::{CallDB, DB}, + bot_manager::DEFAULT_SCRIPT, + db::{bots::BotInstance, CallDB, DB}, BotResult, }; use crate::{BotDialogue, LogMsg, State}; @@ -162,7 +160,27 @@ pub async fn admin_command_handler( Ok(()) } AdminCommands::Deploy { token } => { - let bot_instance = create_bot(&mut db, &token).await?; + let bot_instance = { + let botnew = Bot::new(&token); + let name = match botnew.get_me().await { + Ok(me) => me.username().to_string(), + Err(teloxide::RequestError::Api(teloxide::ApiError::InvalidToken)) => { + bot.send_message(msg.chat.id, "Error: bot token is invalid") + .await?; + return Ok(()); + } + Err(err) => { + return Err(err.into()); + } + }; + + let bi = + BotInstance::new(name.clone(), token.to_string(), DEFAULT_SCRIPT.to_string()) + .store(&mut db) + .await?; + + bi + }; bot.send_message( msg.chat.id, diff --git a/src/bot_manager.rs b/src/bot_manager.rs index 6c95623..be324ec 100644 --- a/src/bot_manager.rs +++ b/src/bot_manager.rs @@ -169,17 +169,6 @@ async fn script_handler_gen(c: BotController, plug_handlers: Vec) -> handler } -pub async fn create_bot(db: &mut DB, token: &str) -> BotResult { - let bot = Bot::new(token); - let name = bot.get_me().await?.username().to_string(); - - let bi = BotInstance::new(name.clone(), token.to_string(), DEFAULT_SCRIPT.to_string()) - .store(db) - .await?; - - Ok(bi) -} - pub async fn start_bot( bi: BotInstance, db: &mut DB,