use super::{PassiveResponder, PassiveResponse}; use crate::util::debug_say_owned; use futures::future::BoxFuture; use lazy_static::lazy_static; use log::trace; use rand::seq::SliceRandom; use rand::thread_rng; use rand::Rng; use regex::Regex; use serenity::model::channel::Message; use serenity::prelude::Context; lazy_static! { #[derive(Debug)] static ref FUFUFU_REGEX: Regex = Regex::new("(?i:fu){3,}").unwrap(); #[derive(Debug)] static ref DESU_REGEX: Regex = Regex::new("(?i:desu)|(?:です)").unwrap(); } pub(crate) struct DesuResponder; impl PassiveResponder for DesuResponder { fn get_message( &self, received_msg: String, ctx: Context, message: Message, ) -> Option> { let fufufu_match = FUFUFU_REGEX.is_match(&received_msg); let desu_match = DESU_REGEX.is_match(&received_msg) && thread_rng().gen::() < 0.1; if fufufu_match || desu_match { trace!( "Responding to `{}` because one of the following matched: fufufu={}, desu={}", received_msg, fufufu_match, desu_match ); Some(DESU_ACTIONS.choose(&mut thread_rng()).unwrap().clone()( ctx, message, )) } else { None } } } type DesuAction<'r, T> = &'r (dyn Fn(Context, Message) -> T + Sync); const DESU_ACTIONS: &[DesuAction, serenity::Error>>>] = &[ &|ctx, msg| Box::pin(debug_say_owned(msg, ctx, "です。")), &|ctx, msg| Box::pin(debug_say_owned(msg, ctx, "desu~")), &|ctx, msg| Box::pin(debug_say_owned(msg, ctx, "desu.")), &|ctx, msg| { Box::pin(debug_say_owned( msg, ctx, r#" ``` ででででででででででで      すす      ででで     すすすすすすすすす     でで  でで      すす    でで   でで     すすす   でで           す す   でで           すすす    でで           すす     でで          すす      でで        すす ```"#, )) }, &|ctx, msg| { // https://imgur.com/a/yOb5n // One day. Box::pin(debug_say_owned( msg, ctx, "https://www.youtube.com/watch?v=60mLvBWOMb4", )) }, &|ctx, msg| Box::pin(send_desu_image(ctx, msg)), ]; async fn send_desu_image(ctx: Context, msg: Message) -> Result, serenity::Error> { Ok(Some( msg.channel_id .send_message(ctx, |m| { m.add_file(serenity::http::AttachmentType::Bytes { data: std::borrow::Cow::from(&include_bytes!("../../res/desu.jpg")[..]), filename: "desu.jpg".to_string(), }) }) .await?, )) }