From b9ea2f8caf848ae4de32c60e8bc129e9f01d40b8 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Fri, 14 Oct 2022 14:20:45 +0300 Subject: [PATCH] TODO: reply_handler does not work --- src/godette.rs | 41 +++++++++++++++++++++++++++++++++++------ src/godette/handlers.rs | 24 ++++++++++++++++++++++++ src/main.rs | 5 +++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/godette.rs b/src/godette.rs index 7045829..c3d1319 100644 --- a/src/godette.rs +++ b/src/godette.rs @@ -1,4 +1,4 @@ -use teloxide::prelude::*; +use teloxide::{prelude::*, repl}; pub mod commands; mod handlers; @@ -43,16 +43,45 @@ 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(); - match text.to_lowercase().find("спасибо") { - Some(_id) => { - bot.send_message(msg.chat.id, "Не за что!").await?; + + for thank in thanks { + match text.to_lowercase().find(thank) { + Some(_id) => { + bot.send_message(msg.chat.id, "Не за что!").await?; + } + None => (), } - None => (), - }; + } + Ok(()) + } + + pub async fn reply_dispatcher( + bot: Bot, + msg: Message, + reply_to_message: Message, + ) -> ResponseResult<()> { + let thanks = vec!["спасибо", "спс", "благодар очка"]; + let text = msg + .text() + .unwrap_or(msg.caption().unwrap_or_default()) + .to_string(); + println!("{:?}", msg); + println!("{:?}", bot); + println!("{:?}", reply_to_message); + for thank in thanks { + match text.to_lowercase().find(thank) { + Some(_id) => { + handlers::karma(&bot, &msg, &reply_to_message, 1).await?; + () + } + None => (), + } + } Ok(()) } } diff --git a/src/godette/handlers.rs b/src/godette/handlers.rs index 7aca68f..e232e1f 100644 --- a/src/godette/handlers.rs +++ b/src/godette/handlers.rs @@ -66,3 +66,27 @@ pub async fn unwarn(bot: Bot, msg: Message) -> ResponseResult { bot.send_message(msg.chat.id, "Это разбан".to_string()) .await } + +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 { + 1 => "повысил", + -1 => "понизил", + _ => "изменил", + }; + let text = format!( + "*{}* {} карму *{}*", + escape(&giver.first_name), + change_text, + escape(&reciever.first_name) + ); + bot.send_message(msg.chat.id, text) + .parse_mode(MarkdownV2) + .await +} diff --git a/src/main.rs b/src/main.rs index 2da409d..09d2fd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,16 +11,21 @@ async fn main() { let bot = Godette::new(); let handler = Update::filter_message() + // User commands .branch( dptree::entry() .filter_command::() .endpoint(Godette::commands_dispatcher), ) + // Admin commands .branch( dptree::entry() .filter_command::() .endpoint(Godette::admin_dispatcher), ) + // Replies + .branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher)) + // Messages .branch( dptree::filter(|msg: Message| { msg.from()