Skip to content

Commit

Permalink
fix: send error replies as ephemeral (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
Ushie and oSumAtrIX committed Jul 6, 2023
1 parent 770f61f commit cc83a39
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage = "https://revanced.app"
license = "GPL-3.0"
name = "revanced-discord-bot"
repository = "https://github.com/revanced/revanced-discord-bot"
version = "2.5.0"
version = "2.5.1"
edition = "2021"

[profile.release]
Expand Down
176 changes: 99 additions & 77 deletions src/utils/moderation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp;
use std::sync::Arc;
use std::sync::{Arc, RwLock};

use mongodb::options::FindOptions;
use poise::serenity_prelude::{ChannelId, GuildChannel, GuildId, Mentionable, User, UserId};
Expand Down Expand Up @@ -123,6 +123,8 @@ pub async fn respond_moderation<'a>(
) -> Result<(), Error> {
let current_user = ctx.serenity_context().http.get_current_user().await?;

let send_ephemeral = RwLock::new(false);

let create_embed = |f: &mut serenity::CreateEmbed| {
let mut moderated_user: Option<&User> = None;

Expand All @@ -131,18 +133,21 @@ pub async fn respond_moderation<'a>(
moderated_user = Some(user);

let embed = match error {
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,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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()),
Expand All @@ -160,18 +165,21 @@ pub async fn respond_moderation<'a>(
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)
.field(
"Action",
format!(
"{} was unmuted by {} but failed",
user.mention(),
author.mention()
),
false,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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()),
Expand All @@ -182,18 +190,21 @@ pub async fn respond_moderation<'a>(
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)
.field(
"Action",
format!(
"{} was banned by {} but failed",
user.mention(),
author.mention()
),
false,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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()),
Expand All @@ -209,18 +220,21 @@ pub async fn respond_moderation<'a>(
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)
.field(
"Action",
format!(
"{} was unbanned by {} but failed",
user.mention(),
author.mention()
),
false,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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()),
Expand All @@ -229,18 +243,21 @@ pub async fn respond_moderation<'a>(
}
},
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,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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(
Expand All @@ -253,18 +270,21 @@ pub async fn respond_moderation<'a>(
),
},
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,
),
Some(err) => {
*send_ephemeral.write().unwrap() = true;

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.")
Expand All @@ -291,10 +311,12 @@ pub async fn respond_moderation<'a>(

let reply = ctx
.send(|reply| {
reply.embed(|embed| {
create_embed(embed);
embed
})
reply
.embed(|embed| {
create_embed(embed);
embed
})
.ephemeral(*send_ephemeral.read().unwrap())
})
.await?;

Expand Down

0 comments on commit cc83a39

Please sign in to comment.