Working karma handler

This commit is contained in:
nefrace 2022-10-15 00:30:05 +03:00
parent b9ea2f8caf
commit 0f3b289763
3 changed files with 17 additions and 32 deletions

View File

@ -1,4 +1,4 @@
use teloxide::{prelude::*, repl}; use teloxide::prelude::*;
pub mod commands; pub mod commands;
mod handlers; mod handlers;
@ -43,41 +43,30 @@ impl Godette {
} }
pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> { pub async fn message_dispatcher(bot: Bot, msg: Message) -> ResponseResult<()> {
let thanks = vec!["спасибо", "спс", "благодар очка"]; // Checking if it's a reply
let text = msg let message = msg.clone();
.text() let reply = message.reply_to_message();
.unwrap_or(msg.caption().unwrap_or_default()) match reply {
.to_string(); Some(reply) => return Godette::reply_dispatcher(bot, msg, reply.to_owned()).await,
for thank in thanks {
match text.to_lowercase().find(thank) {
Some(_id) => {
bot.send_message(msg.chat.id, "Не за что!").await?;
}
None => (), None => (),
} };
}
Ok(()) Ok(())
} }
pub async fn reply_dispatcher( pub async fn reply_dispatcher(bot: Bot, msg: Message, reply: Message) -> ResponseResult<()> {
bot: Bot,
msg: Message,
reply_to_message: Message,
) -> ResponseResult<()> {
let thanks = vec!["спасибо", "спс", "благодар очка"]; let thanks = vec!["спасибо", "спс", "благодар очка"];
println!("Working on reply");
let text = msg let text = msg
.text() .text()
.unwrap_or(msg.caption().unwrap_or_default()) .unwrap_or(msg.caption().unwrap_or_default())
.to_string(); .to_string();
println!("{:?}", msg); println!("{:?}", text);
println!("{:?}", bot);
println!("{:?}", reply_to_message);
for thank in thanks { for thank in thanks {
match text.to_lowercase().find(thank) { match text.to_lowercase().find(thank) {
Some(_id) => { 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 => (), None => (),
} }

View File

@ -67,12 +67,7 @@ pub async fn unwarn(bot: Bot, msg: Message) -> ResponseResult<Message> {
.await .await
} }
pub async fn karma( pub async fn karma(bot: &Bot, msg: &Message, reply: &Message, change: i8) -> ResponseResult<()> {
bot: &Bot,
msg: &Message,
reply: &Message,
change: i8,
) -> ResponseResult<Message> {
let giver = msg.from().unwrap(); let giver = msg.from().unwrap();
let reciever = reply.from().unwrap(); let reciever = reply.from().unwrap();
let change_text = match change { let change_text = match change {
@ -88,5 +83,6 @@ pub async fn karma(
); );
bot.send_message(msg.chat.id, text) bot.send_message(msg.chat.id, text)
.parse_mode(MarkdownV2) .parse_mode(MarkdownV2)
.await .await?;
Ok(())
} }

View File

@ -24,7 +24,7 @@ async fn main() {
.endpoint(Godette::admin_dispatcher), .endpoint(Godette::admin_dispatcher),
) )
// Replies // Replies
.branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher)) // .branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher))
// Messages // Messages
.branch( .branch(
dptree::filter(|msg: Message| { dptree::filter(|msg: Message| {