From d62c8c95c265a22032bfcfa8846fc47180b53285 Mon Sep 17 00:00:00 2001 From: jellywx Date: Tue, 1 Feb 2022 23:41:28 +0000 Subject: [PATCH] support months in sender --- src/commands/reminder_cmds.rs | 2 +- src/component_models/mod.rs | 3 +-- src/sender.rs | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 82b6455..c1731dd 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -323,7 +323,7 @@ async fn look(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) { .iter() .map(|reminder| reminder.display(&flags, &timezone)) .fold(0, |t, r| t + r.len()) - .div_ceil(&EMBED_DESCRIPTION_MAX_LENGTH); + .div_ceil(EMBED_DESCRIPTION_MAX_LENGTH); let pager = LookPager::new(flags, timezone); diff --git a/src/component_models/mod.rs b/src/component_models/mod.rs index 18e0c2e..bf16b2b 100644 --- a/src/component_models/mod.rs +++ b/src/component_models/mod.rs @@ -3,7 +3,6 @@ pub(crate) mod pager; use std::io::Cursor; use chrono_tz::Tz; -use num_integer::Integer; use rmp_serde::Serializer; use serde::{Deserialize, Serialize}; use serenity::{ @@ -79,7 +78,7 @@ impl ComponentDataModel { .iter() .map(|reminder| reminder.display(&flags, &pager.timezone)) .fold(0, |t, r| t + r.len()) - .div_ceil(&EMBED_DESCRIPTION_MAX_LENGTH); + .div_ceil(EMBED_DESCRIPTION_MAX_LENGTH); let channel_name = if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx) { diff --git a/src/sender.rs b/src/sender.rs index 8a04631..d2792e3 100644 --- a/src/sender.rs +++ b/src/sender.rs @@ -257,7 +257,8 @@ pub struct Reminder { timezone: String, restartable: bool, expires: Option, - interval: Option, + interval_seconds: Option, + interval_months: Option, avatar: Option, username: Option, @@ -289,7 +290,8 @@ SELECT reminders.`timezone` AS timezone, reminders.`restartable` AS restartable, reminders.`expires` AS expires, - reminders.`interval` AS 'interval', + reminders.`interval_seconds` AS 'interval_seconds', + reminders.`interval_months` AS 'interval_months', reminders.`avatar` AS avatar, reminders.`username` AS username @@ -327,12 +329,27 @@ UPDATE channels SET webhook_id = NULL, webhook_token = NULL WHERE channel = ? } async fn refresh(&self, pool: &MySqlPool) { - if let Some(interval) = self.interval { + if self.interval_seconds.is_some() || self.interval_months.is_some() { let now = Utc::now().naive_local(); let mut updated_reminder_time = self.utc_time; - while updated_reminder_time < now { - updated_reminder_time += Duration::seconds(interval as i64); + if let Some(interval) = self.interval_months { + let row = sqlx::query!( + "SELECT DATE_ADD(?, INTERVAL ? MONTH) AS new_time", + updated_reminder_time, + interval + ) + .fetch_one(pool) + .await + .unwrap(); + + updated_reminder_time = row.new_time.unwrap(); + } + + if let Some(interval) = self.interval_seconds { + while updated_reminder_time < now { + updated_reminder_time += Duration::seconds(interval as i64); + } } if self.expires.map_or(false, |expires| {