Files
s3bot/src/bot/handlers/fun/greeter.rs
2023-02-21 19:56:36 +00:00

42 lines
1.1 KiB
Rust

use async_trait::async_trait;
use grammers_client::{Client, Update};
use rand::seq::IteratorRandom;
use crate::bot::handlers::base::Handler;
lazy_static::lazy_static! {
static ref GREETINGS: &'static [&'static str] = &[
"Привет!",
"Добрый день!",
"Здравствуйте.",
"Приетствую.",
"Доброго времени суток.",
];
}
/// Greeter just replies to greeting messages.
#[derive(Clone)]
pub struct Greeter;
#[async_trait]
impl Handler for Greeter {
async fn react(&self, client: &Client, update: &Update) -> anyhow::Result<()> {
let Update::NewMessage(message) = update else {return Ok(())};
// Check if chat has less than 100 participants.
let participants = client.iter_participants(message.chat()).total().await?;
if participants >= 100 {
return Ok(());
}
// Choose random greeting from the list of greetings.
let reply_text = GREETINGS.iter().choose(&mut rand::thread_rng()).copied();
if let Some(text) = reply_text {
message.reply(text).await?;
}
Ok(())
}
}