Skip to content

Commit

Permalink
Add config for embed suppress delay
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDaedalus committed Apr 14, 2024
1 parent 8306198 commit 37e2929
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Config {
pub reply_cache_size: usize,
#[serde(default)]
pub ignored_users: Vec<Id<UserMarker>>,
#[serde(default)]
pub suppress_delay_millis: u64,
#[serde(rename = "pass")]
pub passes: Vec<Pass>,
}
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,6 +63,7 @@ async fn shard_loop(state: Arc<State>, mut shard: Shard) -> Result<(), anyhow::E
/// [JoinHandle]: tokio::task::JoinHandle
fn suppress_embeds_deferred(
rest: &Client,
delay: u64,
channel_id: Id<ChannelMarker>,
message_id: Id<MessageMarker>,
) -> tokio::task::JoinHandle<()> {
Expand All @@ -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}");
}
Expand All @@ -91,7 +97,12 @@ async fn dispatch_event(state: Arc<State>, 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);
Expand Down Expand Up @@ -121,7 +132,12 @@ async fn dispatch_event(state: Arc<State>, 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 {
Expand Down

0 comments on commit 37e2929

Please sign in to comment.