mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-05-16 12:57:06 +02:00
refactor: remove unnecessary mutex
This commit is contained in:
parent
3e18d304a3
commit
3f1d6af569
@ -18,7 +18,7 @@ pub async fn unmute(
|
||||
let data = &ctx.data().read().await;
|
||||
let configuration = &data.configuration;
|
||||
|
||||
if let Some(pending_unmute) = data.pending_unmutes.lock().await.get(&member.user.id.0) {
|
||||
if let Some(pending_unmute) = data.pending_unmutes.get(&member.user.id.0) {
|
||||
trace!("Cancelling pending unmute for {}", member.user.id.0);
|
||||
pending_unmute.abort();
|
||||
}
|
||||
@ -82,7 +82,7 @@ pub async fn mute(
|
||||
|
||||
let unmute_time = now + mute_duration;
|
||||
|
||||
let data = ctx.data().read().await;
|
||||
let data = &mut *ctx.data().write().await;
|
||||
let configuration = &data.configuration;
|
||||
let embed_color = configuration.general.embed_color;
|
||||
let mute = &configuration.general.mute;
|
||||
@ -149,29 +149,25 @@ pub async fn mute(
|
||||
}
|
||||
};
|
||||
|
||||
let mut pending_unmute = data.pending_unmutes.lock().await;
|
||||
if let Some(pending_unmute) = pending_unmute.get(&member.user.id.0) {
|
||||
if let Some(pending_unmute) = data.pending_unmutes.get(&member.user.id.0) {
|
||||
trace!("Cancelling pending unmute for {}", member.user.id.0);
|
||||
pending_unmute.abort();
|
||||
}
|
||||
|
||||
let r = queue_unmute_member(
|
||||
data.pending_unmutes.insert(
|
||||
member.user.id.0,
|
||||
queue_unmute_member(
|
||||
ctx.discord(),
|
||||
&data.database,
|
||||
&member,
|
||||
mute_role_id,
|
||||
mute_duration.num_seconds() as u64,
|
||||
),
|
||||
);
|
||||
|
||||
pending_unmute.insert(member.user.id.0, r);
|
||||
|
||||
respond_mute_command(
|
||||
&ctx,
|
||||
ModerationKind::Mute(
|
||||
reason,
|
||||
format!("<t:{}:F>", unmute_time.timestamp()),
|
||||
result,
|
||||
),
|
||||
ModerationKind::Mute(reason, format!("<t:{}:F>", unmute_time.timestamp()), result),
|
||||
&member.user,
|
||||
embed_color,
|
||||
)
|
||||
|
@ -8,12 +8,11 @@ use crate::utils::moderation::queue_unmute_member;
|
||||
|
||||
pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
||||
let data = get_data_lock(ctx).await;
|
||||
let data = data.read().await;
|
||||
let database = &data.database;
|
||||
let data = &mut *data.write().await;
|
||||
let mute_role_id = data.configuration.general.mute.role;
|
||||
let mut pending_unmutes = data.pending_unmutes.lock().await;
|
||||
|
||||
let mut cursor = database
|
||||
let mut cursor = data
|
||||
.database
|
||||
.find::<Muted>(
|
||||
"muted",
|
||||
Muted {
|
||||
@ -37,11 +36,11 @@ pub async fn load_muted_members(ctx: &serenity::Context, _: &serenity::Ready) {
|
||||
let amount_left =
|
||||
std::cmp::max(current.expires.unwrap() as i64 - Utc::now().timestamp(), 0);
|
||||
|
||||
pending_unmutes.insert(
|
||||
data.pending_unmutes.insert(
|
||||
member.user.id.0,
|
||||
queue_unmute_member(
|
||||
ctx,
|
||||
database,
|
||||
&data.database,
|
||||
&member,
|
||||
mute_role_id,
|
||||
amount_left as u64, // i64 as u64 is handled properly here
|
||||
|
@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||
use commands::{configuration, moderation};
|
||||
use db::database::Database;
|
||||
use events::Handler;
|
||||
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, UserId};
|
||||
use poise::serenity_prelude::{self as serenity, RwLock, UserId};
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::{error, trace};
|
||||
use utils::bot::load_configuration;
|
||||
@ -29,7 +29,7 @@ impl serenity::TypeMapKey for Data {
|
||||
pub struct Data {
|
||||
configuration: Configuration,
|
||||
database: Database,
|
||||
pending_unmutes: Mutex<HashMap<u64, JoinHandle<Option<Error>>>>,
|
||||
pending_unmutes: HashMap<u64, JoinHandle<Option<Error>>>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -71,7 +71,7 @@ async fn main() {
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
pending_unmutes: Mutex::new(HashMap::new()),
|
||||
pending_unmutes: HashMap::new(),
|
||||
}));
|
||||
|
||||
let handler = Arc::new(Handler::new(
|
||||
|
@ -10,7 +10,7 @@ pub enum ModerationKind {
|
||||
Unmute(Option<Error>), // Error
|
||||
}
|
||||
|
||||
pub fn queue_unmute_member(
|
||||
pub fn queue_unmute_member<'a>(
|
||||
ctx: &serenity::Context,
|
||||
database: &Database,
|
||||
member: &Member,
|
||||
|
Loading…
x
Reference in New Issue
Block a user