From f654db76f7091b47c0f584ccce9cd4303e8770e3 Mon Sep 17 00:00:00 2001 From: jude Date: Sat, 5 Sep 2020 21:17:45 +0100 Subject: [PATCH] working on look command --- src/commands/reminder_cmds.rs | 143 ++++++++++++++++++++++++++++++++++ src/main.rs | 3 + src/models.rs | 6 ++ 3 files changed, 152 insertions(+) diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 2265da9..5b0992a 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -15,6 +15,7 @@ use crate::{ ChannelData, GuildData, UserData, + Reminder, }, SQLPool, time_parser::TimeParser, @@ -22,6 +23,14 @@ use crate::{ use chrono::NaiveDateTime; +use regex::Regex; + +use std::default::Default; + +lazy_static! { + static ref REGEX_CHANNEL: Regex = Regex::new(r#"^\s*<#(\d+)>\s*$"#).unwrap(); +} + #[command] #[supports_dm(false)] @@ -160,3 +169,137 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) -> CommandResult { Ok(()) } + +enum TimeDisplayType { + Absolute, + Relative, +} + +struct LookFlags { + pub limit: u16, + pub show_disabled: bool, + pub channel_id: Option, + time_display: TimeDisplayType, +} + +impl Default for LookFlags { + fn default() -> Self { + Self { + limit: u16::MAX, + show_disabled: true, + channel_id: None, + time_display: TimeDisplayType::Relative + } + } +} + +impl LookFlags { + fn from_string(args: &str) -> Self { + let mut new_flags: Self = Default::default(); + + for arg in args.split(" ") { + match arg { + "enabled" => { + new_flags.show_disabled = false; + }, + + "time" => { + new_flags.time_display = TimeDisplayType::Absolute; + }, + + param => { + if let Ok(val) = param.parse::() { + new_flags.limit = val; + } + else { + new_flags.channel_id = REGEX_CHANNEL.captures(&args) + .map(|cap| cap.get(1)) + .flatten() + .map(|c| c.as_str().parse::().unwrap()); + } + } + } + } + + new_flags + } + + fn display_time(&self, timestamp: u64) -> String { + + String::from("") + } +} + +#[command] +async fn look(ctx: &Context, msg: &Message, args: String) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let user_data = UserData::from_id(&msg.author, &ctx, &pool).await.unwrap(); + + let flags = LookFlags::from_string(&args); + + let enabled = if flags.show_disabled { None } else { Some(false) }; + + let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) { + let channel_id = flags.channel_id.unwrap_or(msg.channel_id.as_u64().to_owned()); + + sqlx::query_as!(Reminder, + " +SELECT + reminders.id, reminders.time, reminders.name +FROM + reminders +INNER JOIN + channels +ON + reminders.channel_id = channels.id +WHERE + channels.guild_id = (SELECT id FROM guilds WHERE guild = ?) AND + channels.channel = ? AND + reminders.enabled != ? +LIMIT + ? + ", guild_id, channel_id, enabled, flags.limit) + .fetch_all(&pool) + .await + .unwrap() + } + else { + sqlx::query_as!(Reminder, + " +SELECT + reminders.id, reminders.time, reminders.name +FROM + reminders +INNER JOIN + channels +ON + reminders.channel_id = channels.id +WHERE + channels.channel = ? AND + reminders.enabled != ? +LIMIT + ? + ", msg.channel_id.as_u64(), enabled, flags.limit) + .fetch_all(&pool) + .await + .unwrap() + }; + + if reminders.len() == 0 { + let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "look/no_reminders").await).await; + } + else { + let inter = user_data.response(&pool, "look/inter").await; + + let display = reminders + .iter() + .map(|reminder| format!("'{}' *{}* **{}**", reminder.name, &inter, reminder.time)) + .collect::>().join("\n"); + + let _ = msg.channel_id.say(&ctx, display).await; + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 135075e..6080de5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,8 @@ async fn main() -> Result<(), Box> { .add_command("dashboard", &info_cmds::DASHBOARD_COMMAND) .add_command("clock", &info_cmds::CLOCK_COMMAND) + .add_command("look", &reminder_cmds::LOOK_COMMAND) + .add_command("todo", &todo_cmds::TODO_PARSE_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) @@ -87,6 +89,7 @@ async fn main() -> Result<(), Box> { .add_command("nudge", &reminder_cmds::NUDGE_COMMAND) .add_command("alias", &moderation_cmds::ALIAS_COMMAND) + .add_command("a", &moderation_cmds::ALIAS_COMMAND) .build(); diff --git a/src/models.rs b/src/models.rs index 1470ffc..c3b9a45 100644 --- a/src/models.rs +++ b/src/models.rs @@ -205,3 +205,9 @@ SELECT value FROM strings WHERE (language = ? OR language = 'EN') AND name = ? O self.timezone.parse().unwrap() } } + +pub struct Reminder { + pub id: u32, + pub name: String, + pub time: u32, +}