feat: handle mute when rejoining the server

This commit is contained in:
oSumAtrIX 2022-08-25 23:15:09 +02:00
parent 23bc1aa306
commit 042fc8c466
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
6 changed files with 64 additions and 16 deletions

View File

@ -98,21 +98,22 @@ pub async fn mute(
if let Err(add_role_result) = member.add_role(&ctx.discord().http, mute_role_id).await { if let Err(add_role_result) = member.add_role(&ctx.discord().http, mute_role_id).await {
Some(Error::from(add_role_result)) Some(Error::from(add_role_result))
} else { } else {
// accumulate all roles to take from the member
let removed_roles = member let removed_roles = member
.roles .roles
.iter() .iter()
.filter(|r| take.contains(&r.0)) .filter(|r| take.contains(&r.0))
.map(|r| r.to_string()) .map(|r| r.to_string())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// take them from the member, get remaining roles
let removed = member let remaining_roles = member
.remove_roles( .remove_roles(
&ctx.discord().http, &ctx.discord().http,
&take.iter().map(|&r| RoleId::from(r)).collect::<Vec<_>>(), &take.iter().map(|&r| RoleId::from(r)).collect::<Vec<_>>(),
) )
.await; .await;
if let Err(remove_role_result) = removed { if let Err(remove_role_result) = remaining_roles {
Some(Error::from(remove_role_result)) Some(Error::from(remove_role_result))
} else { } else {
// Roles which were removed from the user // Roles which were removed from the user

View File

@ -1,6 +1,9 @@
use super::*; use super::*;
use crate::utils::decancer::cure; use crate::utils::decancer::cure;
use crate::utils::moderation::mute_on_join;
pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &mut serenity::Member) {
mute_on_join(ctx, new_member).await;
pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &serenity::Member) {
cure(ctx, &None, new_member).await; cure(ctx, &None, new_member).await;
} }

View File

@ -7,11 +7,11 @@ use super::*;
use crate::utils::bot::get_data_lock; use crate::utils::bot::get_data_lock;
use crate::utils::ocr; use crate::utils::ocr;
fn contains_match(regex: &Vec<Regex>, text: &str) -> bool { fn contains_match(regex: &[Regex], text: &str) -> bool {
regex.iter().any(|r| r.is_match(text)) regex.iter().any(|r| r.is_match(text))
} }
async fn attachments_contains(attachments: &Vec<Attachment>, regex: &Vec<Regex>) -> bool { async fn attachments_contains(attachments: &[Attachment], regex: &[Regex]) -> bool {
for attachment in attachments { for attachment in attachments {
debug!("Checking attachment {}", &attachment.url); debug!("Checking attachment {}", &attachment.url);
@ -31,7 +31,7 @@ async fn attachments_contains(attachments: &Vec<Attachment>, regex: &Vec<Regex>)
pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Message) { pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Message) {
debug!("Received message: {}", new_message.content); debug!("Received message: {}", new_message.content);
if new_message.guild_id.is_none() || new_message.author.bot { if new_message.guild_id.is_none() || new_message.author.bot {
return; return;
} }
@ -64,18 +64,19 @@ pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Mes
let contains_attachments = !new_message.attachments.is_empty(); let contains_attachments = !new_message.attachments.is_empty();
// 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.text, &message) { if contains_match(&excludes.match_field.text, message) {
continue; continue;
} }
if contains_attachments && !excludes.match_field.ocr.is_empty() { if contains_attachments
if attachments_contains(&new_message.attachments, &excludes.match_field.ocr).await { && !excludes.match_field.ocr.is_empty()
continue; && attachments_contains(&new_message.attachments, &excludes.match_field.ocr).await
} {
continue;
} }
// check if the message does match any of the includes // check if the message does match any of the includes
if !(contains_match(&response.includes.match_field.text, &message) if !(contains_match(&response.includes.match_field.text, message)
|| (contains_attachments || (contains_attachments
&& !response.includes.match_field.ocr.is_empty() && !response.includes.match_field.ocr.is_empty()
&& attachments_contains( && attachments_contains(

View File

@ -87,8 +87,8 @@ impl serenity::EventHandler for Handler<Arc<RwLock<Data>>> {
thread_create::thread_create(&ctx, &thread).await; thread_create::thread_create(&ctx, &thread).await;
} }
async fn guild_member_addition(&self, ctx: serenity::Context, new_member: serenity::Member) { async fn guild_member_addition(&self, ctx: serenity::Context, mut new_member: serenity::Member) {
guild_member_addition::guild_member_addition(&ctx, &new_member).await; guild_member_addition::guild_member_addition(&ctx, &mut new_member).await;
} }
async fn guild_member_update( async fn guild_member_update(

View File

@ -1,8 +1,11 @@
use std::sync::Arc; use std::sync::Arc;
use mongodb::options::FindOptions;
use poise::serenity_prelude::Http; use poise::serenity_prelude::Http;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing::{debug, error, trace};
use super::bot::get_data_lock;
use super::*; use super::*;
use crate::db::database::Database; use crate::db::database::Database;
use crate::db::model::Muted; use crate::db::model::Muted;
@ -13,6 +16,46 @@ pub enum ModerationKind {
Unmute(Option<Error>), // Error Unmute(Option<Error>), // Error
} }
pub async fn mute_on_join(ctx: &serenity::Context, new_member: &mut serenity::Member) {
let data = get_data_lock(ctx).await;
let data = data.read().await;
if let Ok(mut cursor) = data
.database
.find::<Muted>(
"muted",
Muted {
user_id: Some(new_member.user.id.to_string()),
..Default::default()
}
.into(),
Some(FindOptions::builder().limit(1).build()),
)
.await
{
if cursor.advance().await.is_ok() {
trace!("Muted member {} rejoined the server", new_member.user.tag());
if new_member
.add_role(&ctx.http, RoleId(data.configuration.general.mute.role))
.await
.is_ok()
{
debug!(
"Muted member {} was successfully muted",
new_member.user.tag()
);
} else {
error!(
"Failed to mute member {} after rejoining the server",
new_member.user.tag()
);
}
}
} else {
error!("Failed to query database for muted users");
}
}
pub fn queue_unmute_member( pub fn queue_unmute_member(
http: &Arc<Http>, http: &Arc<Http>,
database: &Arc<Database>, database: &Arc<Database>,

View File

@ -5,7 +5,7 @@ pub async fn get_text_from_image_url(url: &str) -> Result<String, Error> {
let image = &reqwest::get(url).await?.bytes().await.unwrap().to_vec(); let image = &reqwest::get(url).await?.bytes().await.unwrap().to_vec();
Ok(Tesseract::new(None, None) Ok(Tesseract::new(None, None)
.unwrap() .unwrap()
.set_image_from_mem(&image) .set_image_from_mem(image)
.unwrap() .unwrap()
.get_text() .get_text()
.unwrap()) .unwrap())