meridian command for switching from 12/24 format

This commit is contained in:
jellywx 2020-12-16 16:53:13 +00:00
parent 2bcaf0718f
commit 6311099cc6
8 changed files with 127 additions and 32 deletions

View File

@ -28,7 +28,8 @@
- General changes: - General changes:
+ More command outputs use embeds to look nicer + More command outputs use embeds to look nicer
+ Commands that fail provide help outputs that are the same as the help command + 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 + Translations are now loaded from a JSON file included within the executable
+ Reduced user caching by individually querying some attributes like language and timezone + Reduced user caching by individually querying some attributes like language and timezone
+ Roles can now be silently mentioned with <<Role name>> + Roles can now be silently mentioned with <<Role name>>
+ The number of days will show even if zero to improve clarity

View File

@ -50,9 +50,11 @@ CREATE TABLE reminders.users (
language VARCHAR(2) DEFAULT 'EN' NOT NULL, language VARCHAR(2) DEFAULT 'EN' NOT NULL,
timezone VARCHAR(32) DEFAULT 'UTC' NOT NULL, timezone VARCHAR(32) DEFAULT 'UTC' NOT NULL,
meridian_time BOOLEAN DEFAULT 0 NOT NULL,
allowed_dm BOOLEAN DEFAULT 1 NOT NULL, allowed_dm BOOLEAN DEFAULT 1 NOT NULL,
patreon BOOL NOT NULL DEFAULT 0, patreon BOOLEAN NOT NULL DEFAULT 0,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (dm_channel) REFERENCES reminders.channels(id) ON DELETE RESTRICT FOREIGN KEY (dm_channel) REFERENCES reminders.channels(id) ON DELETE RESTRICT

View File

@ -239,7 +239,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) {
} }
#[command] #[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 data = ctx.data.read().await;
let pool = data 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 language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_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 now = Utc::now().with_timezone(&timezone);
let clock_display = lm.get(&language, "clock/time"); let clock_display = lm.get(&language, "clock/time");
if args == "12" {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(
&ctx, &ctx,
clock_display.replacen("{}", &now.format("%I:%M:%S %p").to_string(), 1), clock_display.replacen("{}", &now.format(meridian.fmt_str()).to_string(), 1),
)
.await;
} else {
let _ = msg
.channel_id
.say(
&ctx,
clock_display.replacen("{}", &now.format("%H:%M:%S").to_string(), 1),
) )
.await; .await;
} }
}

View File

@ -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::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().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] #[command]
async fn language(ctx: &Context, msg: &Message, args: String) { async fn language(ctx: &Context, msg: &Message, args: String) {
let pool; let pool;

View File

@ -59,11 +59,7 @@ fn shorthand_displacement(seconds: u64) -> String {
let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds); let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds);
if days > 0 {
format!("{} days, {}", days, time_repr) format!("{} days, {}", days, time_repr)
} else {
time_repr
}
} }
fn longhand_displacement(seconds: u64) -> String { 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 language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_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) let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool)
.await .await
@ -168,7 +165,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
"{}", "{}",
&timezone &timezone
.timestamp(timestamp, 0) .timestamp(timestamp, 0)
.format("%Y-%m-%d %H:%M:%S") .format(meridian.fmt_str())
.to_string(), .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 language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_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);
@ -524,7 +522,7 @@ LIMIT
let time_display = match flags.time_display { let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone TimeDisplayType::Absolute => timezone
.timestamp(reminder.time as i64, 0) .timestamp(reminder.time as i64, 0)
.format("%Y-%m-%d %H:%M:%S") .format(meridian.fmt_str())
.to_string(), .to_string(),
TimeDisplayType::Relative => { TimeDisplayType::Relative => {
@ -641,7 +639,7 @@ WHERE
count + 1, count + 1,
reminder.display_content(), reminder.display_content(),
reminder.channel, reminder.channel,
time.format("%Y-%m-%d %H:%M:%S") time.format(user_data.meridian().fmt_str())
) )
}); });

View File

@ -1,8 +1,9 @@
pub const DAY: u64 = 86_400; pub const DAY: u64 = 86_400;
pub const HOUR: u64 = 3_600; pub const HOUR: u64 = 3_600;
pub const MINUTE: u64 = 60; pub const MINUTE: u64 = 60;
pub const HELP_STRINGS: [&'static str; 22] = [ pub const HELP_STRINGS: [&'static str; 23] = [
"help/lang", "help/lang",
"help/meridian",
"help/timezone", "help/timezone",
"help/prefix", "help/prefix",
"help/blacklist", "help/blacklist",

View File

@ -202,6 +202,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
.add_command("restrict", &moderation_cmds::RESTRICT_COMMAND) .add_command("restrict", &moderation_cmds::RESTRICT_COMMAND)
.add_command("timezone", &moderation_cmds::TIMEZONE_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("prefix", &moderation_cmds::PREFIX_COMMAND)
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND) .add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
.add_command("pause", &reminder_cmds::PAUSE_COMMAND) .add_command("pause", &reminder_cmds::PAUSE_COMMAND)

View File

@ -180,6 +180,19 @@ pub struct UserData {
pub dm_channel: u32, pub dm_channel: u32,
pub language: String, pub language: String,
pub timezone: 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 { impl UserData {
@ -227,6 +240,27 @@ SELECT timezone FROM users WHERE user = ?
.unwrap() .unwrap()
} }
pub async fn meridian_of<U>(user: U, pool: &MySqlPool) -> MeridianType
where
U: Into<UserId>,
{
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( pub async fn from_user(
user: &User, user: &User,
ctx: impl CacheHttp, ctx: impl CacheHttp,
@ -237,7 +271,7 @@ SELECT timezone FROM users WHERE user = ?
match sqlx::query_as_unchecked!( match sqlx::query_as_unchecked!(
Self, 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 *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!( Ok(sqlx::query_as_unchecked!(
Self, 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 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) { pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!( sqlx::query!(
" "
UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ? UPDATE users SET name = ?, language = ?, timezone = ?, meridian_time = ? WHERE id = ?
", ",
self.name, self.name,
self.language, self.language,
self.timezone, self.timezone,
self.meridian_time,
self.id self.id
) )
.execute(pool) .execute(pool)
@ -305,6 +340,10 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
pub fn timezone(&self) -> Tz { pub fn timezone(&self) -> Tz {
self.timezone.parse().unwrap() self.timezone.parse().unwrap()
} }
pub fn meridian(&self) -> MeridianType {
MeridianType(self.meridian_time)
}
} }
pub struct Timer { pub struct Timer {