Initial commit.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2023-02-20 01:20:41 +04:00
parent 9a04acd753
commit faae5e4898
39 changed files with 3420 additions and 2 deletions

10
src/bot/handlers/base.rs Normal file
View 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);

View 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(())
}
}

View 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(())
}
}

View File

@ -0,0 +1,2 @@
pub mod get_chat_id;
pub mod help;

View 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(())
}
}

View 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(())
}
}

View File

@ -0,0 +1,2 @@
pub mod blyaficator;
pub mod greeter;

5
src/bot/handlers/mod.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod base;
pub mod basic;
pub mod fun;
pub use base::Handler;