functions to display displacements

This commit is contained in:
jude 2020-10-06 12:02:09 +01:00
parent db9b0ef2b1
commit 2d6d9da70a
4 changed files with 43 additions and 6 deletions

View File

@ -34,6 +34,7 @@ use crate::{
time_parser::TimeParser, time_parser::TimeParser,
framework::SendIterator, framework::SendIterator,
check_subscription_on_message, check_subscription_on_message,
shorthand_displacement,
}; };
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
@ -736,9 +737,11 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
Err(ReminderError::NotEnoughArgs) 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 let str_response = user_data.response(&pool, &response.to_response()).await
.replacen("{location}", &scope_id.mention(), 1) .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("{min_interval}", &MIN_INTERVAL.to_string(), 1)
.replacen("{max_time}", &MAX_TIME.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, interval,
&content).await; &content).await;
let offset = timestamp as u64 - since_epoch.as_secs();
let str_response = user_data.response(&pool, &res.to_response_natural()).await let str_response = user_data.response(&pool, &res.to_response_natural()).await
.replacen("{location}", &location_id.mention(), 1) .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("{min_interval}", &MIN_INTERVAL.to_string(), 1)
.replacen("{max_time}", &MAX_TIME.to_string(), 1); .replacen("{max_time}", &MAX_TIME.to_string(), 1);
@ -914,6 +919,7 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
-> Result<(), ReminderError> { -> Result<(), ReminderError> {
let content_string = content.to_string(); let content_string = content.to_string();
let mut nudge = 0;
let db_channel_id = match scope_id { let db_channel_id = match scope_id {
ReminderScope::User(user_id) => { ReminderScope::User(user_id) => {
@ -932,6 +938,7 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
} }
let mut channel_data = ChannelData::from_channel(channel.clone(), &pool).await.unwrap(); let mut channel_data = ChannelData::from_channel(channel.clone(), &pool).await.unwrap();
nudge = channel_data.nudge;
if let Some(guild_channel) = channel.guild() { if let Some(guild_channel) = channel.guild() {
if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() { if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() {
@ -963,7 +970,9 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
} }
else { else {
match time_parser.try_into() { match time_parser.try_into() {
Ok(time) => { Ok(time_pre) => {
let time = time_pre + nudge as i64;
let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64; let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
if time > unix_time { if time > unix_time {

View File

@ -1,2 +1,6 @@
pub const PREFIX: &str = "$"; pub const PREFIX: &str = "$";
pub const MAX_MESSAGE_LENGTH: usize = 2048; pub const MAX_MESSAGE_LENGTH: usize = 2048;
pub const DAY: u64 = 86_400;
pub const HOUR: u64 = 3_600;
pub const MINUTE: u64 = 60;

View File

@ -41,7 +41,9 @@ use std::{
use crate::{ use crate::{
framework::RegexFramework, framework::RegexFramework,
consts::PREFIX, consts::{
PREFIX, DAY, HOUR, MINUTE,
},
commands::{ commands::{
info_cmds, info_cmds,
reminder_cmds, reminder_cmds,
@ -49,6 +51,7 @@ use crate::{
moderation_cmds, moderation_cmds,
}, },
}; };
use num_integer::Integer;
struct SQLPool; struct SQLPool;
@ -173,3 +176,26 @@ pub async fn check_subscription_on_message(cache_http: impl CacheHttp + AsRef<Ca
check_subscription(&cache_http, &msg.author).await || check_subscription(&cache_http, &msg.author).await ||
if let Some(guild) = msg.guild(&cache_http).await { check_subscription(&cache_http, guild.owner_id).await } else { false } if let Some(guild) = msg.guild(&cache_http).await { check_subscription(&cache_http, guild.owner_id).await } else { false }
} }
pub fn shorthand_displacement(seconds: u64) -> 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(", ")
}

View File

@ -102,8 +102,6 @@ impl TimeParser {
} }
} }
pub fn string_displacement()
fn process_explicit(&self) -> Result<i64, InvalidTime> { fn process_explicit(&self) -> Result<i64, InvalidTime> {
let segments = self.time_string.matches('-').count(); let segments = self.time_string.matches('-').count();