fix: check member role for excluded roles

This commit is contained in:
oSumAtrIX 2022-10-07 12:11:08 +02:00
parent 6e9f20df05
commit 6279a79132
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Duration, NaiveDateTime, Utc}; use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use regex::Regex; use regex::Regex;
use tracing::log::error;
use super::*; use super::*;
use crate::utils::bot::get_data_lock; use crate::utils::bot::get_data_lock;
@ -15,6 +16,7 @@ pub async fn auto_respond(ctx: &serenity::Context, new_message: &serenity::Messa
let data_lock = get_data_lock(ctx).await; let data_lock = get_data_lock(ctx).await;
let responses = &data_lock.read().await.configuration.message_responses; let responses = &data_lock.read().await.configuration.message_responses;
let message = &new_message.content;
for response in responses { for response in responses {
// check if the message was sent in a channel that is included in the responder // check if the message was sent in a channel that is included in the responder
@ -27,18 +29,17 @@ pub async fn auto_respond(ctx: &serenity::Context, new_message: &serenity::Messa
continue; continue;
} }
let excludes = &response.excludes;
// check if the message was sent by a user that is not excluded from the responder // check if the message was sent by a user that is not excluded from the responder
if excludes let excludes = &response.excludes;
.roles let member_roles = &new_message.member.as_ref().unwrap().roles;
if excludes.roles.iter().any(|&role_id| {
member_roles
.iter() .iter()
.any(|&role_id| role_id == new_message.author.id.0) .any(|&member_role| role_id == member_role.0)
{ }) {
continue; continue;
} }
let message = &new_message.content;
// check if the message does not match any of the excludes // check if the message does not match any of the excludes
if contains_match(&excludes.match_field, message) { if contains_match(&excludes.match_field, message) {
continue; continue;
@ -69,7 +70,7 @@ pub async fn auto_respond(ctx: &serenity::Context, new_message: &serenity::Messa
return; return;
} }
new_message if let Err(err) = new_message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
m.reference_message(new_message); m.reference_message(new_message);
@ -95,7 +96,13 @@ pub async fn auto_respond(ctx: &serenity::Context, new_message: &serenity::Messa
} }
}) })
.await .await
.expect("Could not reply to message author."); {
error!(
"Failed to reply to the message from {}. Error: {:?}",
new_message.author.tag(),
err
);
}
} }
} }
} }