From 6311099cc6db36d117512b43e86f50ef20d6a22b Mon Sep 17 00:00:00 2001 From: jellywx Date: Wed, 16 Dec 2020 16:53:13 +0000 Subject: [PATCH] meridian command for switching from 12/24 format --- 1.3.0-changelog | 3 +- create.sql | 4 ++- src/commands/info_cmds.rs | 27 +++++--------- src/commands/moderation_cmds.rs | 62 +++++++++++++++++++++++++++++++++ src/commands/reminder_cmds.rs | 14 ++++---- src/consts.rs | 3 +- src/main.rs | 1 + src/models.rs | 45 ++++++++++++++++++++++-- 8 files changed, 127 insertions(+), 32 deletions(-) diff --git a/1.3.0-changelog b/1.3.0-changelog index 6d86f92..43331c7 100644 --- a/1.3.0-changelog +++ b/1.3.0-changelog @@ -28,7 +28,8 @@ - General changes: + More command outputs use embeds to look nicer + Commands that fail provide help outputs that are the same as the help command - + Remind commands now provide output that is more detailed. This includes showing the exact errors that occurred for each reminder that couldn't be set, if bulk-setting reminders + + Remind commands now provide output that is more detailed + Translations are now loaded from a JSON file included within the executable + Reduced user caching by individually querying some attributes like language and timezone + Roles can now be silently mentioned with <> + + The number of days will show even if zero to improve clarity diff --git a/create.sql b/create.sql index 3216045..f111961 100644 --- a/create.sql +++ b/create.sql @@ -50,9 +50,11 @@ CREATE TABLE reminders.users ( language VARCHAR(2) DEFAULT 'EN' NOT NULL, timezone VARCHAR(32) DEFAULT 'UTC' NOT NULL, + meridian_time BOOLEAN DEFAULT 0 NOT NULL, + allowed_dm BOOLEAN DEFAULT 1 NOT NULL, - patreon BOOL NOT NULL DEFAULT 0, + patreon BOOLEAN NOT NULL DEFAULT 0, PRIMARY KEY (id), FOREIGN KEY (dm_channel) REFERENCES reminders.channels(id) ON DELETE RESTRICT diff --git a/src/commands/info_cmds.rs b/src/commands/info_cmds.rs index 73454fd..b27028f 100644 --- a/src/commands/info_cmds.rs +++ b/src/commands/info_cmds.rs @@ -239,7 +239,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) { } #[command] -async fn clock(ctx: &Context, msg: &Message, args: String) { +async fn clock(ctx: &Context, msg: &Message, _args: String) { let data = ctx.data.read().await; let pool = data @@ -251,26 +251,17 @@ async fn clock(ctx: &Context, msg: &Message, args: String) { 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 now = Utc::now().with_timezone(&timezone); let clock_display = lm.get(&language, "clock/time"); - if args == "12" { - let _ = msg - .channel_id - .say( - &ctx, - clock_display.replacen("{}", &now.format("%I:%M:%S %p").to_string(), 1), - ) - .await; - } else { - let _ = msg - .channel_id - .say( - &ctx, - clock_display.replacen("{}", &now.format("%H:%M:%S").to_string(), 1), - ) - .await; - } + let _ = msg + .channel_id + .say( + &ctx, + clock_display.replacen("{}", &now.format(meridian.fmt_str()).to_string(), 1), + ) + .await; } diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 71aa464..074c5c7 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -225,6 +225,68 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) { } } +#[command] +async fn change_meridian(ctx: &Context, msg: &Message, args: String) { + let pool; + let lm; + + { + let data = ctx.data.read().await; + + pool = data + .get::() + .cloned() + .expect("Could not get SQLPool from data"); + + lm = data.get::().cloned().unwrap(); + } + + let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + + if &args == "12" { + user_data.meridian_time = true; + + user_data.commit_changes(&pool).await; + + let _ = msg + .channel_id + .send_message(&ctx, |m| { + m.embed(|e| { + e.title(lm.get(&user_data.language, "meridian/title")) + .color(*THEME_COLOR) + .description(lm.get(&user_data.language, "meridian/12")) + }) + }) + .await; + } else if &args == "24" { + user_data.meridian_time = false; + + user_data.commit_changes(&pool).await; + + let _ = msg + .channel_id + .send_message(&ctx, |m| { + m.embed(|e| { + e.title(lm.get(&user_data.language, "meridian/title")) + .color(*THEME_COLOR) + .description(lm.get(&user_data.language, "meridian/24")) + }) + }) + .await; + } else { + let _ = msg + .channel_id + .send_message(&ctx, |m| { + m.embed(|e| { + e.title("Meridian Help") + .color(*THEME_COLOR) + .description(lm.get(&user_data.language, "help/meridian")) + }) + }) + .await; + } +} + #[command] async fn language(ctx: &Context, msg: &Message, args: String) { let pool; diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 7575065..691bbc2 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -59,11 +59,7 @@ fn shorthand_displacement(seconds: u64) -> String { let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds); - if days > 0 { - format!("{} days, {}", days, time_repr) - } else { - time_repr - } + format!("{} days, {}", days, time_repr) } fn longhand_displacement(seconds: u64) -> String { @@ -129,6 +125,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { 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 mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) .await @@ -168,7 +165,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { "{}", &timezone .timestamp(timestamp, 0) - .format("%Y-%m-%d %H:%M:%S") + .format(meridian.fmt_str()) .to_string(), ); @@ -430,6 +427,7 @@ async fn look(ctx: &Context, msg: &Message, args: String) { 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); @@ -524,7 +522,7 @@ LIMIT let time_display = match flags.time_display { TimeDisplayType::Absolute => timezone .timestamp(reminder.time as i64, 0) - .format("%Y-%m-%d %H:%M:%S") + .format(meridian.fmt_str()) .to_string(), TimeDisplayType::Relative => { @@ -641,7 +639,7 @@ WHERE count + 1, reminder.display_content(), reminder.channel, - time.format("%Y-%m-%d %H:%M:%S") + time.format(user_data.meridian().fmt_str()) ) }); diff --git a/src/consts.rs b/src/consts.rs index 11e3ee5..b5bb95d 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,8 +1,9 @@ pub const DAY: u64 = 86_400; pub const HOUR: u64 = 3_600; pub const MINUTE: u64 = 60; -pub const HELP_STRINGS: [&'static str; 22] = [ +pub const HELP_STRINGS: [&'static str; 23] = [ "help/lang", + "help/meridian", "help/timezone", "help/prefix", "help/blacklist", diff --git a/src/main.rs b/src/main.rs index 7621253..039f6cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,6 +202,7 @@ async fn main() -> Result<(), Box> { .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) .add_command("restrict", &moderation_cmds::RESTRICT_COMMAND) .add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND) + .add_command("meridian", &moderation_cmds::CHANGE_MERIDIAN_COMMAND) .add_command("prefix", &moderation_cmds::PREFIX_COMMAND) .add_command("lang", &moderation_cmds::LANGUAGE_COMMAND) .add_command("pause", &reminder_cmds::PAUSE_COMMAND) diff --git a/src/models.rs b/src/models.rs index 292f4b7..675ab56 100644 --- a/src/models.rs +++ b/src/models.rs @@ -180,6 +180,19 @@ pub struct UserData { pub dm_channel: u32, pub language: String, pub timezone: String, + pub meridian_time: bool, +} + +pub struct MeridianType(bool); + +impl MeridianType { + pub fn fmt_str(&self) -> &str { + if self.0 { + "%Y-%m-%d %I:%M:%S %p" + } else { + "%Y-%m-%d %H:%M:%S" + } + } } impl UserData { @@ -227,6 +240,27 @@ SELECT timezone FROM users WHERE user = ? .unwrap() } + pub async fn meridian_of(user: U, pool: &MySqlPool) -> MeridianType + where + U: Into, + { + let user_id = user.into().as_u64().to_owned(); + + match sqlx::query!( + " +SELECT meridian_time FROM users WHERE user = ? + ", + user_id + ) + .fetch_one(pool) + .await + { + Ok(r) => MeridianType(r.meridian_time != 0), + + Err(_) => MeridianType(false), + } + } + pub async fn from_user( user: &User, ctx: impl CacheHttp, @@ -237,7 +271,7 @@ SELECT timezone FROM users WHERE user = ? match sqlx::query_as_unchecked!( Self, " -SELECT id, user, name, dm_channel, IF(language IS NULL, ?, language) AS language, IF(timezone IS NULL, ?, timezone) AS timezone FROM users WHERE user = ? +SELECT id, user, name, dm_channel, IF(language IS NULL, ?, language) AS language, IF(timezone IS NULL, ?, timezone) AS timezone, meridian_time FROM users WHERE user = ? ", *LOCAL_LANGUAGE, *LOCAL_TIMEZONE, user_id ) @@ -271,7 +305,7 @@ INSERT INTO users (user, name, dm_channel, language, timezone) VALUES (?, ?, (SE Ok(sqlx::query_as_unchecked!( Self, " -SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ? +SELECT id, user, name, dm_channel, language, timezone, meridian_time FROM users WHERE user = ? ", user_id ) @@ -290,11 +324,12 @@ SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ? pub async fn commit_changes(&self, pool: &MySqlPool) { sqlx::query!( " -UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ? +UPDATE users SET name = ?, language = ?, timezone = ?, meridian_time = ? WHERE id = ? ", self.name, self.language, self.timezone, + self.meridian_time, self.id ) .execute(pool) @@ -305,6 +340,10 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ? pub fn timezone(&self) -> Tz { self.timezone.parse().unwrap() } + + pub fn meridian(&self) -> MeridianType { + MeridianType(self.meridian_time) + } } pub struct Timer {