From d160512a5e11bba3139f99a504ff6dd4886e8785 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 21:25:01 +0300 Subject: [PATCH 01/10] feat(moderation): more detailed logs --- src/commands/moderation.rs | 34 ++++++-- src/utils/moderation.rs | 169 +++++++++++++++++++++++++------------ 2 files changed, 142 insertions(+), 61 deletions(-) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index 9d62e05..d3f0753 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -7,7 +7,7 @@ use poise::serenity_prelude::{ PermissionOverwrite, Permissions, RoleId, - User, + User, Mentionable, }; use tracing::log::error; use tracing::{debug, trace}; @@ -35,6 +35,8 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> { let channel_id = ctx.channel_id().0; let channel = &cache.guild_channel(channel_id).unwrap(); + let author = ctx.author(); + let query: Document = LockedChannel { channel_id: Some(channel_id.to_string()), ..Default::default() @@ -50,7 +52,8 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> { respond_moderation( &ctx, &ModerationKind::Lock( - channel.name.clone(), + channel.clone(), + author.clone(), Some(Error::from("Channel already locked")), ), configuration, @@ -107,7 +110,7 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> { respond_moderation( &ctx, - &ModerationKind::Lock(channel.name.clone(), None), + &ModerationKind::Lock(channel.clone(), author.clone(), None), configuration, ) .await @@ -138,6 +141,9 @@ pub async fn unlock(ctx: Context<'_>) -> Result<(), Error> { .await; let channel = cache.guild_channel(channel_id).unwrap(); + + let author = ctx.author(); + let mut error = None; if let Ok(Some(locked_channel)) = delete_result { for overwrite in &locked_channel.overwrites.unwrap() { @@ -149,7 +155,7 @@ pub async fn unlock(ctx: Context<'_>) -> Result<(), Error> { respond_moderation( &ctx, - &ModerationKind::Unlock(channel.name.clone(), error), // TODO: handle error + &ModerationKind::Unlock(channel.clone(), author.clone(), error), // TODO: handle error configuration, ) .await @@ -171,6 +177,8 @@ pub async fn unmute( pending_unmute.abort(); } + let author = ctx.author(); + let queue = queue_unmute_member( &ctx.discord().http, &data.database, @@ -183,7 +191,7 @@ pub async fn unmute( respond_moderation( &ctx, - &ModerationKind::Unmute(member.user, queue), + &ModerationKind::Unmute(member.user, author.clone(), queue), configuration, ) .await @@ -237,6 +245,8 @@ pub async fn mute( let take = &mute.take; let is_currently_muted = member.roles.iter().any(|r| r.0 == mute_role_id); + let author = ctx.author(); + let result = if let Err(add_role_result) = member.add_role(&ctx.discord().http, mute_role_id).await { Some(Error::from(add_role_result)) @@ -317,6 +327,7 @@ pub async fn mute( &ctx, &ModerationKind::Mute( member.user, + author.clone(), reason, format!("", unmute_time.timestamp()), result, @@ -353,6 +364,8 @@ pub async fn purge( let current_user = ctx.discord().http.get_current_user().await?; let image = current_user.face(); + let author = ctx.author(); + let handle = ctx .send(|f| { f.embed(|f| { @@ -426,7 +439,8 @@ pub async fn purge( e.set_embed( serenity::CreateEmbed::default() .title("Purge successful") - .field("Deleted messages", deleted_amount.to_string(), false) + .field("Deleted messages", deleted_amount.to_string(), true) + .field("Action by", author.mention(), true) .color(embed_color) .thumbnail(image) .clone(), @@ -458,15 +472,17 @@ async fn handle_ban(ctx: &Context<'_>, kind: &BanKind) -> Result<(), Error> { let ban_result = ban_moderation(ctx, kind).await; + let author = ctx.author(); + respond_moderation( ctx, &match kind { BanKind::Ban(user, _, reason) => { - ModerationKind::Ban(user.clone(), reason.clone(), ban_result) + ModerationKind::Ban(user.clone(), author.clone(), reason.clone(), ban_result) }, - BanKind::Unban(user) => ModerationKind::Unban(user.clone(), ban_result), + BanKind::Unban(user) => ModerationKind::Unban(user.clone(), author.clone(), ban_result), }, &data.configuration, ) .await -} +} \ No newline at end of file diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index dc37fef..52a6a8e 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -2,7 +2,7 @@ use std::cmp; use std::sync::Arc; use mongodb::options::FindOptions; -use poise::serenity_prelude::{ChannelId, Http, User}; +use poise::serenity_prelude::{ChannelId, GuildChannel, Http, Mentionable, User}; use tokio::task::JoinHandle; use tracing::{debug, error}; @@ -15,12 +15,12 @@ use crate::serenity::SerenityError; use crate::{Context, Error}; pub enum ModerationKind { - Mute(User, String, String, Option), // User, Reason, Expires, Error - Unmute(User, Option), // User, Error - Ban(User, Option, Option), // User, Reason, Error - Unban(User, Option), // User, Error - Lock(String, Option), // Channel name, Error - Unlock(String, Option), // Channel name, Error + Mute(User, User, String, String, Option), // User, Command author, Reason, Expires, Error + Unmute(User, User, Option), // User, Command author, Error + Ban(User, User, Option, Option), // User, Command author, Reason, Error + Unban(User, User, Option), // User, Command author, Error + Lock(GuildChannel, User, Option), // Channel name, Command author, Error + Unlock(GuildChannel, User, Option), // Channel name, Command author, Error } pub enum BanKind { Ban(User, Option, Option), // User, Amount of days to delete messages, Reason @@ -43,7 +43,6 @@ pub async fn mute_on_join(ctx: &serenity::Context, new_member: &mut serenity::Me ) .await { - if let Ok(found) = cursor.advance().await { if found { debug!("Muted member {} rejoined the server", new_member.user.tag()); @@ -132,77 +131,143 @@ pub async fn respond_moderation<'a>( let mut moderated_user: Option<&User> = None; let result = match moderation { - ModerationKind::Mute(user, reason, expires, error) => { + ModerationKind::Mute(user, author, reason, expires, error) => { moderated_user = Some(user); match error { - Some(err) => f.title(format!("Failed to mute {}", user.tag())).field( - "Exception", - err.to_string(), - false, + Some(err) => f + .title(format!("Failed to mute {}", user.tag())) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!( + "{} was muted by {} but failed", + user.mention(), + author.mention() + ), + false, + ), + None => f.title(format!("Muted {}", user.tag())).field( + "Action", + format!("{} was muted by {}", user.mention(), author.mention()), + true, ), - None => f.title(format!("Muted {}", user.tag())), } - .field("Reason", reason, false) - .field("Expires", expires, false) + .field("Reason", reason, true) + .field("Expires", expires, true) }, - ModerationKind::Unmute(user, error) => { + ModerationKind::Unmute(user, author, error) => { moderated_user = Some(user); match error { - Some(err) => f.title(format!("Failed to unmute {}", user.tag())).field( - "Exception", - err.to_string(), - false, + Some(err) => f + .title(format!("Failed to unmute {}", user.tag())) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!( + "{} was unmuted by {} but failed", + user.mention(), + author.mention() + ), + false, + ), + None => f.title(format!("Unmuted {}", user.tag())).field( + "Action", + format!("{} was unmuted by {}", user.mention(), author.mention()), + true, ), - None => f.title(format!("Unmuted {}", user.tag())), } }, - ModerationKind::Ban(user, reason, error) => { + ModerationKind::Ban(user, author, reason, error) => { moderated_user = Some(user); let f = match error { - Some(err) => f.title(format!("Failed to ban {}", user.tag())).field( - "Exception", - err.to_string(), - false, + Some(err) => f + .title(format!("Failed to ban {}", user.tag())) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!( + "{} was banned by {} but failed", + user.mention(), + author.mention() + ), + false, + ), + None => f.title(format!("Banned {}", user.tag())).field( + "Action", + format!("{} was banned by {}", user.mention(), author.mention()), + true, ), - None => f.title(format!("Banned {}", user.tag())), }; if let Some(reason) = reason { - f.field("Reason", reason, false) + f.field("Reason", reason, true) } else { f } }, - ModerationKind::Unban(user, error) => { + ModerationKind::Unban(user, author, error) => { moderated_user = Some(user); match error { - Some(err) => f.title(format!("Failed to unban {}", user.tag())).field( - "Exception", - err.to_string(), - false, + Some(err) => f + .title(format!("Failed to unban {}", user.tag())) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!( + "{} was unbanned by {} but failed", + user.mention(), + author.mention() + ), + false, + ), + None => f.title(format!("Unbanned {}", user.tag())).field( + "Action by", + format!("{} was unbanned by {}", user.mention(), author.mention()), + true, ), - None => f.title(format!("Unbanned {}", user.tag())), } }, - ModerationKind::Lock(channel, error) => match error { - Some(err) => f.title(format!("Failed to lock {} ", channel)).field( - "Exception", - err.to_string(), - false, - ), - None => f.title(format!("Locked {}", channel)).description( - "Unlocking the channel will restore the original permission overwrites.", - ), + ModerationKind::Lock(channel, author, error) => match error { + Some(err) => f + .title(format!("Failed to lock {} ", channel)) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!("{} was locked by {}", channel.mention(), author.mention()), + true, + ), + None => f + .title(format!("Locked {}", channel.name())) + .description( + "Unlocking the channel will restore the original permission overwrites.", + ) + .field( + "Action", + format!("{} was locked by {}", channel.mention(), author.mention()), + true, + ), }, - ModerationKind::Unlock(channel, error) => match error { - Some(err) => f.title(format!("Failed to unlock {}", channel)).field( - "Exception", - err.to_string(), - false, - ), + ModerationKind::Unlock(channel, author, error) => match error { + Some(err) => f + .title(format!("Failed to unlock {}", channel.name())) + .field("Exception", err.to_string(), false) + .field( + "Action", + format!( + "{} was unlocked by {} but failed", + channel.mention(), + author.mention() + ), + false, + ), None => f .title(format!("Unlocked {}", channel)) - .description("Restored original permission overwrites."), + .description("Restored original permission overwrites.") + .field( + "Action", + format!("{} was unlocked by {}", channel, author.mention()), + true, + ), }, } .color(configuration.general.embed_color); @@ -238,7 +303,7 @@ pub async fn respond_moderation<'a>( response.channel_id, response.id ), - false, + true, ) }) }) From 83961ba80b407669a50ab9741678ab78619e492d Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 21:37:05 +0300 Subject: [PATCH 02/10] refactor: little cleanup --- src/utils/moderation.rs | 46 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index 52a6a8e..08b41f7 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -147,11 +147,13 @@ pub async fn respond_moderation<'a>( ), false, ), - None => f.title(format!("Muted {}", user.tag())).field( - "Action", - format!("{} was muted by {}", user.mention(), author.mention()), + None => f + .title(format!("Muted {}", user.tag())) + .field( + "Action", + format!("{} was muted by {}", user.mention(), author.mention()), true, - ), + ), } .field("Reason", reason, true) .field("Expires", expires, true) @@ -171,11 +173,13 @@ pub async fn respond_moderation<'a>( ), false, ), - None => f.title(format!("Unmuted {}", user.tag())).field( - "Action", - format!("{} was unmuted by {}", user.mention(), author.mention()), - true, - ), + None => f + .title(format!("Unmuted {}", user.tag())) + .field( + "Action", + format!("{} was unmuted by {}", user.mention(), author.mention()), + true, + ), } }, ModerationKind::Ban(user, author, reason, error) => { @@ -193,11 +197,13 @@ pub async fn respond_moderation<'a>( ), false, ), - None => f.title(format!("Banned {}", user.tag())).field( - "Action", - format!("{} was banned by {}", user.mention(), author.mention()), - true, - ), + None => f + .title(format!("Banned {}", user.tag())) + .field( + "Action", + format!("{} was banned by {}", user.mention(), author.mention()), + true, + ), }; if let Some(reason) = reason { f.field("Reason", reason, true) @@ -220,11 +226,13 @@ pub async fn respond_moderation<'a>( ), false, ), - None => f.title(format!("Unbanned {}", user.tag())).field( - "Action by", - format!("{} was unbanned by {}", user.mention(), author.mention()), - true, - ), + None => f + .title(format!("Unbanned {}", user.tag())) + .field( + "Action by", + format!("{} was unbanned by {}", user.mention(), author.mention()), + true, + ), } }, ModerationKind::Lock(channel, author, error) => match error { From a38a9598f9a0523168bb9d70a34a65e8a870560d Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 21:50:27 +0300 Subject: [PATCH 03/10] feat: add embed footers --- src/commands/moderation.rs | 5 +++++ src/utils/moderation.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index d3f0753..786cde3 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -443,6 +443,11 @@ pub async fn purge( .field("Action by", author.mention(), true) .color(embed_color) .thumbnail(image) + .footer(|f| { + f.text("ReVanced"); + f.icon_url(current_user.face()) // ran into a "use of moved value" when using the image variable here, HELP! + } + ) .clone(), ) }) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index 08b41f7..d03681c 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -313,6 +313,10 @@ pub async fn respond_moderation<'a>( ), true, ) + .footer(|f| { + f.text("ReVanced"); + f.icon_url(current_user.face()) + }) }) }) .await?; From ed7b150cb30da83436cf79aae1f98f75d4ac6d15 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 22:19:17 +0300 Subject: [PATCH 04/10] fix: set footer in the correct spot --- src/utils/moderation.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index d03681c..c951790 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -286,7 +286,10 @@ pub async fn respond_moderation<'a>( current_user.face() }; - result.thumbnail(&user); + result.thumbnail(&user).footer(|f| { + f.text("ReVanced"); + f.icon_url(current_user.face()) + }); }; let reply = ctx @@ -313,10 +316,6 @@ pub async fn respond_moderation<'a>( ), true, ) - .footer(|f| { - f.text("ReVanced"); - f.icon_url(current_user.face()) - }) }) }) .await?; From 5e1e1aa403cb6ade939d9591e4aa9c2fb066c9b7 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 22:40:29 +0300 Subject: [PATCH 05/10] feat: better choice of inlined fields --- src/commands/moderation.rs | 4 ++-- src/utils/moderation.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index 786cde3..0c2b9e5 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -439,8 +439,8 @@ pub async fn purge( e.set_embed( serenity::CreateEmbed::default() .title("Purge successful") - .field("Deleted messages", deleted_amount.to_string(), true) - .field("Action by", author.mention(), true) + .field("Deleted messages", deleted_amount.to_string(), false) + .field("Action by", author.mention(), false) .color(embed_color) .thumbnail(image) .footer(|f| { diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index c951790..071bfe3 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -152,7 +152,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was muted by {}", user.mention(), author.mention()), - true, + false, ), } .field("Reason", reason, true) @@ -178,7 +178,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was unmuted by {}", user.mention(), author.mention()), - true, + false, ), } }, @@ -202,7 +202,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was banned by {}", user.mention(), author.mention()), - true, + false, ), }; if let Some(reason) = reason { @@ -231,7 +231,7 @@ pub async fn respond_moderation<'a>( .field( "Action by", format!("{} was unbanned by {}", user.mention(), author.mention()), - true, + false, ), } }, @@ -242,7 +242,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was locked by {}", channel.mention(), author.mention()), - true, + false, ), None => f .title(format!("Locked {}", channel.name())) @@ -252,7 +252,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was locked by {}", channel.mention(), author.mention()), - true, + false, ), }, ModerationKind::Unlock(channel, author, error) => match error { @@ -274,7 +274,7 @@ pub async fn respond_moderation<'a>( .field( "Action", format!("{} was unlocked by {}", channel, author.mention()), - true, + false, ), }, } From 9217f5bd213b9b7797647acd6ce8007ade992386 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 22:45:08 +0300 Subject: [PATCH 06/10] fix: missing text --- src/utils/moderation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index 071bfe3..9248293 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -241,7 +241,7 @@ pub async fn respond_moderation<'a>( .field("Exception", err.to_string(), false) .field( "Action", - format!("{} was locked by {}", channel.mention(), author.mention()), + format!("{} was locked by {} but failed", channel.mention(), author.mention()), false, ), None => f From ddfd7a49de17be22e531eaa804d5b65ccdaefdea Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 22:49:28 +0300 Subject: [PATCH 07/10] fix: unban action field name and spacing --- src/utils/moderation.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index 9248293..cdcf0b9 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -150,9 +150,9 @@ pub async fn respond_moderation<'a>( None => f .title(format!("Muted {}", user.tag())) .field( - "Action", - format!("{} was muted by {}", user.mention(), author.mention()), - false, + "Action", + format!("{} was muted by {}", user.mention(), author.mention()), + false, ), } .field("Reason", reason, true) @@ -229,8 +229,8 @@ pub async fn respond_moderation<'a>( None => f .title(format!("Unbanned {}", user.tag())) .field( - "Action by", - format!("{} was unbanned by {}", user.mention(), author.mention()), + "Action", + format!("{} was unbanned by {}", user.mention(), author.mention()), false, ), } From 23ea6ab8ad6a93daca0a780238ceb3d7972abe98 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 21 Nov 2022 23:12:12 +0300 Subject: [PATCH 08/10] fix: missed a few things --- src/utils/moderation.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/moderation.rs b/src/utils/moderation.rs index cdcf0b9..b171580 100644 --- a/src/utils/moderation.rs +++ b/src/utils/moderation.rs @@ -237,7 +237,7 @@ pub async fn respond_moderation<'a>( }, ModerationKind::Lock(channel, author, error) => match error { Some(err) => f - .title(format!("Failed to lock {} ", channel)) + .title(format!("Failed to lock {} ", channel.name())) .field("Exception", err.to_string(), false) .field( "Action", @@ -269,11 +269,11 @@ pub async fn respond_moderation<'a>( false, ), None => f - .title(format!("Unlocked {}", channel)) + .title(format!("Unlocked {}", channel.name())) .description("Restored original permission overwrites.") .field( "Action", - format!("{} was unlocked by {}", channel, author.mention()), + format!("{} was unlocked by {}", channel.mention(), author.mention()), false, ), }, From 6f3213d54d213bb7e4c3bbae51e3b33a604fe160 Mon Sep 17 00:00:00 2001 From: Ushie Date: Thu, 24 Nov 2022 13:16:20 +0300 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Ax333l --- src/commands/moderation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index 0c2b9e5..1f5353e 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -442,10 +442,10 @@ pub async fn purge( .field("Deleted messages", deleted_amount.to_string(), false) .field("Action by", author.mention(), false) .color(embed_color) - .thumbnail(image) + .thumbnail(image.clone()) .footer(|f| { f.text("ReVanced"); - f.icon_url(current_user.face()) // ran into a "use of moved value" when using the image variable here, HELP! + f.icon_url(image) } ) .clone(), From c260024ce4a6bccb4dd405b2d2f49d624e89a38d Mon Sep 17 00:00:00 2001 From: Ushie Date: Sun, 27 Nov 2022 18:12:11 +0300 Subject: [PATCH 10/10] Apply suggestion from code review Co-authored-by: Ax333l --- src/commands/moderation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/moderation.rs b/src/commands/moderation.rs index 1f5353e..dc9bca4 100644 --- a/src/commands/moderation.rs +++ b/src/commands/moderation.rs @@ -442,7 +442,7 @@ pub async fn purge( .field("Deleted messages", deleted_amount.to_string(), false) .field("Action by", author.mention(), false) .color(embed_color) - .thumbnail(image.clone()) + .thumbnail(&image) .footer(|f| { f.text("ReVanced"); f.icon_url(image)