From 2d6d9da70ad0feb83bc62b8064415c81bb12e8c5 Mon Sep 17 00:00:00 2001 From: jude Date: Tue, 6 Oct 2020 12:02:09 +0100 Subject: [PATCH] functions to display displacements --- src/commands/reminder_cmds.rs | 15 ++++++++++++--- src/consts.rs | 4 ++++ src/main.rs | 28 +++++++++++++++++++++++++++- src/time_parser.rs | 2 -- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index bc4b682..b3c6e9d 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -34,6 +34,7 @@ use crate::{ time_parser::TimeParser, framework::SendIterator, check_subscription_on_message, + shorthand_displacement, }; use chrono::NaiveDateTime; @@ -736,9 +737,11 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem Err(ReminderError::NotEnoughArgs) }; + let offset = time_parser.map(|tp| tp.displacement().ok()).flatten().unwrap_or(0) as u64; + let str_response = user_data.response(&pool, &response.to_response()).await .replacen("{location}", &scope_id.mention(), 1) - .replacen("{offset}", &time_parser.map(|tp| tp.displacement().ok()).flatten().unwrap_or(-1).to_string(), 1) + .replacen("{offset}", &shorthand_displacement(offset), 1) .replacen("{min_interval}", &MIN_INTERVAL.to_string(), 1) .replacen("{max_time}", &MAX_TIME.to_string(), 1); @@ -852,9 +855,11 @@ async fn natural(ctx: &Context, msg: &Message, args: String) -> CommandResult { interval, &content).await; + let offset = timestamp as u64 - since_epoch.as_secs(); + let str_response = user_data.response(&pool, &res.to_response_natural()).await .replacen("{location}", &location_id.mention(), 1) - .replacen("{offset}", &(timestamp as u64 - since_epoch.as_secs()).to_string(), 1) + .replacen("{offset}", &shorthand_displacement(offset), 1) .replacen("{min_interval}", &MIN_INTERVAL.to_string(), 1) .replacen("{max_time}", &MAX_TIME.to_string(), 1); @@ -914,6 +919,7 @@ async fn create_reminder, S: ToString + Type + Encode Result<(), ReminderError> { let content_string = content.to_string(); + let mut nudge = 0; let db_channel_id = match scope_id { ReminderScope::User(user_id) => { @@ -932,6 +938,7 @@ async fn create_reminder, S: ToString + Type + Encode, S: ToString + Type + Encode { + Ok(time_pre) => { + let time = time_pre + nudge as i64; + let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64; if time > unix_time { diff --git a/src/consts.rs b/src/consts.rs index bf39f7f..b6defb1 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,2 +1,6 @@ pub const PREFIX: &str = "$"; pub const MAX_MESSAGE_LENGTH: usize = 2048; + +pub const DAY: u64 = 86_400; +pub const HOUR: u64 = 3_600; +pub const MINUTE: u64 = 60; diff --git a/src/main.rs b/src/main.rs index f40cc8e..53a34c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,9 @@ use std::{ use crate::{ framework::RegexFramework, - consts::PREFIX, + consts::{ + PREFIX, DAY, HOUR, MINUTE, + }, commands::{ info_cmds, reminder_cmds, @@ -49,6 +51,7 @@ use crate::{ moderation_cmds, }, }; +use num_integer::Integer; struct SQLPool; @@ -173,3 +176,26 @@ pub async fn check_subscription_on_message(cache_http: impl CacheHttp + AsRef String { + let (hours, seconds) = seconds.div_rem(&HOUR); + let (minutes, seconds) = seconds.div_rem(&MINUTE); + + format!("{:02}:{:02}:{:02}", hours, minutes, seconds) +} + +pub fn longhand_displacement(seconds: u64) -> String { + let (days, seconds) = seconds.div_rem(&DAY); + let (hours, seconds) = seconds.div_rem(&HOUR); + let (minutes, seconds) = seconds.div_rem(&MINUTE); + + let mut sections = vec![]; + + for (var, name) in [days, hours, minutes, seconds].iter().zip(["days", "hours", "minutes", "seconds"].iter()) { + if *var > 0 { + sections.push(format!("{} {}", var, name)); + } + } + + sections.join(", ") +} diff --git a/src/time_parser.rs b/src/time_parser.rs index a569fda..7bb158b 100644 --- a/src/time_parser.rs +++ b/src/time_parser.rs @@ -102,8 +102,6 @@ impl TimeParser { } } - pub fn string_displacement() - fn process_explicit(&self) -> Result { let segments = self.time_string.matches('-').count();