summaryrefslogtreecommitdiff
path: root/src/handler/reactboard.rs
blob: 36f83610e65058e681028ebbbae335fffb022473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::Error;
use crate::{settings::Settings, utils};
use log::*;
use poise::serenity_prelude::{Context, Message, MessageReaction, Reaction};

pub async fn handle(ctx: &Context, reaction: &Reaction, settings: &Settings) -> Result<(), Error> {
	let msg = match reaction.message(&ctx.http).await {
		Ok(msg) => msg,
		Err(why) => {
			warn!("couldn't get message of reaction! {}", why);
			return Err(Box::new(why));
		}
	};

	if let Some(matched) = msg
		.clone()
		.reactions
		.into_iter()
		.find(|r| r.reaction_type == reaction.emoji)
	{
		send_to_reactboard(ctx, &matched, &msg, settings).await?;
	} else {
		warn!(
			"couldn't find any matching reactions for {} in {}",
			reaction.emoji.as_data(),
			msg.id
		)
	}

	Ok(())
}

async fn send_to_reactboard(
	ctx: &Context,
	reaction: &MessageReaction,
	msg: &Message,
	settings: &Settings,
) -> Result<(), Error> {
	if !settings.can_use_reaction(reaction) {
		info!("reaction {} can't be used!", reaction.reaction_type);
		return Ok(());
	}

	if reaction.count == settings.reactboard_requirement.unwrap_or(5) {
		let embed = utils::resolve_message_to_embed(ctx, msg).await;

		settings
			.reactboard_target
			.send_message(&ctx.http, |m| {
				m.allowed_mentions(|am| am.empty_parse())
					.content(format!(
						"{} **#{}**",
						reaction.reaction_type, reaction.count
					))
					.set_embed(embed)
			})
			.await?;
	} else {
		info!(
			"not putting message {} on reactboard, not enough reactions",
			msg.id
		)
	}

	Ok(())
}