discord-kurante/src/passive/desu.rs

81 lines
2.7 KiB
Rust
Raw Normal View History

2020-05-02 07:06:43 +00:00
use futures::future::BoxFuture;
2020-05-02 05:04:51 +00:00
use lazy_static::lazy_static;
2020-05-02 17:45:50 +00:00
use log::trace;
2020-05-02 07:30:16 +00:00
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;
2020-05-02 02:17:53 +00:00
use regex::Regex;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::{Context, EventHandler};
2020-05-02 07:06:43 +00:00
lazy_static! {
2020-05-02 17:45:50 +00:00
#[derive(Debug)]
static ref FUFUFU_REGEX: Regex = Regex::new("(?i:fu){3,}").unwrap();
#[derive(Debug)]
2020-05-02 07:30:16 +00:00
static ref DESU_REGEX: Regex = Regex::new("(?i:desu)|(?:です)").unwrap();
2020-05-02 07:06:43 +00:00
}
2020-05-02 07:30:16 +00:00
pub(crate) struct DesuResponder;
2020-05-02 07:06:43 +00:00
#[async_trait]
2020-05-02 07:30:16 +00:00
impl EventHandler for DesuResponder {
2020-05-02 07:06:43 +00:00
async fn message(&self, ctx: Context, message: Message) {
2020-05-02 07:30:16 +00:00
let content = message.content_safe(ctx.clone()).await;
2020-05-02 17:45:50 +00:00
let fufufu_match = FUFUFU_REGEX.is_match(&content);
let desu_match = DESU_REGEX.is_match(&content) && thread_rng().gen::<f32>() < 0.1;
if fufufu_match || desu_match {
trace!(
"Responding to `{}` because one of the following matched: fufufu={}, desu={}",
content,
fufufu_match,
desu_match
);
2020-05-02 07:30:16 +00:00
let random_action =
DESU_ACTIONS.choose(&mut thread_rng()).unwrap().clone()(ctx, message);
2020-05-02 07:06:43 +00:00
random_action.await.unwrap();
}
}
}
2020-05-02 02:17:53 +00:00
2020-05-02 07:06:43 +00:00
type DesuAction<'r, T> = &'r (dyn Fn(Context, Message) -> T + Sync);
const DESU_ACTIONS: &[DesuAction<BoxFuture<Result<Message, serenity::Error>>>] = &[
&|ctx, msg| Box::pin(msg.channel_id.say(ctx, "です。")),
&|ctx, msg| Box::pin(msg.channel_id.say(ctx, "desu~")),
&|ctx, msg| Box::pin(msg.channel_id.say(ctx, "desu.")),
&|ctx, msg| {
Box::pin(msg.channel_id.say(
ctx,
r#"
2020-05-02 02:17:53 +00:00
```
      
          
            
           
              
             
              
              
             
```"#,
2020-05-02 07:06:43 +00:00
))
},
&|ctx, msg| {
// https://imgur.com/a/yOb5n
// One day.
Box::pin(
msg.channel_id
.say(ctx, "https://www.youtube.com/watch?v=60mLvBWOMb4"),
)
},
&|ctx, msg| {
Box::pin(msg.channel_id.send_message(ctx, |m| {
m.add_file(serenity::http::AttachmentType::Bytes {
data: std::borrow::Cow::from(&include_bytes!("../../res/desu.jpg")[..]),
2020-05-02 07:06:43 +00:00
filename: "desu.jpg".to_string(),
})
}))
},
2020-05-02 02:17:53 +00:00
];