From 1e6d671feb13d7edbdf29afd5e731484d44e88d8 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 2 Mar 2024 02:53:54 +0200 Subject: [PATCH] rearrange file structure for bot commands --- src/bot.rs | 2 ++ src/bot/bot.rs | 40 +++------------------------------------- src/bot/dl.rs | 32 ++++++++++++++++++++++++++++++++ src/bot/types.rs | 8 ++++++++ src/db.rs | 9 ++++----- 5 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 src/bot/dl.rs create mode 100644 src/bot/types.rs diff --git a/src/bot.rs b/src/bot.rs index 283a1a5..3ae8ce4 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,2 +1,4 @@ pub mod bot; +pub mod dl; pub mod sanitize; +pub mod types; diff --git a/src/bot/bot.rs b/src/bot/bot.rs index eaafd33..86bc818 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -8,20 +8,11 @@ use std::time::Duration; use teloxide::dispatching::dialogue; use teloxide::dispatching::dialogue::InMemStorage; use teloxide::dispatching::UpdateHandler; -use teloxide::types::InputFile; use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; use tracing::{event, Level}; -use crate::dl::delete_if_exists; -use crate::dl::download; - -use crate::db; - -type State = (); -type MyDialogue = Dialogue>; - -type HandlerErr = Box; -type HandlerResult = Result<(), HandlerErr>; +use super::dl::cmd_download; +use super::types::*; fn parse_env(name: &str) -> T where @@ -83,37 +74,12 @@ enum Command { Download(String), } -async fn cmd_test(bot: Bot, msg: Message, db: SqlitePool) -> HandlerResult { +async fn cmd_test(bot: Bot, msg: Message, _db: SqlitePool) -> HandlerResult { bot.send_message(msg.chat.id, "test response").await?; - - Ok(()) -} - -async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult { - let output_path = match download(url.as_str()).await { - Ok(path) => path, - Err(e) => { - event!(Level::ERROR, "{}", e.to_string()); - bot.send_message(msg.chat.id, e.to_string()).await?; - return Ok(()); - } - }; - - if let Err(e) = bot - .send_video(msg.chat.id, InputFile::file(&output_path)) - .await - { - delete_if_exists(&output_path); - return Err(Box::new(e)); - } Ok(()) } -async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult { - bot_download(bot, msg, url).await -} - async fn handle_message(_bot: Bot, _dialogue: MyDialogue, _msg: Message) -> HandlerResult { Ok(()) } diff --git a/src/bot/dl.rs b/src/bot/dl.rs new file mode 100644 index 0000000..52a69b6 --- /dev/null +++ b/src/bot/dl.rs @@ -0,0 +1,32 @@ +use teloxide::prelude::*; +use teloxide::types::InputFile; +use tracing::{event, Level}; + +use super::types::HandlerResult; +use crate::dl::delete_if_exists; +use crate::dl::download; + +async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult { + let output_path = match download(url.as_str()).await { + Ok(path) => path, + Err(e) => { + event!(Level::ERROR, "{}", e.to_string()); + bot.send_message(msg.chat.id, e.to_string()).await?; + return Ok(()); + } + }; + + if let Err(e) = bot + .send_video(msg.chat.id, InputFile::file(&output_path)) + .await + { + delete_if_exists(&output_path); + return Err(Box::new(e)); + } + + Ok(()) +} + +pub async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult { + bot_download(bot, msg, url).await +} diff --git a/src/bot/types.rs b/src/bot/types.rs new file mode 100644 index 0000000..4a4eaed --- /dev/null +++ b/src/bot/types.rs @@ -0,0 +1,8 @@ +use teloxide::dispatching::dialogue::InMemStorage; +use teloxide::prelude::*; + +pub type State = (); +pub type MyDialogue = Dialogue>; + +pub type HandlerErr = Box; +pub type HandlerResult = Result<(), HandlerErr>; diff --git a/src/db.rs b/src/db.rs index b7fcf27..9e400fc 100644 --- a/src/db.rs +++ b/src/db.rs @@ -3,7 +3,6 @@ use sqlx::{Sqlite, SqlitePool}; use super::util::make_database_url; - #[derive(sqlx::FromRow)] pub struct User { pub id: i64, @@ -21,7 +20,7 @@ pub struct Chat { pub tg_id: i64, pub username: Option, pub title: String, - pub can_download: i64 + pub can_download: i64, } #[derive(sqlx::FromRow)] @@ -30,7 +29,7 @@ pub struct Link { pub domain: String, pub path: Option, pub download_allowed: i64, - pub auto_download: i64 + pub auto_download: i64, } #[derive(sqlx::FromRow)] @@ -39,7 +38,7 @@ pub struct Request { pub requested_by: i64, pub approved_by: Option, pub message: Option, - pub is_approved: i64 + pub is_approved: i64, } pub async fn db_init() -> SqlitePool { @@ -54,4 +53,4 @@ pub async fn db_init() -> SqlitePool { sqlx::migrate!().run(&db).await.unwrap(); db -} \ No newline at end of file +}