From a9205b6298652b0c2e61ff0abbdb29da3ace440c Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sat, 15 Oct 2022 01:18:36 +0300 Subject: [PATCH] KarmaTrigger type for both "+" and "-" triggers --- src/godette.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/godette.rs b/src/godette.rs index 938dadf..4d57758 100644 --- a/src/godette.rs +++ b/src/godette.rs @@ -8,6 +8,19 @@ pub struct Godette { pub bot: Bot, } +pub struct KarmaTrigger { + pub text: String, + pub value: i8, +} +impl KarmaTrigger { + pub fn new(text: &str, value: i8) -> KarmaTrigger { + KarmaTrigger { + text: String::from(text), + value, + } + } +} + impl Godette { pub fn new() -> Godette { Godette { @@ -55,18 +68,27 @@ impl Godette { } pub async fn reply_dispatcher(bot: Bot, msg: Message, reply: Message) -> ResponseResult<()> { - let thanks = vec!["спасибо", "спс", "благодар очка"]; + let triggers = vec![ + KarmaTrigger::new("спс", 1), + KarmaTrigger::new("спасибо", 1), + KarmaTrigger::new("+", 1), + KarmaTrigger::new("благодарю", 1), + KarmaTrigger::new("пасиб", 1), + KarmaTrigger::new("-", -1), + KarmaTrigger::new("👍", 1), + KarmaTrigger::new("👎", -1), + ]; println!("Working on reply"); let text = msg .text() .unwrap_or(msg.caption().unwrap_or_default()) .to_string(); println!("{:?}", text); - for thank in thanks { - match text.to_lowercase().find(thank) { + for trigger in triggers { + match text.to_lowercase().find(&trigger.text) { Some(_id) => { println!("It's a thanks!"); - return handlers::karma(&bot, &msg, &reply, 1).await; + return handlers::karma(&bot, &msg, &reply, trigger.value).await; } None => (), }