Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat(moderation): improve log embeds (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Ax333l <main@axelen.xyz>
  • Loading branch information
Ushie and Axelen123 authored Dec 3, 2022
1 parent 1c12647 commit 78f954b
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 67 deletions.
39 changes: 30 additions & 9 deletions src/commands/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use poise::serenity_prelude::{
PermissionOverwrite,
Permissions,
RoleId,
User,
User, Mentionable,
};
use tracing::log::error;
use tracing::{debug, warn, trace};
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -323,6 +333,7 @@ pub async fn mute(
&ctx,
&ModerationKind::Mute(
member.user,
author.clone(),
reason,
format!("<t:{}:F>", unmute_time.timestamp()),
result,
Expand Down Expand Up @@ -359,6 +370,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| {
Expand Down Expand Up @@ -433,8 +446,14 @@ pub async fn purge(
serenity::CreateEmbed::default()
.title("Purge successful")
.field("Deleted messages", deleted_amount.to_string(), false)
.field("Action by", author.mention(), false)
.color(embed_color)
.thumbnail(image)
.thumbnail(&image)
.footer(|f| {
f.text("ReVanced");
f.icon_url(image)
}
)
.clone(),
)
})
Expand Down Expand Up @@ -464,15 +483,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
}
}
192 changes: 134 additions & 58 deletions src/utils/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -15,12 +15,12 @@ use crate::serenity::SerenityError;
use crate::{Context, Error};

pub enum ModerationKind {
Mute(User, String, String, Option<Error>), // User, Reason, Expires, Error
Unmute(User, Option<Error>), // User, Error
Ban(User, Option<String>, Option<SerenityError>), // User, Reason, Error
Unban(User, Option<SerenityError>), // User, Error
Lock(String, Option<Error>), // Channel name, Error
Unlock(String, Option<Error>), // Channel name, Error
Mute(User, User, String, String, Option<Error>), // User, Command author, Reason, Expires, Error
Unmute(User, User, Option<Error>), // User, Command author, Error
Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
Unban(User, User, Option<SerenityError>), // User, Command author, Error
Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
}
pub enum BanKind {
Ban(User, Option<u8>, Option<String>), // User, Amount of days to delete messages, Reason
Expand All @@ -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());
Expand Down Expand Up @@ -132,77 +131,151 @@ 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,
),
None => f.title(format!("Muted {}", user.tag())),
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()),
false,
),
}
.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,
),
None => f.title(format!("Unmuted {}", user.tag())),
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()),
false,
),
}
},
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,
),
None => f.title(format!("Banned {}", user.tag())),
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()),
false,
),
};
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,
),
None => f.title(format!("Unbanned {}", user.tag())),
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",
format!("{} was unbanned by {}", user.mention(), author.mention()),
false,
),
}
},
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.name()))
.field("Exception", err.to_string(), false)
.field(
"Action",
format!("{} was locked by {} but failed", channel.mention(), author.mention()),
false,
),
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()),
false,
),
},
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."),
.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);
Expand All @@ -213,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
Expand All @@ -238,7 +314,7 @@ pub async fn respond_moderation<'a>(
response.channel_id,
response.id
),
false,
true,
)
})
})
Expand Down

0 comments on commit 78f954b

Please sign in to comment.