look command now shows interval

This commit is contained in:
jellywx 2021-03-27 13:12:08 +00:00
parent c76a456af5
commit 72a976dea8
3 changed files with 77 additions and 80 deletions

2
Cargo.lock generated
View File

@ -1271,7 +1271,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.10" version = "1.4.12"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.10" version = "1.4.12"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -1,5 +1,7 @@
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,
@ -45,7 +47,9 @@ use std::{
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use crate::models::MeridianType;
use regex::Captures; use regex::Captures;
use serenity::model::channel::Channel;
fn shorthand_displacement(seconds: u64) -> String { fn shorthand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY); let (days, seconds) = seconds.div_rem(&DAY);
@ -342,6 +346,7 @@ impl LookFlags {
struct LookReminder { struct LookReminder {
id: u32, id: u32,
time: u32, time: u32,
interval: Option<u32>,
channel: u64, channel: u64,
content: String, content: String,
description: Option<String>, description: Option<String>,
@ -355,6 +360,47 @@ impl LookReminder {
self.description.clone().unwrap_or(String::from("")) self.description.clone().unwrap_or(String::from(""))
} }
} }
fn display(
&self,
flags: &LookFlags,
meridian: &MeridianType,
timezone: &Tz,
inter: &str,
) -> String {
let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone
.timestamp(self.time as i64, 0)
.format(meridian.fmt_str())
.to_string(),
TimeDisplayType::Relative => {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
longhand_displacement((self.time as u64).checked_sub(now).unwrap_or(1))
}
};
if let Some(interval) = self.interval {
format!(
"'{}' *{}* **{}**, repeating every **{}**",
self.display_content(),
&inter,
time_display,
longhand_displacement(interval as u64)
)
} else {
format!(
"'{}' *{}* **{}**",
self.display_content(),
&inter,
time_display
)
}
}
} }
#[command("look")] #[command("look")]
@ -370,16 +416,25 @@ async fn look(ctx: &Context, msg: &Message, args: String) {
let enabled = if flags.show_disabled { "0,1" } else { "1" }; let enabled = if flags.show_disabled { "0,1" } else { "1" };
let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) { let channel_opt = msg.channel_id.to_channel_cached(&ctx).await;
let channel_id = flags
.channel_id
.unwrap_or_else(|| msg.channel_id.as_u64().to_owned());
sqlx::query_as!( let channel_id = if let Some(Channel::Guild(channel)) = channel_opt {
LookReminder, if Some(channel.guild_id) == msg.guild_id {
" flags
.channel_id
.unwrap_or_else(|| msg.channel_id.as_u64().to_owned())
} else {
msg.channel_id.as_u64().to_owned()
}
} else {
msg.channel_id.as_u64().to_owned()
};
let reminders = sqlx::query_as!(
LookReminder,
"
SELECT SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description
FROM FROM
reminders reminders
INNER JOIN INNER JOIN
@ -395,7 +450,6 @@ LEFT JOIN
ON ON
embeds.id = messages.embed_id embeds.id = messages.embed_id
WHERE WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?) AND
channels.channel = ? AND channels.channel = ? AND
FIND_IN_SET(reminders.enabled, ?) FIND_IN_SET(reminders.enabled, ?)
ORDER BY ORDER BY
@ -403,48 +457,12 @@ ORDER BY
LIMIT LIMIT
? ?
", ",
guild_id, channel_id,
channel_id, enabled,
enabled, flags.limit
flags.limit )
) .fetch_all(&pool)
.fetch_all(&pool) .await
.await
} else {
sqlx::query_as_unchecked!(
LookReminder,
"
SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description
FROM
reminders
LEFT OUTER JOIN
channels
ON
channels.id = reminders.channel_id
INNER JOIN
messages
ON
messages.id = reminders.message_id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
WHERE
channels.channel = ? AND
FIND_IN_SET(reminders.enabled, ?)
ORDER BY
reminders.time
LIMIT
?
",
msg.channel_id.as_u64(),
enabled,
flags.limit
)
.fetch_all(&pool)
.await
}
.unwrap(); .unwrap();
if reminders.is_empty() { if reminders.is_empty() {
@ -455,30 +473,9 @@ LIMIT
} else { } else {
let inter = lm.get(&language, "look/inter"); let inter = lm.get(&language, "look/inter");
let display = reminders.iter().map(|reminder| { let display = reminders
let time_display = match flags.time_display { .iter()
TimeDisplayType::Absolute => timezone .map(|reminder| reminder.display(&flags, &meridian, &timezone, &inter));
.timestamp(reminder.time as i64, 0)
.format(meridian.fmt_str())
.to_string(),
TimeDisplayType::Relative => {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
longhand_displacement((reminder.time as u64).checked_sub(now).unwrap_or(1))
}
};
format!(
"'{}' *{}* **{}**",
reminder.display_content(),
&inter,
time_display
)
});
let _ = msg.channel_id.say_lines(&ctx, display).await; let _ = msg.channel_id.say_lines(&ctx, display).await;
} }
@ -512,7 +509,7 @@ async fn delete(ctx: &Context, msg: &Message, _args: String) {
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description
FROM FROM
reminders reminders
LEFT OUTER JOIN LEFT OUTER JOIN
@ -539,7 +536,7 @@ WHERE
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description
FROM FROM
reminders reminders
LEFT OUTER JOIN LEFT OUTER JOIN
@ -567,7 +564,7 @@ WHERE
LookReminder, LookReminder,
" "
SELECT SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description reminders.id, reminders.time, reminders.interval, channels.channel, messages.content, embeds.description
FROM FROM
reminders reminders
INNER JOIN INNER JOIN