From 2e5c10288763b8d74d2b925b9f46f8c4667ce5d3 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 15 May 2022 11:54:42 -0600 Subject: [PATCH] Block users from using external emoji (Fixes #2) + Scan incoming messages for emoji, if they are from an external server they are blocked + clippy + fmt --- src/discord/commands.rs | 6 +----- src/discord/event_handler.rs | 18 ++++++++++++++++++ src/discord/helper.rs | 1 - src/error.rs | 2 +- src/imgur/mod.rs | 7 ++----- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/discord/commands.rs b/src/discord/commands.rs index 689c418..823c34d 100644 --- a/src/discord/commands.rs +++ b/src/discord/commands.rs @@ -474,11 +474,7 @@ async fn handle_errors( match command_result { Ok(()) => println!("Successfully processed command '{}'", command_name), Err(err) => { - let reply_msg = format!( - "Command '{}' returned an error. {}", - command_name, - err.to_string() - ); + let reply_msg = format!("Command '{}' returned an error. {}", command_name, err,); println!("{}", reply_msg); msg.reply(&ctx.http, reply_msg).await.unwrap(); } diff --git a/src/discord/event_handler.rs b/src/discord/event_handler.rs index c007024..8da9ce2 100644 --- a/src/discord/event_handler.rs +++ b/src/discord/event_handler.rs @@ -3,6 +3,7 @@ use serenity::client::{Context, EventHandler}; use serenity::http::AttachmentType; use serenity::model::channel::Message; use serenity::model::gateway::Ready; +use serenity::utils::parse_emoji; use crate::discord::helper::send_webhook_msg_to_player_channels; use crate::game::global_data::GlobalData; @@ -40,6 +41,23 @@ impl EventHandler for Handler { let guild = msg.guild(&ctx.cache).await.unwrap(); let user_msg = msg.content.clone(); + let re = regex::Regex::new(r"").unwrap(); + + for emoji_cap in re.captures_iter(&user_msg) { + if let Some(emoji) = parse_emoji(&emoji_cap[0]) { + if !msg + .guild(&ctx.cache) + .await + .unwrap() + .emojis + .contains_key(&emoji.id) + { + msg.reply(&ctx.http, "Your messages contains custom emojis from outside this guild and has been blocked.").await.unwrap(); + return; + } + } + } + let attachments: Vec = msg .attachments .iter() diff --git a/src/discord/helper.rs b/src/discord/helper.rs index 4a2e78f..8a66da6 100644 --- a/src/discord/helper.rs +++ b/src/discord/helper.rs @@ -114,7 +114,6 @@ pub async fn send_webhook_msg( msg: &str, attachment: Option>>, ) -> error::Result<()> { - let webhook = http.get_webhook(webhook_id).await?; webhook diff --git a/src/error.rs b/src/error.rs index 98b0c53..dfa03b1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,7 +30,7 @@ impl Display for WoxlfError { WoxlfError::DiscordIdParseError(e) => format!("Unable to parse player id {}", e), WoxlfError::GameNotInProgress => "A game is not currently in progress".to_string(), WoxlfError::HostWebhookError => "Unable to communicate to the host webhook".to_string(), - WoxlfError::ImgurError(err) => format!("Imgur module error: {}", err.to_string()), + WoxlfError::ImgurError(err) => format!("Imgur module error: {}", err), }; write!(f, "Woxlf Error: {}", msg) diff --git a/src/imgur/mod.rs b/src/imgur/mod.rs index 0d1f09c..ce0dfd2 100644 --- a/src/imgur/mod.rs +++ b/src/imgur/mod.rs @@ -17,7 +17,7 @@ impl From for ImgurError { impl Display for ImgurError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let msg = match self { - ImgurError::ReqwestError(err) => format!("Reqwest error: {}", err.to_string()), + ImgurError::ReqwestError(err) => format!("Reqwest error: {}", err), ImgurError::ImgurRequestError(msg) => format!("Imgur request error: {}", msg), }; @@ -56,10 +56,7 @@ pub async fn get_album_images(client_id: &str, album_hash: &str) -> Result