From 535618025c095b5252bfec623363eb3f55de2201 Mon Sep 17 00:00:00 2001 From: Leko Date: Sun, 12 Jan 2025 12:07:06 +0800 Subject: [PATCH] fix: not_long_enough --- src/i18n/en.yaml | 4 ++++ src/i18n/zh-TW.yaml | 4 ++++ src/slash_commands/invites.rs | 39 +++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/i18n/en.yaml b/src/i18n/en.yaml index 49e7985..7367a79 100644 --- a/src/i18n/en.yaml +++ b/src/i18n/en.yaml @@ -29,6 +29,10 @@ commands: title: "❌ Join Date Not Found" description: "Failed to fetch join date" footer: "This is likely a Discord API error" + not_long_enough: + title: "❌ Not Long Enough" + description: "You must be in the server for at least {days} days" + footer: "Try again in {remaining} days" success: title: "🎫 New Invite Created" description: "Here's your invite link for **{guild}**" diff --git a/src/i18n/zh-TW.yaml b/src/i18n/zh-TW.yaml index 0088100..31a82fd 100644 --- a/src/i18n/zh-TW.yaml +++ b/src/i18n/zh-TW.yaml @@ -50,6 +50,10 @@ commands: title: "❌ 找不到用戶加入日期" description: "無法取得用戶加入日期" footer: "這可能是 Discord API 錯誤" + not_long_enough: + title: "❌ 加入時間不足" + description: "您必須在伺服器中至少待 {days} 天" + footer: "請等待至少 {remaining} 天後再試" success: title: "👥 邀請資訊" user: "用戶" diff --git a/src/slash_commands/invites.rs b/src/slash_commands/invites.rs index 7569bfd..04f10c0 100644 --- a/src/slash_commands/invites.rs +++ b/src/slash_commands/invites.rs @@ -52,8 +52,12 @@ pub async fn invites(ctx: Context<'_>) -> Result<(), Error> { ); if let Some(join_date) = member.joined_at { - if Utc::now() - join_date.to_utc() < min_stay_duration { - send_error_embed(ctx, locale, "commands.invites.errors.not_long_enough").await?; + let join_date_utc = join_date.naive_utc().and_utc(); + let joined_time = Utc::now() - join_date_utc; + if joined_time < min_stay_duration { + let params = + create_not_long_enough_params(min_stay_duration.num_days(), joined_time.num_days()); + send_not_long_enough_embed(ctx, locale, params).await?; return Ok(()); } } else { @@ -173,6 +177,30 @@ async fn send_limit_reached_embed( Ok(()) } +async fn send_not_long_enough_embed( + ctx: Context<'_>, + locale: &str, + params: HashMap<&str, String>, +) -> Result<(), Error> { + let embed = CreateEmbed::default() + .title(t!(locale, "commands.invites.errors.not_long_enough.title")) + .description(t!( + locale, + "commands.invites.errors.not_long_enough.description", + params.clone() + )) + .color(0xFF3333) + .footer(CreateEmbedFooter::new(t!( + locale, + "commands.invites.errors.not_long_enough.footer", + params.clone() + ))); + + ctx.send(CreateReply::default().embed(embed).ephemeral(true)) + .await?; + Ok(()) +} + async fn send_success_embed( ctx: Context<'_>, locale: &str, @@ -224,6 +252,13 @@ fn create_limit_params(role_with_limit: &AllowedRole, used_invites: i64) -> Hash params } +fn create_not_long_enough_params<'a>(days: i64, remaining: i64) -> HashMap<&'a str, String> { + let mut params = HashMap::new(); + params.insert("days", days.to_string()); + params.insert("remaining", remaining.to_string()); + params +} + fn create_success_params<'a>( role_with_limit: &'a AllowedRole, used_invites: i64,