From fe2a863bc5ee3641a10e975801cb2ae87df22465 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Tue, 21 Feb 2023 22:53:31 +0000
Subject: [PATCH] Added .rl, fixed some messages, removed liveness probe.

---
 helm/templates/deployment.yaml      |  4 ---
 src/bot/handlers/fun/blyaficator.rs |  6 ++--
 src/bot/handlers/fun/mod.rs         |  1 +
 src/bot/handlers/fun/rotator.rs     | 49 +++++++++++++++++++++++++++++
 src/bot/main.rs                     |  7 ++++-
 5 files changed, 60 insertions(+), 7 deletions(-)
 create mode 100644 src/bot/handlers/fun/rotator.rs

diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml
index d6d2c0b..66b9b17 100644
--- a/helm/templates/deployment.yaml
+++ b/helm/templates/deployment.yaml
@@ -37,10 +37,6 @@ spec:
             - name: http
               containerPort: {{ default 8000 .Values.env.BOT_SERVER_PORT }}
               protocol: TCP
-          livenessProbe:
-            httpGet:
-              path: /health
-              port: http
           readinessProbe:
             httpGet:
               path: /health
diff --git a/src/bot/handlers/fun/blyaficator.rs b/src/bot/handlers/fun/blyaficator.rs
index c8660f8..e6e41cf 100644
--- a/src/bot/handlers/fun/blyaficator.rs
+++ b/src/bot/handlers/fun/blyaficator.rs
@@ -2,7 +2,7 @@ use crate::{
     bot::handlers::Handler,
     utils::{inter_join::RandomIntersperse, messages::get_message},
 };
-use grammers_client::{Client, Update};
+use grammers_client::{Client, InputMessage, Update};
 
 const BLYA_WORDS: &[&str] = &[", бля,", ", сука,", ", ёбаный рот,", ", охуеть конечно,"];
 
@@ -32,7 +32,9 @@ impl Handler for Blyaficator {
 
             // If the text was blyaficated we send it as a reply.
             if let Some(blyficated) = maybe_blyaficated {
-                message.reply(blyficated).await?;
+                message
+                    .reply(InputMessage::from(blyficated).silent(true))
+                    .await?;
             }
         }
         Ok(())
diff --git a/src/bot/handlers/fun/mod.rs b/src/bot/handlers/fun/mod.rs
index 6645e92..8d24276 100644
--- a/src/bot/handlers/fun/mod.rs
+++ b/src/bot/handlers/fun/mod.rs
@@ -1,2 +1,3 @@
 pub mod blyaficator;
 pub mod greeter;
+pub mod rotator;
diff --git a/src/bot/handlers/fun/rotator.rs b/src/bot/handlers/fun/rotator.rs
new file mode 100644
index 0000000..db4511c
--- /dev/null
+++ b/src/bot/handlers/fun/rotator.rs
@@ -0,0 +1,49 @@
+use std::time::Duration;
+
+use grammers_client::{types::Message, Client, InputMessage, Update};
+
+use crate::{bot::handlers::Handler, utils::messages::get_message};
+
+#[derive(Clone)]
+pub struct Rotator;
+
+async fn rotator(replied_message: Message, text: String) {
+    let mut skip = 1;
+    let start = std::time::SystemTime::now();
+    let mut current = std::time::SystemTime::now();
+    while current
+        .duration_since(start)
+        .map(|dur| dur.as_secs())
+        .unwrap_or(100)
+        < 10
+    {
+        let rotated = text
+            .chars()
+            .cycle()
+            .skip(skip)
+            .take(text.len())
+            .collect::<String>();
+        skip += 1;
+        if skip == rotated.len() {
+            skip = 0;
+        }
+        replied_message.edit(rotated).await.ok();
+        current = std::time::SystemTime::now();
+        tokio::time::sleep(Duration::from_millis(200)).await;
+    }
+    replied_message.delete().await.ok();
+}
+
+#[async_trait::async_trait]
+impl Handler for Rotator {
+    async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
+        let Some(message) = get_message(update) else{
+            return Ok(());
+        };
+        if let Some(text) = message.text().strip_prefix(".rl").map(str::trim) {
+            let replied_message = message.reply(InputMessage::from(text).silent(true)).await?;
+            tokio::spawn(rotator(replied_message, text.to_string()));
+        }
+        Ok(())
+    }
+}
diff --git a/src/bot/main.rs b/src/bot/main.rs
index c05bb8d..2459f03 100644
--- a/src/bot/main.rs
+++ b/src/bot/main.rs
@@ -20,7 +20,7 @@ use super::{
             get_chat_id::GetChatId,
             help::Help,
         },
-        fun::{blyaficator::Blyaficator, greeter::Greeter},
+        fun::{blyaficator::Blyaficator, greeter::Greeter, rotator::Rotator},
         Handler,
     },
 };
@@ -104,12 +104,17 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
             .add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)),
         // Make бля fun again.
         FilteredHandler::new(Blyaficator)
+            .add_filter(SilentFilter)
             .add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)),
         // Handler for converting currecies.
         FilteredHandler::new(CurrencyConverter::new()?)
             .add_filter(SilentFilter)
             .add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
             .add_filter(CurrencyTextFilter),
+        // Simlpe rotator.
+        FilteredHandler::new(Rotator)
+            .add_filter(SilentFilter)
+            .add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)),
     ];
 
     loop {
-- 
GitLab