diff --git a/src/main.rs b/src/main.rs index 43729ce..58bf7be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod boredom; mod collective; mod confess; +mod portside; mod rp_commands; mod utility; @@ -35,6 +36,7 @@ use crate::{ boredom::{check_for_boredom, check_for_boredom_acknowledgment, BoredomTracker}, collective::collective, confess::confess, + portside::check_portside_reactions, rp_commands::{bite, boop, gnaw, meow, murder, pat}, }; @@ -142,8 +144,17 @@ async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleS ], event_handler: |ctx, event, _framework, _data| { Box::pin(async move { - if let FullEvent::Message { new_message } = event { - check_for_boredom_acknowledgment(ctx, new_message).await?; + match event { + FullEvent::Message { new_message } => { + check_for_boredom_acknowledgment(ctx, new_message).await?; + } + FullEvent::ReactionAdd { add_reaction } => { + check_portside_reactions(ctx, add_reaction).await?; + } + FullEvent::ReactionRemove { removed_reaction } => { + check_portside_reactions(ctx, removed_reaction).await?; + } + _ => (), } Ok(()) @@ -175,10 +186,13 @@ async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleS }) }) .build(); - let client = ClientBuilder::new(discord_token, GatewayIntents::non_privileged()) - .framework(framework) - .await - .map_err(shuttle_runtime::CustomError::new)?; + let client = ClientBuilder::new( + discord_token, + GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT, + ) + .framework(framework) + .await + .map_err(shuttle_runtime::CustomError::new)?; info!("Constructed client"); diff --git a/src/portside.rs b/src/portside.rs new file mode 100644 index 0000000..1e87aa9 --- /dev/null +++ b/src/portside.rs @@ -0,0 +1,83 @@ +use poise::serenity_prelude::{ + futures::StreamExt, CacheHttp, ChannelId, Color, CreateEmbed, CreateEmbedAuthor, + CreateEmbedFooter, CreateMessage, EditMessage, Http, Reaction, +}; +use tracing::warn; + +use crate::Error; + +pub(super) async fn check_portside_reactions( + ctx: impl CacheHttp + AsRef, + reaction: &Reaction, +) -> Result<(), Error> { + let message = reaction.message(&ctx).await?; + let tomato_reactions = message + .reactions + .into_iter() + .find(|message_rection| message_rection.reaction_type.unicode_eq("🍅")); + + if let Some(tomato_reactions) = tomato_reactions { + let tomato_reactions_count = tomato_reactions.count; + + if tomato_reactions_count > 1 { + let portside_channel = ChannelId::new(1229587493100327003); + let mut portside_messages = portside_channel.messages_iter(&ctx).boxed(); + let portside_message_content = + format!("**🍅 {tomato_reactions_count} <#{}>**", message.channel_id,); + let message_id_string = message.id.to_string(); + + while let Some(portside_message) = portside_messages.next().await { + if let Ok(mut portside_message) = portside_message { + match portside_message.embeds.get(0) { + Some(embed) => match &embed.footer { + Some(footer) => { + if footer.text == message_id_string { + portside_message + .edit( + &ctx, + EditMessage::new().content(portside_message_content), + ) + .await?; + + return Ok(()); + } + } + None => warn!("Embed in #portside is missing its footer"), + }, + None => { + warn!( + "Message in #portside does not have embed, ID: {}", + portside_message.id, + ); + } + } + } + } + + portside_channel + .send_message( + &ctx, + CreateMessage::new() + .content(portside_message_content) + .embed( + CreateEmbed::new() + .color(Color::RED) + .author( + CreateEmbedAuthor::new(&message.author.name).icon_url( + message + .author + .avatar_url() + .unwrap_or(message.author.default_avatar_url()), + ), + ) + .description(message.content) + .footer(CreateEmbedFooter::new(message_id_string)) + .timestamp(message.timestamp), + ), + ) + .await?; + } + } + + Ok(()) +}