10
src/bot/handlers/base.rs
Normal file
10
src/bot/handlers/base.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use async_trait::async_trait;
|
||||
use dyn_clone::DynClone;
|
||||
use grammers_client::{Client, Update};
|
||||
|
||||
#[async_trait]
|
||||
pub trait Handler: DynClone + Sync + Send {
|
||||
async fn react(&self, client: &Client, update: &Update) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
dyn_clone::clone_trait_object!(Handler);
|
17
src/bot/handlers/basic/get_chat_id.rs
Normal file
17
src/bot/handlers/basic/get_chat_id.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use grammers_client::{Client, Update};
|
||||
|
||||
use crate::{bot::handlers::Handler, utils::messages::get_message};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GetChatId;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Handler for GetChatId {
|
||||
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
|
||||
let message = get_message(update);
|
||||
if let Some(msg) = message {
|
||||
msg.reply(msg.chat().id().to_string()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
17
src/bot/handlers/basic/help.rs
Normal file
17
src/bot/handlers/basic/help.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use grammers_client::{Client, Update};
|
||||
|
||||
use crate::bot::handlers::Handler;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Help;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Handler for Help {
|
||||
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
|
||||
let Update::NewMessage(message) = update else {return Ok(())};
|
||||
|
||||
message.reply("Хелпа").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
2
src/bot/handlers/basic/mod.rs
Normal file
2
src/bot/handlers/basic/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod get_chat_id;
|
||||
pub mod help;
|
39
src/bot/handlers/fun/blyaficator.rs
Normal file
39
src/bot/handlers/fun/blyaficator.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use crate::{
|
||||
bot::handlers::Handler,
|
||||
utils::{inter_join::RandomIntersperse, messages::get_message},
|
||||
};
|
||||
use grammers_client::{Client, Update};
|
||||
|
||||
const BLYA_WORDS: &[&str] = &[", бля,", ", сука,", ", ёбаный рот,", ", охуеть конечно,"];
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Blyaficator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Handler for Blyaficator {
|
||||
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
|
||||
if let Some(message) = get_message(update) {
|
||||
let maybe_blyaficated = message.text().strip_prefix(".bl").map(|stripped| {
|
||||
stripped
|
||||
// Trim string after removing prefix, to
|
||||
// remove leading spaces.
|
||||
.trim()
|
||||
// split by commas
|
||||
.split(',')
|
||||
// choose random strings from BLYA_WORDS
|
||||
// and insert them between splitted strings.
|
||||
.random_itersperse(BLYA_WORDS, &mut rand::thread_rng())
|
||||
// Collect it back to vec
|
||||
.collect::<Vec<_>>()
|
||||
// Creating one string with all words concatenated.
|
||||
.join("")
|
||||
});
|
||||
|
||||
// If the text was blyaficated we send it as a reply.
|
||||
if let Some(blyficated) = maybe_blyaficated {
|
||||
message.reply(blyficated).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
31
src/bot/handlers/fun/greeter.rs
Normal file
31
src/bot/handlers/fun/greeter.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use async_trait::async_trait;
|
||||
use grammers_client::{Client, Update};
|
||||
use rand::seq::IteratorRandom;
|
||||
|
||||
use crate::bot::handlers::base::Handler;
|
||||
|
||||
#[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(());
|
||||
}
|
||||
|
||||
let reply_text = ["Привет!", "Добрый день!", "Здравствуйте.", "Приетствую"]
|
||||
.into_iter()
|
||||
.choose(&mut rand::thread_rng());
|
||||
|
||||
if let Some(text) = reply_text {
|
||||
message.reply(text).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
2
src/bot/handlers/fun/mod.rs
Normal file
2
src/bot/handlers/fun/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod blyaficator;
|
||||
pub mod greeter;
|
5
src/bot/handlers/mod.rs
Normal file
5
src/bot/handlers/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
pub mod base;
|
||||
pub mod basic;
|
||||
pub mod fun;
|
||||
|
||||
pub use base::Handler;
|
Reference in New Issue
Block a user