TODO: reply_handler does not work

This commit is contained in:
Nefrace 2022-10-14 14:20:45 +03:00
parent b03d76c494
commit b9ea2f8caf
3 changed files with 64 additions and 6 deletions

View File

@ -1,4 +1,4 @@
use teloxide::prelude::*; use teloxide::{prelude::*, repl};
pub mod commands; pub mod commands;
mod handlers; mod handlers;
@ -43,16 +43,45 @@ 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!["спасибо", "спс", "благодар очка"];
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();
match text.to_lowercase().find("спасибо") {
Some(_id) => { for thank in thanks {
bot.send_message(msg.chat.id, "Не за что!").await?; 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(()) Ok(())
} }
} }

View File

@ -66,3 +66,27 @@ pub async fn unwarn(bot: Bot, msg: Message) -> ResponseResult<Message> {
bot.send_message(msg.chat.id, "Это разбан".to_string()) bot.send_message(msg.chat.id, "Это разбан".to_string())
.await .await
} }
pub async fn karma(
bot: &Bot,
msg: &Message,
reply: &Message,
change: i8,
) -> ResponseResult<Message> {
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
}

View File

@ -11,16 +11,21 @@ async fn main() {
let bot = Godette::new(); let bot = Godette::new();
let handler = Update::filter_message() let handler = Update::filter_message()
// User commands
.branch( .branch(
dptree::entry() dptree::entry()
.filter_command::<commands::Command>() .filter_command::<commands::Command>()
.endpoint(Godette::commands_dispatcher), .endpoint(Godette::commands_dispatcher),
) )
// Admin commands
.branch( .branch(
dptree::entry() dptree::entry()
.filter_command::<commands::AdminCommand>() .filter_command::<commands::AdminCommand>()
.endpoint(Godette::admin_dispatcher), .endpoint(Godette::admin_dispatcher),
) )
// Replies
.branch(Message::filter_reply_to_message().endpoint(Godette::reply_dispatcher))
// Messages
.branch( .branch(
dptree::filter(|msg: Message| { dptree::filter(|msg: Message| {
msg.from() msg.from()