From c8f197bf58f198e1cb70d6ea3ae49fc51434b718 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 28 Mar 2022 10:17:22 +0800 Subject: Replace Arc with DbPool::clone. --- src/bot.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/bot.rs') diff --git a/src/bot.rs b/src/bot.rs index e46ed85..d60bc6f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::ops::Deref; -use std::sync::Arc; use teloxide::{prelude2::*, types::MessageKind, utils::command::BotCommand}; use tracing::{debug, error, info}; @@ -17,7 +15,7 @@ enum Command { Start, } -async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc) +async fn answer(bot: AutoSend, message: Message, command: Command, db: DbPool) -> Result<(), Box> { debug!("Answering telegram message: {:?}", message); match command { @@ -36,7 +34,6 @@ async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc< return Ok(()); // ignore messages from channel } let from = msg.from.unwrap(); - let db = db.deref(); let chat_id = message.chat.id; let user = User { id: from.id as u64, @@ -44,7 +41,7 @@ async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc< token: token::generate(), chat_id: chat_id as u64, }; - if let Err(why) = database::create_user(db, &user).await { + if let Err(why) = database::create_user(&db, &user).await { if format!("{:?}", why).contains("UNIQUE constraint failed") { info!("User exists: {}", user); } else { @@ -54,7 +51,7 @@ async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc< info!("Created user {}.", user); } let message = - match database::get_user_by_chat_id(db, chat_id as u64).await { + match database::get_user_by_chat_id(&db, chat_id as u64).await { Ok(u) => match u { Some(user) => format!("Your token is `{}`. Treat it as a secret!", user.token), _ => String::from("Error: cannot fetch token.") @@ -73,7 +70,7 @@ async fn answer(bot: AutoSend, message: Message, command: Command, db: Arc< Ok(()) } -pub async fn repl(bot: Bot, db: Arc) { +pub async fn repl(bot: Bot, db: database::DbPool) { teloxide::repls2::commands_repl( bot.auto_send(), move |bot, msg, cmd| -- cgit v1.2.3