Skip to content

Commit

Permalink
Only ignore messages that are from our own configured webhook
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Maddock <maddock.evan@vivaldi.net>
  • Loading branch information
EbonJaeger committed Mar 27, 2021
1 parent a5b94cd commit 156a2e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/discord.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::RootConfig;
use crate::errors::{Error, Result};
use crate::listener::{Listener, LogTailer, Webserver};
use crate::listener::{split_webhook_url, Listener, LogTailer, Webserver};
use crate::markdown;
use rcon::Connection;
use serenity::{
Expand Down Expand Up @@ -51,8 +51,12 @@ impl EventHandler for Handler {
let bot = ctx.cache.current_user().await;

// Ignore messages that are from ourselves
if msg.author.id == bot.id || msg.webhook_id.is_some() {
debug!("event_handler:message: skipping message from ourselves or webhook");
let webhook_url = self.config_lock.read().await.webhook_url();
let webhook_id = split_webhook_url(&webhook_url).unwrap_or_default().0;
if msg.author.id == bot.id
|| (msg.webhook_id.is_some() && msg.webhook_id.unwrap() == webhook_id)
{
debug!("event_handler:message: skipping message from ourselves or our webhook");
return;
}

Expand Down
16 changes: 15 additions & 1 deletion src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,21 @@ async fn send_to_discord(
}

/// Use Regex to split the configured webhook URL into an ID and a token.
fn split_webhook_url(url: &str) -> Option<(u64, &str)> {
/// If the input url doesn't match the regex, [None] will be returned. No
/// validation is done to see if the webhook URL is actually a valid and
/// active Discord webhook.
///
/// # Examples
///
/// ```rust
/// let webhook_url = String::from("https://discord.com/api/webhooks/12345/67890");
/// let webhook_parts = split_webhook_url(&webhook_url);
///
/// assert!(webhook_parts.is_some());
/// assert_eq!(webhook_parts.unwrap().0, 12345);
/// assert_eq!(webhook_parts.unwrap().1, 67890);
/// ```
pub fn split_webhook_url(url: &str) -> Option<(u64, &str)> {
// Only compile the regex once, since this is expensive
lazy_static! {
static ref WEBHOOK_REGEX: Regex =
Expand Down

0 comments on commit 156a2e9

Please sign in to comment.