Skip to content
Snippets Groups Projects
react.rs 1.11 KiB
Newer Older
Stephen D's avatar
Stephen D committed
use crate::handlers::LineHandler;
use phf::phf_map;
use serenity::async_trait;
use serenity::model::channel::{Message, ReactionType};
use serenity::prelude::*;
use std::str::FromStr;

static EMOJI_MAP: phf::Map<&'static str, &'static str> = phf_map! {
	"cat" => "🐈",
	"chicken" => "🐔",
	"spaghetti" => "🍝",
	"dog" => "🐕",
	"bot" => "🤖",
	"mango" => "🥭",
	"banana" => "🍌",
Stephen D's avatar
Stephen D committed
	"bee" => "🐝",
	"hose" => "🐎",
Stephen D's avatar
Stephen D committed
};

pub struct ReactHandler;

#[async_trait]
impl LineHandler for ReactHandler {
	async fn message(&self, ctx: &Context, msg: &Message) {
		for (key, value) in EMOJI_MAP.entries() {
			let msg_lower = format!(" {} ", msg.content.to_lowercase());
			if msg_lower.contains(&format!(" {} ", key))
				|| msg_lower.contains(&format!(" {}s ", key))
			{
				let reaction_type = match ReactionType::from_str(value) {
					Ok(x) => x,
					Err(x) => {
						println!("Could not react: {}", x);
						return;
					}
				};
				if let Err(e) = msg.react(&ctx, reaction_type).await {
					println!("Error reacting: {}", e);
				}
			}
		}
	}
}

impl Default for ReactHandler {
	fn default() -> Self {
		Self
	}
}