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

Commit 573d907

Browse files
CannyAxelen123
Canny
andauthored
feat: permanently mute when no duration is specified (#44)
* feat: permanently mute when no duration is specified Co-authored-by: Sculas <[email protected]> Co-authored-by: Ax333l <[email protected]> * refactor: remove unneeded if statement * fix: unset expires field when permanently muting * fix: clippy Co-authored-by: Ax333l <[email protected]> Co-authored-by: Ax333l <[email protected]>
1 parent 2466226 commit 573d907

File tree

3 files changed

+92
-82
lines changed

3 files changed

+92
-82
lines changed

src/commands/moderation.rs

+51-42
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@ use bson::{doc, Document};
22
use chrono::{Duration, Utc};
33
use mongodb::options::{UpdateModifications, UpdateOptions};
44
use poise::serenity_prelude::{
5-
self as serenity,
6-
Member,
7-
PermissionOverwrite,
8-
Permissions,
9-
RoleId,
10-
User, Mentionable,
5+
self as serenity, Member, Mentionable, PermissionOverwrite, Permissions, RoleId, User,
116
};
127
use tracing::log::error;
13-
use tracing::{debug, warn, trace};
8+
use tracing::{debug, trace, warn};
149

1510
use crate::db::model::{LockedChannel, Muted};
1611
use crate::utils::moderation::{
17-
ban_moderation,
18-
queue_unmute_member,
19-
respond_moderation,
20-
BanKind,
21-
ModerationKind,
12+
ban_moderation, queue_unmute_member, respond_moderation, BanKind, ModerationKind,
2213
};
2314
use crate::{Context, Error};
2415

@@ -97,11 +88,14 @@ pub async fn lock(ctx: Context<'_>) -> Result<(), Error> {
9788
let permission = Permissions::SEND_MESSAGES & Permissions::ADD_REACTIONS;
9889

9990
if let Err(err) = channel
100-
.create_permission(http, &PermissionOverwrite {
101-
allow: permission_overwrite.allow & !permission,
102-
deny: permission_overwrite.deny | permission,
103-
kind: permission_overwrite.kind,
104-
})
91+
.create_permission(
92+
http,
93+
&PermissionOverwrite {
94+
allow: permission_overwrite.allow & !permission,
95+
deny: permission_overwrite.deny | permission,
96+
kind: permission_overwrite.kind,
97+
},
98+
)
10599
.await
106100
{
107101
error!("Failed to create the new permission: {:?}", err);
@@ -143,7 +137,7 @@ pub async fn unlock(ctx: Context<'_>) -> Result<(), Error> {
143137
let channel = cache.guild_channel(channel_id).unwrap();
144138

145139
let author = ctx.author();
146-
140+
147141
let mut error = None;
148142
if let Ok(Some(locked_channel)) = delete_result {
149143
for overwrite in &locked_channel.overwrites.unwrap() {
@@ -236,7 +230,11 @@ pub async fn mute(
236230
.unwrap();
237231
}
238232

239-
let unmute_time = now + mute_duration;
233+
let unmute_time = if !mute_duration.is_zero() {
234+
Some((now + mute_duration).timestamp() as u64)
235+
} else {
236+
None
237+
};
240238

241239
let data = &mut *ctx.data().write().await;
242240
let configuration = &data.configuration;
@@ -272,7 +270,7 @@ pub async fn mute(
272270
// Roles which were removed from the user
273271
let updated: Document = Muted {
274272
guild_id: Some(member.guild_id.0.to_string()),
275-
expires: Some(unmute_time.timestamp() as u64),
273+
expires: unmute_time,
276274
reason: Some(reason.clone()),
277275
taken_roles: if is_currently_muted {
278276
// Prevent the bot from overriding the "take" field.
@@ -301,6 +299,20 @@ pub async fn mute(
301299
.await
302300
{
303301
Some(database_update_result)
302+
} else if unmute_time.is_none() {
303+
data.database
304+
.update::<Muted>(
305+
"muted",
306+
Muted {
307+
user_id: Some(member.user.id.0.to_string()),
308+
..Default::default()
309+
}
310+
.into(),
311+
UpdateModifications::Document(doc! { "$unset": { "expires": "" } }),
312+
None,
313+
)
314+
.await
315+
.err()
304316
} else {
305317
None
306318
}
@@ -312,32 +324,30 @@ pub async fn mute(
312324
pending_unmute.abort();
313325
}
314326

315-
data.pending_unmutes.insert(
316-
member.user.id.0,
317-
queue_unmute_member(
318-
&ctx.discord().http,
319-
&data.database,
320-
&member,
321-
mute_role_id,
322-
mute_duration.num_seconds() as u64,
323-
),
324-
);
327+
if let Some(mute_duration) = unmute_time {
328+
data.pending_unmutes.insert(
329+
member.user.id.0,
330+
queue_unmute_member(
331+
&ctx.discord().http,
332+
&data.database,
333+
&member,
334+
mute_role_id,
335+
mute_duration,
336+
),
337+
);
338+
}
325339

326340
if result.is_none() {
327341
if let Err(e) = member.disconnect_from_voice(&ctx.discord().http).await {
328342
warn!("Could not disconnect member from voice channel: {}", e);
329343
}
330344
}
331345

346+
let duration = unmute_time.map(|time| format!("<t:{}:F>", time));
347+
332348
respond_moderation(
333349
&ctx,
334-
&ModerationKind::Mute(
335-
member.user,
336-
author.clone(),
337-
reason,
338-
format!("<t:{}:F>", unmute_time.timestamp()),
339-
result,
340-
),
350+
&ModerationKind::Mute(member.user, author.clone(), reason, duration, result),
341351
configuration,
342352
)
343353
.await
@@ -450,10 +460,9 @@ pub async fn purge(
450460
.color(embed_color)
451461
.thumbnail(&image)
452462
.footer(|f| {
453-
f.text("ReVanced");
454-
f.icon_url(image)
455-
}
456-
)
463+
f.text("ReVanced");
464+
f.icon_url(image)
465+
})
457466
.clone(),
458467
)
459468
})
@@ -496,4 +505,4 @@ async fn handle_ban(ctx: &Context<'_>, kind: &BanKind) -> Result<(), Error> {
496505
&data.configuration,
497506
)
498507
.await
499-
}
508+
}

src/events/ready.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
2828

2929
while cursor.advance().await.unwrap() {
3030
let current: Muted = cursor.deserialize_current().unwrap();
31+
let Some(expires) = current.expires else { continue };
3132
let guild_id = current.guild_id.unwrap().parse::<u64>().unwrap();
3233
let member_id = current.user_id.unwrap().parse::<u64>().unwrap();
3334

3435
if let Ok(guild) = http_ref.get_guild(guild_id).await {
3536
if let Ok(member) = guild.member(http_ref, member_id).await {
36-
let amount_left =
37-
std::cmp::max(current.expires.unwrap() as i64 - Utc::now().timestamp(), 0);
37+
let amount_left = std::cmp::max(expires as i64 - Utc::now().timestamp(), 0);
3838

3939
data.pending_unmutes.insert(
4040
member.user.id.0,

src/utils/moderation.rs

+39-38
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use crate::serenity::SerenityError;
1515
use crate::{Context, Error};
1616

1717
pub enum ModerationKind {
18-
Mute(User, User, String, String, Option<Error>), // User, Command author, Reason, Expires, Error
19-
Unmute(User, User, Option<Error>), // User, Command author, Error
20-
Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
21-
Unban(User, User, Option<SerenityError>), // User, Command author, Error
22-
Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
23-
Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
18+
Mute(User, User, String, Option<String>, Option<Error>), // User, Command author, Reason, Expires, Error
19+
Unmute(User, User, Option<Error>), // User, Command author, Error
20+
Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
21+
Unban(User, User, Option<SerenityError>), // User, Command author, Error
22+
Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
23+
Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
2424
}
2525
pub enum BanKind {
2626
Ban(User, Option<u8>, Option<String>), // User, Amount of days to delete messages, Reason
@@ -134,7 +134,7 @@ pub async fn respond_moderation<'a>(
134134
ModerationKind::Mute(user, author, reason, expires, error) => {
135135
moderated_user = Some(user);
136136

137-
match error {
137+
let embed = match error {
138138
Some(err) => f
139139
.title(format!("Failed to mute {}", user.tag()))
140140
.field("Exception", err.to_string(), false)
@@ -147,16 +147,19 @@ pub async fn respond_moderation<'a>(
147147
),
148148
false,
149149
),
150-
None => f
151-
.title(format!("Muted {}", user.tag()))
152-
.field(
153-
"Action",
154-
format!("{} was muted by {}", user.mention(), author.mention()),
155-
false,
156-
),
150+
None => f.title(format!("Muted {}", user.tag())).field(
151+
"Action",
152+
format!("{} was muted by {}", user.mention(), author.mention()),
153+
false,
154+
),
155+
}
156+
.field("Reason", reason, true);
157+
158+
// add expiration date to embed if mute has a duration
159+
if let Some(expire) = expires {
160+
embed.field("Expires", expire, true);
157161
}
158-
.field("Reason", reason, true)
159-
.field("Expires", expires, true)
162+
embed
160163
},
161164
ModerationKind::Unmute(user, author, error) => {
162165
moderated_user = Some(user);
@@ -173,13 +176,11 @@ pub async fn respond_moderation<'a>(
173176
),
174177
false,
175178
),
176-
None => f
177-
.title(format!("Unmuted {}", user.tag()))
178-
.field(
179-
"Action",
180-
format!("{} was unmuted by {}", user.mention(), author.mention()),
181-
false,
182-
),
179+
None => f.title(format!("Unmuted {}", user.tag())).field(
180+
"Action",
181+
format!("{} was unmuted by {}", user.mention(), author.mention()),
182+
false,
183+
),
183184
}
184185
},
185186
ModerationKind::Ban(user, author, reason, error) => {
@@ -197,13 +198,11 @@ pub async fn respond_moderation<'a>(
197198
),
198199
false,
199200
),
200-
None => f
201-
.title(format!("Banned {}", user.tag()))
202-
.field(
203-
"Action",
204-
format!("{} was banned by {}", user.mention(), author.mention()),
205-
false,
206-
),
201+
None => f.title(format!("Banned {}", user.tag())).field(
202+
"Action",
203+
format!("{} was banned by {}", user.mention(), author.mention()),
204+
false,
205+
),
207206
};
208207
if let Some(reason) = reason {
209208
f.field("Reason", reason, true)
@@ -226,13 +225,11 @@ pub async fn respond_moderation<'a>(
226225
),
227226
false,
228227
),
229-
None => f
230-
.title(format!("Unbanned {}", user.tag()))
231-
.field(
232-
"Action",
233-
format!("{} was unbanned by {}", user.mention(), author.mention()),
234-
false,
235-
),
228+
None => f.title(format!("Unbanned {}", user.tag())).field(
229+
"Action",
230+
format!("{} was unbanned by {}", user.mention(), author.mention()),
231+
false,
232+
),
236233
}
237234
},
238235
ModerationKind::Lock(channel, author, error) => match error {
@@ -241,7 +238,11 @@ pub async fn respond_moderation<'a>(
241238
.field("Exception", err.to_string(), false)
242239
.field(
243240
"Action",
244-
format!("{} was locked by {} but failed", channel.mention(), author.mention()),
241+
format!(
242+
"{} was locked by {} but failed",
243+
channel.mention(),
244+
author.mention()
245+
),
245246
false,
246247
),
247248
None => f

0 commit comments

Comments
 (0)