mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-05-08 10:04:24 +02:00
feat: use go-parse-duration
for time parameters
This commit is contained in:
parent
66c64e2950
commit
323611568d
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -588,6 +588,12 @@ dependencies = [
|
|||||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "go-parse-duration"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "558b88954871f5e5b2af0e62e2e176c8bde7a6c2c4ed41b13d138d96da2e2cbd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "h2"
|
name = "h2"
|
||||||
version = "0.3.16"
|
version = "0.3.16"
|
||||||
@ -1297,6 +1303,7 @@ dependencies = [
|
|||||||
"decancer",
|
"decancer",
|
||||||
"dirs",
|
"dirs",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
"go-parse-duration",
|
||||||
"hmac-sha256",
|
"hmac-sha256",
|
||||||
"mongodb",
|
"mongodb",
|
||||||
"poise",
|
"poise",
|
||||||
|
@ -34,3 +34,4 @@ tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_leve
|
|||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = "0.3.16"
|
||||||
hmac-sha256 = "1.1.6"
|
hmac-sha256 = "1.1.6"
|
||||||
base64 = "0.21.0"
|
base64 = "0.21.0"
|
||||||
|
go-parse-duration = "0.1.1"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use bson::{doc, Document};
|
use bson::{doc, Document};
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
|
use crate::utils::moderation::parse_duration;
|
||||||
use mongodb::options::{UpdateModifications, UpdateOptions};
|
use mongodb::options::{UpdateModifications, UpdateOptions};
|
||||||
use poise::serenity_prelude::{
|
use poise::serenity_prelude::{
|
||||||
self as serenity, Mentionable, PermissionOverwrite, Permissions, UserId,
|
self as serenity, Mentionable, PermissionOverwrite, Permissions, UserId,
|
||||||
@ -197,16 +198,11 @@ pub async fn unmute(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Mute a member.
|
/// Mute a member.
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn mute(
|
pub async fn mute(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "The member to mute"] member: UserId,
|
#[description = "The member to mute"] member: User,
|
||||||
#[description = "Seconds"] seconds: Option<i64>,
|
#[description = "The duration of the mute"] duration: String,
|
||||||
#[description = "Minutes"] minutes: Option<i64>,
|
|
||||||
#[description = "Hours"] hours: Option<i64>,
|
|
||||||
#[description = "Days"] days: Option<i64>,
|
|
||||||
#[description = "Months"] months: Option<i64>,
|
|
||||||
#[description = "The reason of the mute"] reason: String,
|
#[description = "The reason of the mute"] reason: String,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let user = to_user!(member, ctx);
|
let user = to_user!(member, ctx);
|
||||||
@ -214,27 +210,13 @@ pub async fn mute(
|
|||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let mut mute_duration = Duration::zero();
|
let mut mute_duration = Duration::zero();
|
||||||
|
|
||||||
if let Some(seconds) = seconds {
|
match parse_duration(duration) {
|
||||||
mute_duration = mute_duration
|
Ok(duration) => {
|
||||||
.checked_add(&Duration::seconds(seconds))
|
mute_duration = duration;
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
if let Some(minutes) = minutes {
|
Err(err) => {
|
||||||
mute_duration = mute_duration
|
error!("Failed to parse duration: {:?}", err);
|
||||||
.checked_add(&Duration::minutes(minutes))
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
if let Some(hours) = hours {
|
|
||||||
mute_duration = mute_duration.checked_add(&Duration::hours(hours)).unwrap();
|
|
||||||
}
|
|
||||||
if let Some(days) = days {
|
|
||||||
mute_duration = mute_duration.checked_add(&Duration::days(days)).unwrap();
|
|
||||||
}
|
|
||||||
if let Some(months) = months {
|
|
||||||
const DAYS_IN_MONTH: i64 = 30;
|
|
||||||
mute_duration = mute_duration
|
|
||||||
.checked_add(&Duration::days(months * DAYS_IN_MONTH))
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = &mut *ctx.data().write().await;
|
let data = &mut *ctx.data().write().await;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use chrono::Duration;
|
||||||
use mongodb::options::FindOptions;
|
use mongodb::options::FindOptions;
|
||||||
use poise::serenity_prelude::{ChannelId, GuildChannel, GuildId, Mentionable, User, UserId};
|
use poise::serenity_prelude::{ChannelId, GuildChannel, GuildId, Mentionable, User, UserId};
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
@ -399,3 +400,8 @@ pub async fn mute_moderation(
|
|||||||
|
|
||||||
Ok((is_currently_muted, removed_roles))
|
Ok((is_currently_muted, removed_roles))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_duration(duration: String) -> Result<Duration, go_parse_duration::Error> {
|
||||||
|
let d = go_parse_duration::parse_duration(&duration)?;
|
||||||
|
Ok(Duration::nanoseconds(d))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user