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,
Permissions,
RoleId,
User,
User, Mentionable,
};
use tracing::log::error;
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 = &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))
@ -323,6 +333,7 @@ pub async fn mute(
&ctx,
&ModerationKind::Mute(
member.user,
author.clone(),
reason,
format!("<t:{}:F>", unmute_time.timestamp()),
result,
@ -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| {
@ -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(),
)
})
@ -464,13 +483,15 @@ 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,
)

View File

@ -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<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
@ -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,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(),
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,
),
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(),
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,
),
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(),
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,
),
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()
),
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::Unlock(channel, error) => match error {
Some(err) => f.title(format!("Failed to unlock {}", channel)).field(
"Exception",
err.to_string(),
false,
),
None => f
.title(format!("Unlocked {}", channel))
.description("Restored original permission overwrites."),
.title(format!("Unbanned {}", user.tag()))
.field(
"Action",
format!("{} was unbanned by {}", user.mention(), author.mention()),
false,
),
}
},
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, 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);
@ -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
@ -238,7 +314,7 @@ pub async fn respond_moderation<'a>(
response.channel_id,
response.id
),
false,
true,
)
})
})