From 37e29291936ef9c428b21c962f157d04389cc717 Mon Sep 17 00:00:00 2001 From: Daedalus <16168171+RedDaedalus@users.noreply.github.com> Date: Sat, 13 Apr 2024 19:42:25 -0600 Subject: [PATCH] Add config for embed suppress delay --- config.example.toml | 2 ++ src/config.rs | 2 ++ src/main.rs | 20 ++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/config.example.toml b/config.example.toml index cd3e071..4295a22 100644 --- a/config.example.toml +++ b/config.example.toml @@ -4,6 +4,8 @@ token = "your bot's token" reply_cache_size = 3 # User IDs the bot won't respond to. ignored_users = [] +# The number of milliseconds to wait before suppressing embeds -- can help reduce flashing. +suppress_delay_millis = 200 # Passes: each pass gets run independently and all of its matched URLs are appended # to the bot's output. diff --git a/src/config.rs b/src/config.rs index 2f0e416..16fc534 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,8 @@ pub struct Config { pub reply_cache_size: usize, #[serde(default)] pub ignored_users: Vec>, + #[serde(default)] + pub suppress_delay_millis: u64, #[serde(rename = "pass")] pub passes: Vec, } diff --git a/src/main.rs b/src/main.rs index 173adfe..1e0fef8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::fs; use std::future::IntoFuture; use std::sync::{Arc, RwLock}; +use std::time::Duration; use twilight_gateway::{Event, EventTypeFlags, Intents, Shard, ShardId, StreamExt as _}; use twilight_http::Client; @@ -62,6 +63,7 @@ async fn shard_loop(state: Arc, mut shard: Shard) -> Result<(), anyhow::E /// [JoinHandle]: tokio::task::JoinHandle fn suppress_embeds_deferred( rest: &Client, + delay: u64, channel_id: Id, message_id: Id, ) -> tokio::task::JoinHandle<()> { @@ -72,6 +74,10 @@ fn suppress_embeds_deferred( .into_future(); tokio::spawn(async move { + if delay > 0 { + tokio::time::sleep(Duration::from_millis(delay)).await; + } + if let Err(e) = f.await { tracing::error!(error = ?e, "Error suppressing embeds on {channel_id}/{message_id}"); } @@ -91,7 +97,12 @@ async fn dispatch_event(state: Arc, event: Event) -> Result<(), anyhow::E // If the unfurler has an embed cached, embeds will be included if !message.embeds.is_empty() { - suppress_embeds_deferred(&state.rest, message.channel_id, message.id); + suppress_embeds_deferred( + &state.rest, + state.config.suppress_delay_millis, + message.channel_id, + message.id, + ); } let token = state.replies.write().unwrap().file_pending(message.id); @@ -121,7 +132,12 @@ async fn dispatch_event(state: Arc, event: Event) -> Result<(), anyhow::E // Suppress embeds the unfurler provided lazily if message.embeds.is_some_and(|embeds| !embeds.is_empty()) { tracing::info!("Unfurler triggered on {:?}, suppressing...", entry); - suppress_embeds_deferred(&state.rest, message.channel_id, message.id); + suppress_embeds_deferred( + &state.rest, + state.config.suppress_delay_millis, + message.channel_id, + message.id, + ); }; if let CacheEntry::Filled(reply_id) = entry {