From 0f3b28976307535745c6e51507df96b3604db492 Mon Sep 17 00:00:00 2001 From: nefrace Date: Sat, 15 Oct 2022 00:30:05 +0300 Subject: [PATCH] Working karma handler --- src/godette.rs | 37 +++++++++++++------------------------ src/godette/handlers.rs | 10 +++------- src/main.rs | 2 +- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/godette.rs b/src/godette.rs index c3d1319..938dadf 100644 --- a/src/godette.rs +++ b/src/godette.rs @@ -1,4 +1,4 @@ -use teloxide::{prelude::*, repl}; +use teloxide::prelude::*; pub mod commands; mod handlers; @@ -43,41 +43,30 @@ impl Godette { } pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> { - let thanks = vec!["спасибо", "спс", "благодар очка"]; - let text = msg - .text() - .unwrap_or(msg.caption().unwrap_or_default()) - .to_string(); + // Checking if it's a reply + let message = msg.clone(); + let reply = message.reply_to_message(); + match reply { + Some(reply) => return Godette::reply_dispatcher(bot, msg, reply.to_owned()).await, + None => (), + }; - for thank in thanks { - match text.to_lowercase().find(thank) { - Some(_id) => { - bot.send_message(msg.chat.id, "Не за что!").await?; - } - None => (), - } - } Ok(()) } - pub async fn reply_dispatcher( - bot: Bot, - msg: Message, - reply_to_message: Message, - ) -> ResponseResult<()> { + pub async fn reply_dispatcher(bot: Bot, msg: Message, reply: Message) -> ResponseResult<()> { let thanks = vec!["спасибо", "спс", "благодар очка"]; + println!("Working on reply"); let text = msg .text() .unwrap_or(msg.caption().unwrap_or_default()) .to_string(); - println!("{:?}", msg); - println!("{:?}", bot); - println!("{:?}", reply_to_message); + println!("{:?}", text); for thank in thanks { match text.to_lowercase().find(thank) { Some(_id) => { - handlers::karma(&bot, &msg, &reply_to_message, 1).await?; - () + println!("It's a thanks!"); + return handlers::karma(&bot, &msg, &reply, 1).await; } None => (), } diff --git a/src/godette/handlers.rs b/src/godette/handlers.rs index e232e1f..5e8aa3d 100644 --- a/src/godette/handlers.rs +++ b/src/godette/handlers.rs @@ -67,12 +67,7 @@ pub async fn unwarn(bot: Bot, msg: Message) -> ResponseResult { .await } -pub async fn karma( - bot: &Bot, - msg: &Message, - reply: &Message, - change: i8, -) -> ResponseResult { +pub async fn karma(bot: &Bot, msg: &Message, reply: &Message, change: i8) -> ResponseResult<()> { let giver = msg.from().unwrap(); let reciever = reply.from().unwrap(); let change_text = match change { @@ -88,5 +83,6 @@ pub async fn karma( ); bot.send_message(msg.chat.id, text) .parse_mode(MarkdownV2) - .await + .await?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 09d2fd1..abf0b9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ async fn main() { .endpoint(Godette::admin_dispatcher), ) // Replies - .branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher)) + // .branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher)) // Messages .branch( dptree::filter(|msg: Message| {