Skip to content

Commit

Permalink
feat(starboard): handle message deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed May 28, 2024
1 parent f5e9bc6 commit d8a4372
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tokio = { version = "1.35.0", features = ["full"] }
toml = "0.8.8"
tracing = "0.1.40"
tracing-actix-web = "0.7.10"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[lints.clippy]
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ async fn event_handler(
starboard::handle(ctx, data, &message).await?;
}

FullEvent::MessageDelete {
deleted_message_id,
channel_id,
..
} => {
starboard::handle_deletion(ctx, data, deleted_message_id, channel_id).await?;
}

FullEvent::PresenceUpdate { new_data, .. } => {
if new_data.guild_id.map(|g| g.to_string()) == std::env::var("GUILD_ID").ok() {
let mut store = api::PRESENCE_STORE.write().unwrap();
Expand Down Expand Up @@ -118,6 +126,7 @@ async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(tracing_error::ErrorLayer::default())
.init();

#[cfg(debug_assertions)]
Expand Down
42 changes: 39 additions & 3 deletions src/starboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ fn channel_from_env(key: &str) -> Option<serenity::ChannelId> {
.and_then(|s| s.parse::<serenity::ChannelId>().ok())
}

#[tracing::instrument]
#[tracing::instrument(skip(http))]
async fn get_starboard_channel(
http: impl serenity::CacheHttp + std::fmt::Debug,
message_channel: serenity::ChannelId,
message_channel: &serenity::ChannelId,
) -> Result<Option<serenity::ChannelId>> {
let Some(message_channel) = message_channel.to_channel(&http).await?.guild() else {
return Ok(None);
Expand Down Expand Up @@ -147,7 +147,7 @@ pub async fn handle(
.as_ref()
.ok_or_eyre("no storage available for starboard features")?;

if let Some(starboard) = get_starboard_channel(&ctx, message.channel_id).await? {
if let Some(starboard) = get_starboard_channel(&ctx, &message.channel_id).await? {
let significant_reactions = get_significant_reactions(message);

if let Some(existing_starboard_message) = storage
Expand Down Expand Up @@ -214,3 +214,39 @@ pub async fn handle(

Ok(())
}

#[tracing::instrument(skip(ctx, data))]
pub async fn handle_deletion(
ctx: &serenity::Context,
data: &crate::Data,
deleted_message_id: &serenity::MessageId,
channel_id: &serenity::ChannelId,
) -> Result<()> {
if let Some(storage) = &data.storage {
if let Some(starboard_channel) = get_starboard_channel(&ctx, channel_id).await? {
if let Some(starboard_id) = storage
.get_starboard(&deleted_message_id.to_string())
.await?
{
debug!(
"Deleted starboard message {} for {} (source deleted)",
starboard_id, deleted_message_id
);

storage
.del_starboard(&deleted_message_id.to_string())
.await?;

ctx.http
.delete_message(
starboard_channel,
starboard_id.parse::<serenity::MessageId>()?,
None,
)
.await?;
}
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/utils/serenity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn unique_username(user: &serenity::User) -> String {
ret
}

#[tracing::instrument]
#[tracing::instrument(skip(ctx))]
pub async fn suppress_embeds(ctx: &serenity::Context, message: &serenity::Message) -> Result<()> {
use poise::futures_util::StreamExt as _;
use serenity::{EditMessage, Event};
Expand Down

0 comments on commit d8a4372

Please sign in to comment.