use discord timestamp formatting
This commit is contained in:
parent
320060b1bd
commit
f02c04b313
@ -1,7 +1,5 @@
|
|||||||
use regex_command_attr::command;
|
use regex_command_attr::command;
|
||||||
|
|
||||||
use chrono_tz::Tz;
|
|
||||||
|
|
||||||
use serenity::{
|
use serenity::{
|
||||||
cache::Cache,
|
cache::Cache,
|
||||||
client::Context,
|
client::Context,
|
||||||
@ -49,18 +47,9 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use regex::Captures;
|
use regex::Captures;
|
||||||
|
|
||||||
use ring::hmac;
|
use ring::hmac;
|
||||||
|
|
||||||
fn shorthand_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 time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds);
|
|
||||||
|
|
||||||
format!("{} days, {}", days, time_repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn longhand_displacement(seconds: u64) -> String {
|
fn longhand_displacement(seconds: u64) -> String {
|
||||||
let (days, seconds) = seconds.div_rem(&DAY);
|
let (days, seconds) = seconds.div_rem(&DAY);
|
||||||
let (hours, seconds) = seconds.div_rem(&HOUR);
|
let (hours, seconds) = seconds.div_rem(&HOUR);
|
||||||
@ -397,27 +386,11 @@ impl LookReminder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display(
|
fn display(&self, flags: &LookFlags, inter: &str) -> String {
|
||||||
&self,
|
|
||||||
flags: &LookFlags,
|
|
||||||
meridian: &MeridianType,
|
|
||||||
timezone: &Tz,
|
|
||||||
inter: &str,
|
|
||||||
) -> String {
|
|
||||||
let time_display = match flags.time_display {
|
let time_display = match flags.time_display {
|
||||||
TimeDisplayType::Absolute => timezone
|
TimeDisplayType::Absolute => format!("<t:{}>", self.time.timestamp()),
|
||||||
.timestamp(self.time.timestamp(), 0)
|
|
||||||
.format(meridian.fmt_str())
|
|
||||||
.to_string(),
|
|
||||||
|
|
||||||
TimeDisplayType::Relative => {
|
TimeDisplayType::Relative => format!("<t:{}:R>", self.time.timestamp()),
|
||||||
let now = SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.unwrap()
|
|
||||||
.as_secs();
|
|
||||||
|
|
||||||
longhand_displacement((self.time.timestamp() as u64).checked_sub(now).unwrap_or(1))
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(interval) = self.interval {
|
if let Some(interval) = self.interval {
|
||||||
@ -451,8 +424,6 @@ async fn look(ctx: &Context, msg: &Message, args: String) {
|
|||||||
let (pool, lm) = get_ctx_data(&ctx).await;
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
||||||
|
|
||||||
let language = UserData::language_of(&msg.author, &pool).await;
|
let language = UserData::language_of(&msg.author, &pool).await;
|
||||||
let timezone = UserData::timezone_of(&msg.author, &pool).await;
|
|
||||||
let meridian = UserData::meridian_of(&msg.author, &pool).await;
|
|
||||||
|
|
||||||
let flags = LookFlags::from_string(&args);
|
let flags = LookFlags::from_string(&args);
|
||||||
|
|
||||||
@ -519,7 +490,7 @@ LIMIT
|
|||||||
|
|
||||||
let display = reminders
|
let display = reminders
|
||||||
.iter()
|
.iter()
|
||||||
.map(|reminder| reminder.display(&flags, &meridian, &timezone, &inter));
|
.map(|reminder| reminder.display(&flags, &inter));
|
||||||
|
|
||||||
let _ = msg.channel_id.say_lines(&ctx, display).await;
|
let _ = msg.channel_id.say_lines(&ctx, display).await;
|
||||||
}
|
}
|
||||||
@ -643,14 +614,13 @@ WHERE
|
|||||||
|
|
||||||
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
|
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
|
||||||
reminder_ids.push(reminder.id);
|
reminder_ids.push(reminder.id);
|
||||||
let time = user_data.timezone().timestamp(reminder.time.timestamp(), 0);
|
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"**{}**: '{}' *<#{}>* at {}",
|
"**{}**: '{}' *<#{}>* at <t:{}>",
|
||||||
count + 1,
|
count + 1,
|
||||||
reminder.display_content(),
|
reminder.display_content(),
|
||||||
reminder.channel,
|
reminder.channel,
|
||||||
time.format(user_data.meridian().fmt_str())
|
reminder.time.timestamp()
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1292,9 +1262,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
|
|||||||
.replace("{location}", &ok_locations[0].mention())
|
.replace("{location}", &ok_locations[0].mention())
|
||||||
.replace(
|
.replace(
|
||||||
"{offset}",
|
"{offset}",
|
||||||
&shorthand_displacement(
|
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
|
||||||
time_parser.displacement().unwrap() as u64,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
n => lm
|
n => lm
|
||||||
.get(&language, "remind/success_bulk")
|
.get(&language, "remind/success_bulk")
|
||||||
@ -1309,9 +1277,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
|
|||||||
)
|
)
|
||||||
.replace(
|
.replace(
|
||||||
"{offset}",
|
"{offset}",
|
||||||
&shorthand_displacement(
|
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
|
||||||
time_parser.displacement().unwrap() as u64,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1418,11 +1384,6 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
|
|||||||
async fn natural(ctx: &Context, msg: &Message, args: String) {
|
async fn natural(ctx: &Context, msg: &Message, args: String) {
|
||||||
let (pool, lm) = get_ctx_data(&ctx).await;
|
let (pool, lm) = get_ctx_data(&ctx).await;
|
||||||
|
|
||||||
let now = SystemTime::now();
|
|
||||||
let since_epoch = now
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("Time calculated as going backwards. Very bad");
|
|
||||||
|
|
||||||
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
|
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
|
||||||
|
|
||||||
match REGEX_NATURAL_COMMAND_1.captures(&args) {
|
match REGEX_NATURAL_COMMAND_1.captures(&args) {
|
||||||
@ -1486,8 +1447,6 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
|
|||||||
|
|
||||||
match content_res {
|
match content_res {
|
||||||
Ok(mut content) => {
|
Ok(mut content) => {
|
||||||
let offset = timestamp as u64 - since_epoch.as_secs();
|
|
||||||
|
|
||||||
let mut ok_locations = vec![];
|
let mut ok_locations = vec![];
|
||||||
let mut err_locations = vec![];
|
let mut err_locations = vec![];
|
||||||
let mut err_types = HashSet::new();
|
let mut err_types = HashSet::new();
|
||||||
@ -1519,7 +1478,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
|
|||||||
1 => lm
|
1 => lm
|
||||||
.get(&user_data.language, "remind/success")
|
.get(&user_data.language, "remind/success")
|
||||||
.replace("{location}", &ok_locations[0].mention())
|
.replace("{location}", &ok_locations[0].mention())
|
||||||
.replace("{offset}", &shorthand_displacement(offset)),
|
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
|
||||||
n => lm
|
n => lm
|
||||||
.get(&user_data.language, "remind/success_bulk")
|
.get(&user_data.language, "remind/success_bulk")
|
||||||
.replace("{number}", &n.to_string())
|
.replace("{number}", &n.to_string())
|
||||||
@ -1531,7 +1490,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
|
|||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(", "),
|
.join(", "),
|
||||||
)
|
)
|
||||||
.replace("{offset}", &shorthand_displacement(offset)),
|
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let error_part = format!(
|
let error_part = format!(
|
||||||
|
Loading…
Reference in New Issue
Block a user