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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "go-parse-duration"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "558b88954871f5e5b2af0e62e2e176c8bde7a6c2c4ed41b13d138d96da2e2cbd"
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
version = "0.3.16"
|
||||
@ -1297,6 +1303,7 @@ dependencies = [
|
||||
"decancer",
|
||||
"dirs",
|
||||
"dotenv",
|
||||
"go-parse-duration",
|
||||
"hmac-sha256",
|
||||
"mongodb",
|
||||
"poise",
|
||||
|
@ -34,3 +34,4 @@ tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_leve
|
||||
tracing-subscriber = "0.3.16"
|
||||
hmac-sha256 = "1.1.6"
|
||||
base64 = "0.21.0"
|
||||
go-parse-duration = "0.1.1"
|
||||
|
@ -1,5 +1,6 @@
|
||||
use bson::{doc, Document};
|
||||
use chrono::{Duration, Utc};
|
||||
use crate::utils::moderation::parse_duration;
|
||||
use mongodb::options::{UpdateModifications, UpdateOptions};
|
||||
use poise::serenity_prelude::{
|
||||
self as serenity, Mentionable, PermissionOverwrite, Permissions, UserId,
|
||||
@ -197,16 +198,11 @@ pub async fn unmute(
|
||||
}
|
||||
|
||||
/// Mute a member.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn mute(
|
||||
ctx: Context<'_>,
|
||||
#[description = "The member to mute"] member: UserId,
|
||||
#[description = "Seconds"] seconds: Option<i64>,
|
||||
#[description = "Minutes"] minutes: Option<i64>,
|
||||
#[description = "Hours"] hours: Option<i64>,
|
||||
#[description = "Days"] days: Option<i64>,
|
||||
#[description = "Months"] months: Option<i64>,
|
||||
#[description = "The member to mute"] member: User,
|
||||
#[description = "The duration of the mute"] duration: String,
|
||||
#[description = "The reason of the mute"] reason: String,
|
||||
) -> Result<(), Error> {
|
||||
let user = to_user!(member, ctx);
|
||||
@ -214,27 +210,13 @@ pub async fn mute(
|
||||
let now = Utc::now();
|
||||
let mut mute_duration = Duration::zero();
|
||||
|
||||
if let Some(seconds) = seconds {
|
||||
mute_duration = mute_duration
|
||||
.checked_add(&Duration::seconds(seconds))
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(minutes) = minutes {
|
||||
mute_duration = mute_duration
|
||||
.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();
|
||||
match parse_duration(duration) {
|
||||
Ok(duration) => {
|
||||
mute_duration = duration;
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Failed to parse duration: {:?}", err);
|
||||
}
|
||||
}
|
||||
|
||||
let data = &mut *ctx.data().write().await;
|
||||
|
@ -1,6 +1,7 @@
|
||||
use std::cmp;
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::Duration;
|
||||
use mongodb::options::FindOptions;
|
||||
use poise::serenity_prelude::{ChannelId, GuildChannel, GuildId, Mentionable, User, UserId};
|
||||
use tokio::task::JoinHandle;
|
||||
@ -18,10 +19,10 @@ use crate::{Context, Error};
|
||||
pub enum ModerationKind {
|
||||
Mute(User, User, String, Option<String>, Option<Error>), /* User, Command author, Reason, Expires, Error */
|
||||
Unmute(User, User, Option<Error>), // User, Command author, Error
|
||||
Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
|
||||
Unban(User, User, Option<SerenityError>), // User, Command author, Error
|
||||
Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
|
||||
Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
|
||||
Ban(User, User, Option<String>, Option<SerenityError>), // User, Command author, Reason, Error
|
||||
Unban(User, User, Option<SerenityError>), // User, Command author, Error
|
||||
Lock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
|
||||
Unlock(GuildChannel, User, Option<Error>), // Channel name, Command author, Error
|
||||
}
|
||||
pub enum BanKind {
|
||||
Ban(User, Option<u8>, Option<String>), // User, Amount of days to delete messages, Reason
|
||||
@ -399,3 +400,8 @@ pub async fn mute_moderation(
|
||||
|
||||
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