40 lines
1.1 KiB
Rust
40 lines
1.1 KiB
Rust
use crate::Bot;
|
|
use rand::seq::IteratorRandom;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Meme {
|
|
pub keyword: String,
|
|
pub ident: MemeIdent,
|
|
}
|
|
|
|
impl Meme {
|
|
/// checks if the meme should be triggered for the given message
|
|
pub fn matches(&self, msg: &str) -> bool {
|
|
let msg = msg.to_ascii_lowercase();
|
|
msg.starts_with(&self.keyword) &&
|
|
// msg must have one of allowed chars after keyword
|
|
msg.chars().nth(self.keyword.len()).map(|c|" ,.;:!?({-_".contains(c)).unwrap_or(true)
|
|
}
|
|
|
|
pub async fn get_meme(&self, bot: &Bot) -> anyhow::Result<Option<jm_client_core::api::Meme>> {
|
|
let memes = bot.jm_client.read().await.get_memes().await?;
|
|
match &self.ident {
|
|
MemeIdent::Id(i) => Ok(memes
|
|
.iter()
|
|
.find(|m| m.id.parse::<u32>().ok() == Some(*i))
|
|
.cloned()),
|
|
MemeIdent::RandomCat(c) => Ok(memes
|
|
.iter()
|
|
.filter(|m| &m.category == c)
|
|
.choose(&mut *bot.rng.lock().await)
|
|
.cloned()),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum MemeIdent {
|
|
RandomCat(String),
|
|
Id(u32),
|
|
}
|