working on look command

This commit is contained in:
jude 2020-09-05 21:17:45 +01:00
parent 571802c232
commit f654db76f7
3 changed files with 152 additions and 0 deletions

View File

@ -15,6 +15,7 @@ use crate::{
ChannelData, ChannelData,
GuildData, GuildData,
UserData, UserData,
Reminder,
}, },
SQLPool, SQLPool,
time_parser::TimeParser, time_parser::TimeParser,
@ -22,6 +23,14 @@ use crate::{
use chrono::NaiveDateTime; 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] #[command]
#[supports_dm(false)] #[supports_dm(false)]
@ -160,3 +169,137 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) -> CommandResult {
Ok(()) Ok(())
} }
enum TimeDisplayType {
Absolute,
Relative,
}
struct LookFlags {
pub limit: u16,
pub show_disabled: bool,
pub channel_id: Option<u64>,
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::<u16>() {
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::<u64>().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::<SQLPool>().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::<Vec<String>>().join("\n");
let _ = msg.channel_id.say(&ctx, display).await;
}
Ok(())
}

View File

@ -74,6 +74,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("dashboard", &info_cmds::DASHBOARD_COMMAND) .add_command("dashboard", &info_cmds::DASHBOARD_COMMAND)
.add_command("clock", &info_cmds::CLOCK_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("todo", &todo_cmds::TODO_PARSE_COMMAND)
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
@ -87,6 +89,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("nudge", &reminder_cmds::NUDGE_COMMAND) .add_command("nudge", &reminder_cmds::NUDGE_COMMAND)
.add_command("alias", &moderation_cmds::ALIAS_COMMAND) .add_command("alias", &moderation_cmds::ALIAS_COMMAND)
.add_command("a", &moderation_cmds::ALIAS_COMMAND)
.build(); .build();

View File

@ -205,3 +205,9 @@ SELECT value FROM strings WHERE (language = ? OR language = 'EN') AND name = ? O
self.timezone.parse().unwrap() self.timezone.parse().unwrap()
} }
} }
pub struct Reminder {
pub id: u32,
pub name: String,
pub time: u32,
}