From 5cadb2bd7606ba3c568e4eff1d70b3e69bd537fe Mon Sep 17 00:00:00 2001 From: Akulij Date: Thu, 3 Apr 2025 04:57:46 +0900 Subject: [PATCH] rustfmt --- src/admin.rs | 46 +++++++++++++++++++++---------- src/db.rs | 29 ++++++++++++++------ src/db/models.rs | 2 -- src/main.rs | 71 +++++++++++++++++++++++++++--------------------- 4 files changed, 92 insertions(+), 56 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index ed94d33..53476be 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -1,16 +1,20 @@ -use teloxide::{dispatching::dialogue::GetChatId, payloads::SendMessageSetters, prelude::*, types::InputFile, utils::{command::BotCommands, render::RenderMessageTextHelper}}; +use teloxide::{ + dispatching::dialogue::GetChatId, + payloads::SendMessageSetters, + prelude::*, + types::InputFile, + utils::{command::BotCommands, render::RenderMessageTextHelper}, +}; -use crate::db::DB; use crate::LogMsg; +use crate::db::DB; // These are should not appear in /help #[derive(BotCommands, Clone)] #[command(rename_rule = "lowercase")] pub enum SecretCommands { /// Activate admin mode - Secret { - pass: String - }, + Secret { pass: String }, } #[derive(BotCommands, Clone)] @@ -34,20 +38,28 @@ pub async fn admin_command_handler( println!("MSG: {}", msg.html_text().unwrap()); match cmd { AdminCommands::MyId => { - bot.send_message(msg.chat.id, format!("Your ID is: {}", tguser.id)).log().await?; + bot.send_message(msg.chat.id, format!("Your ID is: {}", tguser.id)) + .log() + .await?; Ok(()) - }, + } AdminCommands::Pin => { if let Some(msg_to_pin) = msg.reply_to_message() { bot.pin_chat_message(msg.chat.id, msg_to_pin.id).await?; } else { - bot.send_message(msg.chat.id, "you need to reply to some message with this command").log().await?; + bot.send_message( + msg.chat.id, + "you need to reply to some message with this command", + ) + .log() + .await?; } Ok(()) - }, + } AdminCommands::Deop => { db.set_admin(tguser.id.0 as i64, false).await; - bot.send_message(msg.chat.id, "You are not an admin anymore").await?; + bot.send_message(msg.chat.id, "You are not an admin anymore") + .await?; Ok(()) } } @@ -59,20 +71,24 @@ pub async fn secret_command_handler( bot: Bot, msg: Message, cmd: SecretCommands, - admin_password: String + admin_password: String, ) -> Result<(), teloxide::RequestError> { println!("Admin Pass: {}", admin_password); - let user = db.get_or_init_user(msg.from.clone().unwrap().id.0 as i64).await; + let user = db + .get_or_init_user(msg.from.clone().unwrap().id.0 as i64) + .await; println!("MSG: {}", msg.html_text().unwrap()); match cmd { SecretCommands::Secret { pass } => { if user.is_admin == true { - bot.send_message(msg.from.unwrap().id, "You are an admin already").await?; + bot.send_message(msg.from.unwrap().id, "You are an admin already") + .await?; } else if pass == admin_password { db.set_admin(user.id, true).await; - bot.send_message(msg.from.unwrap().id, "You are admin now!").await?; + bot.send_message(msg.from.unwrap().id, "You are admin now!") + .await?; } Ok(()) - }, + } } } diff --git a/src/db.rs b/src/db.rs index 3087891..b946e02 100644 --- a/src/db.rs +++ b/src/db.rs @@ -8,14 +8,14 @@ use diesel::prelude::*; //use diesel::query_dsl::methods::FilterDsl; //use diesel::{prelude::*, r2d2::{ConnectionManager, Pool}}; use diesel_async::AsyncPgConnection; +use diesel_async::RunQueryDsl; use diesel_async::pooled_connection::AsyncDieselConnectionManager; use diesel_async::pooled_connection::bb8::Pool; -use diesel_async::RunQueryDsl; #[derive(Clone)] pub struct DB { //pool: Pool> - pool: diesel_async::pooled_connection::bb8::Pool + pool: diesel_async::pooled_connection::bb8::Pool, } impl DB { @@ -34,7 +34,11 @@ impl DB { use self::schema::users::dsl::*; let mut conn = self.pool.get().await.unwrap(); //let mut conn = AsyncPgConnection::establish(&std::env::var("DATABASE_URL").unwrap()).await.unwrap(); - users.filter(id.gt(0)).load::(&mut conn).await.unwrap() + users + .filter(id.gt(0)) + .load::(&mut conn) + .await + .unwrap() } pub async fn set_admin(&mut self, userid: i64, isadmin: bool) { @@ -45,20 +49,29 @@ impl DB { diesel::update(users) .filter(id.eq(userid)) .set(is_admin.eq(isadmin)) - .execute(connection).await.unwrap(); + .execute(connection) + .await + .unwrap(); } pub async fn get_or_init_user(&mut self, userid: i64) -> User { use self::schema::users::dsl::*; let connection = &mut self.pool.get().await.unwrap(); - let user = users.filter(id.eq(userid)).first::(connection).await.optional().unwrap(); + let user = users + .filter(id.eq(userid)) + .first::(connection) + .await + .optional() + .unwrap(); match user { Some(existing_user) => existing_user, - None => { - diesel::insert_into(users).values((id.eq(userid as i64), is_admin.eq(false))).get_result(connection).await.unwrap() - } + None => diesel::insert_into(users) + .values((id.eq(userid as i64), is_admin.eq(false))) + .get_result(connection) + .await + .unwrap(), } } } diff --git a/src/db/models.rs b/src/db/models.rs index a6f7de1..5cb96bd 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -3,7 +3,6 @@ #![allow(unused)] #![allow(clippy::all)] - use diesel::prelude::*; #[derive(Queryable, Debug)] #[diesel(table_name = users)] @@ -11,4 +10,3 @@ pub struct User { pub id: i64, pub is_admin: bool, } - diff --git a/src/main.rs b/src/main.rs index 3f6b6b8..fd43621 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,18 @@ -pub mod db; pub mod admin; +pub mod db; -use crate::db::DB; use crate::admin::{AdminCommands, admin_command_handler}; -use crate::admin::{secret_command_handler, SecretCommands}; +use crate::admin::{SecretCommands, secret_command_handler}; +use crate::db::DB; -use teloxide::{dispatching::dialogue::GetChatId, payloads::SendMessageSetters, prelude::*, types::InputFile, utils::{command::BotCommands, render::RenderMessageTextHelper}}; use envconfig::Envconfig; +use teloxide::{ + dispatching::dialogue::GetChatId, + payloads::SendMessageSetters, + prelude::*, + types::InputFile, + utils::{command::BotCommands, render::RenderMessageTextHelper}, +}; #[derive(Envconfig)] struct Config { @@ -39,7 +45,7 @@ impl LogMsg for ::SendMessage { } #[tokio::main] -async fn main() -> Result<(), Box>{ +async fn main() -> Result<(), Box> { dotenvy::dotenv()?; let config = Config::init_from_env()?; @@ -52,25 +58,28 @@ async fn main() -> Result<(), Box>{ }) .branch( Update::filter_message() - .branch( - dptree::entry().filter_command::().endpoint(user_command_handler) - ) - .branch( - dptree::entry().filter_command::() - .map(move || config.admin_password.clone()) - .endpoint(secret_command_handler) - ) - .branch( - dptree::entry().filter_async(async |msg: Message, mut db: DB| { - let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await; - user.is_admin - }).filter_command::().endpoint(admin_command_handler) - ) + .branch( + dptree::entry() + .filter_command::() + .endpoint(user_command_handler), + ) + .branch( + dptree::entry() + .filter_command::() + .map(move || config.admin_password.clone()) + .endpoint(secret_command_handler), + ) + .branch( + dptree::entry() + .filter_async(async |msg: Message, mut db: DB| { + let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await; + user.is_admin + }) + .filter_command::() + .endpoint(admin_command_handler), + ), ) - .branch( - Update::filter_message().endpoint(echo) - ) - ; + .branch(Update::filter_message().endpoint(echo)); Dispatcher::builder(bot, handler) .dependencies(dptree::deps![db]) @@ -94,11 +103,12 @@ async fn user_command_handler( UserCommands::Start => { bot.send_photo(msg.chat.id, InputFile::file_id("AgACAgIAAxkBAANRZ-2EJWUdkgwG4tfJfNwut4bssVkAAunyMRvTJ2FLn4FTtVdyfOoBAAMCAANzAAM2BA")).await?; Ok(()) - }, + } UserCommands::Help => { - bot.send_message(msg.chat.id, UserCommands::descriptions().to_string()).await?; + bot.send_message(msg.chat.id, UserCommands::descriptions().to_string()) + .await?; Ok(()) - }, + } _ => { bot.send_message(msg.chat.id, "Not yet implemented").await?; Ok(()) @@ -106,13 +116,12 @@ async fn user_command_handler( } } -async fn echo( - bot: Bot, - msg: Message, -) -> Result<(), teloxide::RequestError> { +async fn echo(bot: Bot, msg: Message) -> Result<(), teloxide::RequestError> { if let Some(photo) = msg.photo() { println!("File ID: {}", photo[0].file.id); } - bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into())).parse_mode(teloxide::types::ParseMode::Html).await?; + bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into())) + .parse_mode(teloxide::types::ParseMode::Html) + .await?; Ok(()) }