Skip to content

Commit

Permalink
Add portside feature, opposite of starboard
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Apr 16, 2024
1 parent 692582a commit 554114f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
mod boredom;
mod collective;
mod confess;
mod portside;
mod rp_commands;
mod utility;

Expand All @@ -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},
};

Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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");

Expand Down
83 changes: 83 additions & 0 deletions src/portside.rs
Original file line number Diff line number Diff line change
@@ -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<Http>,
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(())
}

0 comments on commit 554114f

Please sign in to comment.