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
msg_refactor
Joey Hines 2022-05-15 11:54:42 -06:00
parent ea0be5c708
commit 2e5c102887
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
5 changed files with 22 additions and 12 deletions

View File

@ -474,11 +474,7 @@ async fn handle_errors(
match command_result { match command_result {
Ok(()) => println!("Successfully processed command '{}'", command_name), Ok(()) => println!("Successfully processed command '{}'", command_name),
Err(err) => { Err(err) => {
let reply_msg = format!( let reply_msg = format!("Command '{}' returned an error. {}", command_name, err,);
"Command '{}' returned an error. {}",
command_name,
err.to_string()
);
println!("{}", reply_msg); println!("{}", reply_msg);
msg.reply(&ctx.http, reply_msg).await.unwrap(); msg.reply(&ctx.http, reply_msg).await.unwrap();
} }

View File

@ -3,6 +3,7 @@ use serenity::client::{Context, EventHandler};
use serenity::http::AttachmentType; use serenity::http::AttachmentType;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use serenity::model::gateway::Ready; use serenity::model::gateway::Ready;
use serenity::utils::parse_emoji;
use crate::discord::helper::send_webhook_msg_to_player_channels; use crate::discord::helper::send_webhook_msg_to_player_channels;
use crate::game::global_data::GlobalData; use crate::game::global_data::GlobalData;
@ -40,6 +41,23 @@ impl EventHandler for Handler {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
let user_msg = msg.content.clone(); let user_msg = msg.content.clone();
let re = regex::Regex::new(r"<a?:.+:\d+>").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<AttachmentType> = msg let attachments: Vec<AttachmentType> = msg
.attachments .attachments
.iter() .iter()

View File

@ -114,7 +114,6 @@ pub async fn send_webhook_msg(
msg: &str, msg: &str,
attachment: Option<Vec<AttachmentType<'_>>>, attachment: Option<Vec<AttachmentType<'_>>>,
) -> error::Result<()> { ) -> error::Result<()> {
let webhook = http.get_webhook(webhook_id).await?; let webhook = http.get_webhook(webhook_id).await?;
webhook webhook

View File

@ -30,7 +30,7 @@ impl Display for WoxlfError {
WoxlfError::DiscordIdParseError(e) => format!("Unable to parse player id {}", e), WoxlfError::DiscordIdParseError(e) => format!("Unable to parse player id {}", e),
WoxlfError::GameNotInProgress => "A game is not currently in progress".to_string(), WoxlfError::GameNotInProgress => "A game is not currently in progress".to_string(),
WoxlfError::HostWebhookError => "Unable to communicate to the host webhook".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) write!(f, "Woxlf Error: {}", msg)

View File

@ -17,7 +17,7 @@ impl From<reqwest::Error> for ImgurError {
impl Display for ImgurError { impl Display for ImgurError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let msg = match self { 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), 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<Vec<I
let client = Client::new(); let client = Client::new();
let res = client let res = client
.get(format!( .get(format!("https://api.imgur.com/3/album/{}", album_hash))
"https://api.imgur.com/3/album/{}",
album_hash
))
.header("Authorization", format!("Client-ID {}", client_id)) .header("Authorization", format!("Client-ID {}", client_id))
.send() .send()
.await?; .await?;