feat(moderation): improve log embeds (#37)

Co-authored-by: Ax333l <main@axelen.xyz>
This commit is contained in:
Ushie 2022-12-03 05:58:38 +03:00 committed by GitHub
parent 1c12647364
commit 78f954b7e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 165 additions and 68 deletions

View File

@ -7,7 +7,7 @@ use poise::serenity_prelude::{
PermissionOverwrite, PermissionOverwrite,
Permissions, Permissions,
RoleId, RoleId,
User, User, Mentionable,
}; };
use tracing::log::error; use tracing::log::error;
use tracing::{debug, warn, trace}; use tracing::{debug, warn, trace};
@ -35,6 +35,8 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> {
let channel_id = ctx.channel_id().0; let channel_id = ctx.channel_id().0;
let channel = &cache.guild_channel(channel_id).unwrap(); let channel = &cache.guild_channel(channel_id).unwrap();
let author = ctx.author();
let query: Document = LockedChannel { let query: Document = LockedChannel {
channel_id: Some(channel_id.to_string()), channel_id: Some(channel_id.to_string()),
..Default::default() ..Default::default()
@ -50,7 +52,8 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> {
respond_moderation( respond_moderation(
&ctx, &ctx,
&ModerationKind::Lock( &ModerationKind::Lock(
channel.name.clone(), channel.clone(),
author.clone(),
Some(Error::from("Channel already locked")), Some(Error::from("Channel already locked")),
), ),
configuration, configuration,
@ -107,7 +110,7 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> {
respond_moderation( respond_moderation(
&ctx, &ctx,
&ModerationKind::Lock(channel.name.clone(), None), &ModerationKind::Lock(channel.clone(), author.clone(), None),
configuration, configuration,
) )
.await .await
@ -138,6 +141,9 @@ pub async fn unlock(ctx: Context<'_>) -> Result<(), Error> {
.await; .await;
let channel = cache.guild_channel(channel_id).unwrap(); let channel = cache.guild_channel(channel_id).unwrap();
let author = ctx.author();
let mut error = None; let mut error = None;
if let Ok(Some(locked_channel)) = delete_result { if let Ok(Some(locked_channel)) = delete_result {
for overwrite in &locked_channel.overwrites.unwrap() { for overwrite in &locked_channel.overwrites.unwrap() {
@ -149,7 +155,7 @@ pub async fn unlock(ctx: Context<'_>) -> Result<(), Error> {
respond_moderation( respond_moderation(
&ctx, &ctx,
&ModerationKind::Unlock(channel.name.clone(), error), // TODO: handle error &ModerationKind::Unlock(channel.clone(), author.clone(), error), // TODO: handle error
configuration, configuration,
) )
.await .await
@ -171,6 +177,8 @@ pub async fn unmute(
pending_unmute.abort(); pending_unmute.abort();
} }
let author = ctx.author();
let queue = queue_unmute_member( let queue = queue_unmute_member(
&ctx.discord().http, &ctx.discord().http,
&data.database, &data.database,
@ -183,7 +191,7 @@ pub async fn unmute(
respond_moderation( respond_moderation(
&ctx, &ctx,
&ModerationKind::Unmute(member.user, queue), &ModerationKind::Unmute(member.user, author.clone(), queue),
configuration, configuration,
) )
.await .await
@ -237,6 +245,8 @@ pub async fn mute(
let take = &mute.take; let take = &mute.take;
let is_currently_muted = member.roles.iter().any(|r| r.0 == mute_role_id); let is_currently_muted = member.roles.iter().any(|r| r.0 == mute_role_id);
let author = ctx.author();
let result = let result =
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))
@ -323,6 +333,7 @@ pub async fn mute(
&ctx, &ctx,
&ModerationKind::Mute( &ModerationKind::Mute(
member.user, member.user,
author.clone(),
reason, reason,
format!("<t:{}:F>", unmute_time.timestamp()), format!("<t:{}:F>", unmute_time.timestamp()),
result, result,
@ -359,6 +370,8 @@ pub async fn purge(
let current_user = ctx.discord().http.get_current_user().await?; let current_user = ctx.discord().http.get_current_user().await?;
let image = current_user.face(); let image = current_user.face();
let author = ctx.author();
let handle = ctx let handle = ctx
.send(|f| { .send(|f| {
f.embed(|f| { f.embed(|f| {
@ -433,8 +446,14 @@ pub async fn purge(
serenity::CreateEmbed::default() serenity::CreateEmbed::default()
.title("Purge successful") .title("Purge successful")
.field("Deleted messages", deleted_amount.to_string(), false) .field("Deleted messages", deleted_amount.to_string(), false)
.field("Action by", author.mention(), false)
.color(embed_color) .color(embed_color)
.thumbnail(image) .thumbnail(&image)
.footer(|f| {
f.text("ReVanced");
f.icon_url(image)
}
)
.clone(), .clone(),
) )
}) })
@ -464,15 +483,17 @@ async fn handle_ban(ctx: &Context<'_>, kind: &BanKind) -> Result<(), Error> {
let ban_result = ban_moderation(ctx, kind).await; let ban_result = ban_moderation(ctx, kind).await;
let author = ctx.author();
respond_moderation( respond_moderation(
ctx, ctx,
&match kind { &match kind {
BanKind::Ban(user, _, reason) => { 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, &data.configuration,
) )
.await .await
} }

View File

@ -2,7 +2,7 @@ use std::cmp;
use std::sync::Arc; use std::sync::Arc;
use mongodb::options::FindOptions; 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 tokio::task::JoinHandle;
use tracing::{debug, error}; use tracing::{debug, error};
@ -15,12 +15,12 @@ use crate::serenity::SerenityError;
use crate::{Context, Error}; use crate::{Context, Error};
pub enum ModerationKind { pub enum ModerationKind {
Mute(User, String, String, Option<Error>), // User, Reason, Expires, Error Mute(User, User, String, String, Option<Error>), // User, Command author, Reason, Expires, Error
Unmute(User, Option<Error>), // User, Error Unmute(User, User, Option<Error>), // User, Command author, Error
Ban(User, Option<String>, Option<SerenityError>), // User, Reason, Error Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
Unban(User, Option<SerenityError>), // User, Error Unban(User, User, Option<SerenityError>), // User, Command author, Error
Lock(String, Option<Error>), // Channel name, Error Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
Unlock(String, Option<Error>), // Channel name, Error Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
} }
pub enum BanKind { pub enum BanKind {
Ban(User, Option<u8>, Option<String>), // User, Amount of days to delete messages, Reason Ban(User, Option<u8>, Option<String>), // 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 .await
{ {
if let Ok(found) = cursor.advance().await { if let Ok(found) = cursor.advance().await {
if found { if found {
debug!("Muted member {} rejoined the server", new_member.user.tag()); debug!("Muted member {} rejoined the server", new_member.user.tag());
@ -132,77 +131,151 @@ pub async fn respond_moderation<'a>(
let mut moderated_user: Option<&User> = None; let mut moderated_user: Option<&User> = None;
let result = match moderation { let result = match moderation {
ModerationKind::Mute(user, reason, expires, error) => { ModerationKind::Mute(user, author, reason, expires, error) => {
moderated_user = Some(user); moderated_user = Some(user);
match error { match error {
Some(err) => f.title(format!("Failed to mute {}", user.tag())).field( Some(err) => f
"Exception", .title(format!("Failed to mute {}", user.tag()))
err.to_string(), .field("Exception", err.to_string(), false)
false, .field(
), "Action",
None => f.title(format!("Muted {}", user.tag())), 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()),
false,
),
} }
.field("Reason", reason, false) .field("Reason", reason, true)
.field("Expires", expires, false) .field("Expires", expires, true)
}, },
ModerationKind::Unmute(user, error) => { ModerationKind::Unmute(user, author, error) => {
moderated_user = Some(user); moderated_user = Some(user);
match error { match error {
Some(err) => f.title(format!("Failed to unmute {}", user.tag())).field( Some(err) => f
"Exception", .title(format!("Failed to unmute {}", user.tag()))
err.to_string(), .field("Exception", err.to_string(), false)
false, .field(
), "Action",
None => f.title(format!("Unmuted {}", user.tag())), 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()),
false,
),
} }
}, },
ModerationKind::Ban(user, reason, error) => { ModerationKind::Ban(user, author, reason, error) => {
moderated_user = Some(user); moderated_user = Some(user);
let f = match error { let f = match error {
Some(err) => f.title(format!("Failed to ban {}", user.tag())).field( Some(err) => f
"Exception", .title(format!("Failed to ban {}", user.tag()))
err.to_string(), .field("Exception", err.to_string(), false)
false, .field(
), "Action",
None => f.title(format!("Banned {}", user.tag())), 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()),
false,
),
}; };
if let Some(reason) = reason { if let Some(reason) = reason {
f.field("Reason", reason, false) f.field("Reason", reason, true)
} else { } else {
f f
} }
}, },
ModerationKind::Unban(user, error) => { ModerationKind::Unban(user, author, error) => {
moderated_user = Some(user); moderated_user = Some(user);
match error { match error {
Some(err) => f.title(format!("Failed to unban {}", user.tag())).field( Some(err) => f
"Exception", .title(format!("Failed to unban {}", user.tag()))
err.to_string(), .field("Exception", err.to_string(), false)
false, .field(
), "Action",
None => f.title(format!("Unbanned {}", user.tag())), format!(
"{} was unbanned by {} but failed",
user.mention(),
author.mention()
),
false,
),
None => f
.title(format!("Unbanned {}", user.tag()))
.field(
"Action",
format!("{} was unbanned by {}", user.mention(), author.mention()),
false,
),
} }
}, },
ModerationKind::Lock(channel, error) => match error { ModerationKind::Lock(channel, author, error) => match error {
Some(err) => f.title(format!("Failed to lock {} ", channel)).field( Some(err) => f
"Exception", .title(format!("Failed to lock {} ", channel.name()))
err.to_string(), .field("Exception", err.to_string(), false)
false, .field(
), "Action",
None => f.title(format!("Locked {}", channel)).description( format!("{} was locked by {} but failed", channel.mention(), author.mention()),
"Unlocking the channel will restore the original permission overwrites.", false,
), ),
},
ModerationKind::Unlock(channel, error) => match error {
Some(err) => f.title(format!("Failed to unlock {}", channel)).field(
"Exception",
err.to_string(),
false,
),
None => f None => f
.title(format!("Unlocked {}", channel)) .title(format!("Locked {}", channel.name()))
.description("Restored original permission overwrites."), .description(
"Unlocking the channel will restore the original permission overwrites.",
)
.field(
"Action",
format!("{} was locked by {}", channel.mention(), author.mention()),
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.name()))
.description("Restored original permission overwrites.")
.field(
"Action",
format!("{} was unlocked by {}", channel.mention(), author.mention()),
false,
),
}, },
} }
.color(configuration.general.embed_color); .color(configuration.general.embed_color);
@ -213,7 +286,10 @@ pub async fn respond_moderation<'a>(
current_user.face() current_user.face()
}; };
result.thumbnail(&user); result.thumbnail(&user).footer(|f| {
f.text("ReVanced");
f.icon_url(current_user.face())
});
}; };
let reply = ctx let reply = ctx
@ -238,7 +314,7 @@ pub async fn respond_moderation<'a>(
response.channel_id, response.channel_id,
response.id response.id
), ),
false, true,
) )
}) })
}) })