34
src/bot/handlers/fun/chooser.rs
Normal file
34
src/bot/handlers/fun/chooser.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use grammers_client::{Client, Update};
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
|
||||||
|
use crate::{bot::handlers::Handler, utils::messages::get_message};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Chooser;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Handler for Chooser {
|
||||||
|
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
|
||||||
|
let Some(input_message) = get_message(update) else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let response = input_message
|
||||||
|
.text()
|
||||||
|
.strip_prefix(".c")
|
||||||
|
// Check if string is not empty.
|
||||||
|
.filter(|a| !a.is_empty())
|
||||||
|
// Split by commas.
|
||||||
|
.map(|text| text.trim().split(',').collect::<Vec<_>>())
|
||||||
|
// Choose random variant.
|
||||||
|
.and_then(|collected| collected.choose(&mut rand::rngs::OsRng).copied());
|
||||||
|
// It the string is chosen, reply to message with it.
|
||||||
|
if let Some(answer) = response {
|
||||||
|
input_message.reply(answer).await?;
|
||||||
|
} else {
|
||||||
|
input_message
|
||||||
|
.reply("Я не смог понять из чего мне выбирать. Попробуй ещё раз.")
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
pub mod blyaficator;
|
pub mod blyaficator;
|
||||||
|
pub mod chooser;
|
||||||
pub mod greeter;
|
pub mod greeter;
|
||||||
pub mod magic_ball;
|
pub mod magic_ball;
|
||||||
pub mod repeator;
|
pub mod repeator;
|
||||||
|
@ -23,8 +23,8 @@ use super::{
|
|||||||
weather_forecaster::WeatherForecaster,
|
weather_forecaster::WeatherForecaster,
|
||||||
},
|
},
|
||||||
fun::{
|
fun::{
|
||||||
blyaficator::Blyaficator, greeter::Greeter, magic_ball::MagicBall, repeator::Repeator,
|
blyaficator::Blyaficator, chooser::Chooser, greeter::Greeter, magic_ball::MagicBall,
|
||||||
rotator::Rotator,
|
repeator::Repeator, rotator::Rotator,
|
||||||
},
|
},
|
||||||
Handler,
|
Handler,
|
||||||
},
|
},
|
||||||
@ -147,6 +147,11 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".mb"], TextMatchMethod::IStartsWith)),
|
.add_filter(TextFilter(&[".mb"], TextMatchMethod::IStartsWith)),
|
||||||
|
// Random chooser.
|
||||||
|
FilteredHandler::new(Chooser)
|
||||||
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
|
.add_filter(SilentFilter)
|
||||||
|
.add_filter(TextFilter(&[".c"], TextMatchMethod::IStartsWith)),
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut errors_count = 0;
|
let mut errors_count = 0;
|
||||||
|
Reference in New Issue
Block a user