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

Commit

Permalink
fix(moderation): properly schedule unmutes
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Jan 4, 2023
1 parent 6b08dfd commit 503e095
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
33 changes: 17 additions & 16 deletions src/commands/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,7 @@ pub async fn mute(
let discord = ctx.discord();
let cache = &discord.cache;

if let Some(pending_unmute) = data.pending_unmutes.get(&id.0) {
trace!("Cancelling pending unmute for {}", id.0);
pending_unmute.abort();
}

let unmute_time = if !mute_duration.is_zero() {
data.pending_unmutes.insert(
id.0,
queue_unmute_member(
discord.clone(),
data.database.clone(),
guild_id,
id,
mute.role,
mute_duration.num_seconds() as u64,
),
);
Some((now + mute_duration).timestamp() as u64)
} else {
None
Expand Down Expand Up @@ -303,6 +287,11 @@ pub async fn mute(
)
.await?;

if let Some(pending_unmute) = data.pending_unmutes.get(&id.0) {
trace!("Cancelling pending unmute for {}", id.0);
pending_unmute.abort();
}

if unmute_time.is_none() {
data.database
.update::<Muted>(
Expand All @@ -312,6 +301,18 @@ pub async fn mute(
None,
)
.await?;
} else {
data.pending_unmutes.insert(
id.0,
queue_unmute_member(
discord.clone(),
data.database.clone(),
guild_id,
id,
mute.role,
mute_duration.num_seconds() as u64,
),
);
}
Ok(())
}
Expand Down
37 changes: 13 additions & 24 deletions src/events/ready.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chrono::Utc;
use tracing::trace;

use super::*;
use crate::db::model::Muted;
Expand All @@ -24,34 +23,24 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
.await
.unwrap();

let http_ref = &ctx.http;

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

if let Ok(guild) = http_ref.get_guild(guild_id).await {
if let Ok(member) = guild.member(http_ref, member_id).await {
let amount_left = std::cmp::max(expires as i64 - Utc::now().timestamp(), 0);
let amount_left = std::cmp::max(expires as i64 - Utc::now().timestamp(), 0);

data.pending_unmutes.insert(
member.user.id.0,
queue_unmute_member(
ctx.clone(),
data.database.clone(),
member.guild_id,
member.user.id,
mute_role_id,
amount_left as u64, // i64 as u64 is handled properly here
),
);
} else {
trace!("Failed to find member {} in guild {}", member_id, guild_id);
}
} else {
trace!("Guild {} unavailable", guild_id);
}
data.pending_unmutes.insert(
user_id,
queue_unmute_member(
ctx.clone(),
data.database.clone(),
serenity::GuildId(guild_id),
serenity::UserId(user_id),
mute_role_id,
amount_left as u64, // i64 as u64 is handled properly here
),
);
}
}

0 comments on commit 503e095

Please sign in to comment.