From 012d90e832ea2cbe190e2f2e1699c5f2d2bb9909 Mon Sep 17 00:00:00 2001 From: jellywx Date: Sun, 22 Nov 2020 12:09:57 +0000 Subject: [PATCH] timezone_of method for more lazy-storage. fix for del command raising issues if 0 is written. fix for get_language not recognising --- src/commands/info_cmds.rs | 8 +++-- src/commands/moderation_cmds.rs | 56 +++++++++++++++------------------ src/commands/reminder_cmds.rs | 42 ++++++++++++++----------- src/language_manager.rs | 6 +++- src/models.rs | 42 +++++++++++++++++++++---- 5 files changed, 94 insertions(+), 60 deletions(-) diff --git a/src/commands/info_cmds.rs b/src/commands/info_cmds.rs index 143985d..8511d75 100644 --- a/src/commands/info_cmds.rs +++ b/src/commands/info_cmds.rs @@ -169,11 +169,13 @@ async fn clock(ctx: &Context, msg: &Message, args: String) { .expect("Could not get SQLPool from data"); let lm = data.get::().unwrap(); - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); - let now = Utc::now().with_timezone(&user_data.timezone()); + let language = UserData::language_of(&msg.author, &pool).await; + let timezone = UserData::timezone_of(&msg.author, &pool).await; - let clock_display = lm.get(&user_data.language, "clock/time"); + let now = Utc::now().with_timezone(&timezone); + + let clock_display = lm.get(&language, "clock/time"); if args == "12" { let _ = msg diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index fea6618..4c3cd48 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -42,7 +42,7 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; let capture_opt = REGEX_CHANNEL .captures(&args) @@ -71,24 +71,24 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) { if local { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "blacklist/added")) + .say(&ctx, lm.get(&language, "blacklist/added")) .await; } else { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "blacklist/added_from")) + .say(&ctx, lm.get(&language, "blacklist/added_from")) .await; } } else { if local { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "blacklist/removed")) + .say(&ctx, lm.get(&language, "blacklist/removed")) .await; } else { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "blacklist/removed_from")) + .say(&ctx, lm.get(&language, "blacklist/removed_from")) .await; } } @@ -215,27 +215,25 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) { let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) .await .unwrap(); - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; if args.len() > 5 { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "prefix/too_long")) + .say(&ctx, lm.get(&language, "prefix/too_long")) .await; } else if args.is_empty() { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "prefix/no_argument")) + .say(&ctx, lm.get(&language, "prefix/no_argument")) .await; } else { guild_data.prefix = args; guild_data.commit_changes(&pool).await; - let content = lm.get(&user_data.language, "prefix/success").replacen( - "{prefix}", - &guild_data.prefix, - 1, - ); + let content = + lm.get(&language, "prefix/success") + .replacen("{prefix}", &guild_data.prefix, 1); let _ = msg.channel_id.say(&ctx, content).await; } @@ -259,7 +257,7 @@ async fn restrict(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) .await .unwrap(); @@ -292,7 +290,7 @@ DELETE FROM command_restrictions WHERE role_id = (SELECT id FROM roles WHERE rol if commands.is_empty() { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "restrict/disabled")) + .say(&ctx, lm.get(&language, "restrict/disabled")) .await; } else { let _ = sqlx::query!( @@ -317,7 +315,7 @@ INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM role if res.is_err() { println!("{:?}", res); - let content = lm.get(&user_data.language, "restrict/failure").replacen( + let content = lm.get(&language, "restrict/failure").replacen( "{command}", &command, 1, @@ -329,7 +327,7 @@ INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM role let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "restrict/enabled")) + .say(&ctx, lm.get(&language, "restrict/enabled")) .await; } } @@ -360,15 +358,15 @@ WHERE .map(|row| format!("<@&{}> can use {}", row.role, row.command)) .collect::>() .join("\n"); - let display = - lm.get(&user_data.language, "restrict/allowed") - .replacen("{}", &display_inner, 1); + let display = lm + .get(&language, "restrict/allowed") + .replacen("{}", &display_inner, 1); let _ = msg.channel_id.say(&ctx, display).await; } else { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "restrict/help")) + .say(&ctx, lm.get(&language, "restrict/help")) .await; } } @@ -391,7 +389,7 @@ async fn alias(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; let guild_id = msg.guild_id.unwrap().as_u64().to_owned(); @@ -444,14 +442,14 @@ DELETE FROM command_aliases WHERE name = ? AND guild_id = (SELECT id FROM guilds .unwrap(); let content = lm - .get(&user_data.language, "alias/removed") + .get(&language, "alias/removed") .replace("{count}", &deleted_count.count.to_string()); let _ = msg.channel_id.say(&ctx, content).await; } else { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "alias/help")) + .say(&ctx, lm.get(&language, "alias/help")) .await; } } @@ -475,9 +473,7 @@ UPDATE command_aliases SET command = ? WHERE guild_id = (SELECT id FROM guilds W .unwrap(); } - let content = lm - .get(&user_data.language, "alias/created") - .replace("{name}", name); + let content = lm.get(&language, "alias/created").replace("{name}", name); let _ = msg.channel_id.say(&ctx, content).await; } else { @@ -499,7 +495,7 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER }, Err(_) => { - let content = lm.get(&user_data.language, "alias/not_found").replace("{name}", name); + let content = lm.get(&language, "alias/not_found").replace("{name}", name); let _ = msg.channel_id.say(&ctx, content).await; }, @@ -509,9 +505,7 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER } } else { let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; - let content = lm - .get(&user_data.language, "alias/help") - .replace("{prefix}", &prefix); + let content = lm.get(&language, "alias/help").replace("{prefix}", &prefix); let _ = msg.channel_id.say(&ctx, content).await; } diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 23d05de..e7a61c3 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -128,7 +128,9 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; + let timezone = UserData::timezone_of(&msg.author, &pool).await; + let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) .await .unwrap(); @@ -142,16 +144,16 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { if channel.paused { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "pause/paused_indefinite")) + .say(&ctx, lm.get(&language, "pause/paused_indefinite")) .await; } else { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "pause/unpaused")) + .say(&ctx, lm.get(&language, "pause/unpaused")) .await; } } else { - let parser = TimeParser::new(args, user_data.timezone()); + let parser = TimeParser::new(args, timezone); let pause_until = parser.timestamp(); match pause_until { @@ -163,10 +165,9 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { channel.commit_changes(&pool).await; - let content = lm.get(&user_data.language, "pause/paused_until").replace( + let content = lm.get(&language, "pause/paused_until").replace( "{}", - &user_data - .timezone() + &timezone .timestamp(timestamp, 0) .format("%Y-%m-%d %H:%M:%S") .to_string(), @@ -178,7 +179,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) { Err(_) => { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "pause/invalid_time")) + .say(&ctx, lm.get(&language, "pause/invalid_time")) .await; } } @@ -277,19 +278,21 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; + let timezone = UserData::timezone_of(&msg.author, &pool).await; + let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) .await .unwrap(); if args.is_empty() { let content = lm - .get(&user_data.language, "nudge/no_argument") + .get(&language, "nudge/no_argument") .replace("{nudge}", &format!("{}s", &channel.nudge.to_string())); let _ = msg.channel_id.say(&ctx, content).await; } else { - let parser = TimeParser::new(args, user_data.timezone.parse().unwrap()); + let parser = TimeParser::new(args, timezone); let nudge_time = parser.displacement(); match nudge_time { @@ -297,14 +300,14 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) { if displacement < i16::MIN as i64 || displacement > i16::MAX as i64 { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "nudge/invalid_time")) + .say(&ctx, lm.get(&language, "nudge/invalid_time")) .await; } else { channel.nudge = displacement as i16; channel.commit_changes(&pool).await; - let response = lm.get(&user_data.language, "nudge/success").replacen( + let response = lm.get(&language, "nudge/success").replacen( "{}", &displacement.to_string(), 1, @@ -317,7 +320,7 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) { Err(_) => { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "nudge/invalid_time")) + .say(&ctx, lm.get(&language, "nudge/invalid_time")) .await; } } @@ -425,7 +428,8 @@ async fn look(ctx: &Context, msg: &Message, args: String) { lm = data.get::().cloned().unwrap(); } - let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let language = UserData::language_of(&msg.author, &pool).await; + let timezone = UserData::timezone_of(&msg.author, &pool).await; let flags = LookFlags::from_string(&args); @@ -549,15 +553,14 @@ LIMIT if reminders.is_empty() { let _ = msg .channel_id - .say(&ctx, lm.get(&user_data.language, "look/no_reminders")) + .say(&ctx, lm.get(&language, "look/no_reminders")) .await; } else { - let inter = lm.get(&user_data.language, "look/inter"); + let inter = lm.get(&language, "look/inter"); let display = reminders.iter().map(|reminder| { let time_display = match flags.time_display { - TimeDisplayType::Absolute => user_data - .timezone() + TimeDisplayType::Absolute => timezone .timestamp(reminder.time as i64, 0) .format("%Y-%m-%d %H:%M:%S") .to_string(), @@ -703,6 +706,7 @@ WHERE .filter_map(|i| { i.parse::() .ok() + .filter(|val| val > &0) .map(|val| reminder_ids.get(val - 1)) .flatten() }) diff --git a/src/language_manager.rs b/src/language_manager.rs index ce493cc..7e182fd 100644 --- a/src/language_manager.rs +++ b/src/language_manager.rs @@ -36,9 +36,13 @@ impl LanguageManager { } pub fn get_language(&self, language: &str) -> Option<&str> { + let language_normal = language.to_lowercase(); + self.languages .iter() - .filter(|(k, v)| k.to_lowercase() == language || v.to_lowercase() == language) + .filter(|(k, v)| { + k.to_lowercase() == language_normal || v.to_lowercase() == language_normal + }) .map(|(k, _)| k.as_str()) .next() } diff --git a/src/models.rs b/src/models.rs index df92c79..292f4b7 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,6 +1,11 @@ use serenity::{ http::CacheHttp, - model::{channel::Channel, guild::Guild, id::GuildId, user::User}, + model::{ + channel::Channel, + guild::Guild, + id::{GuildId, UserId}, + user::User, + }, }; use sqlx::MySqlPool; @@ -178,25 +183,50 @@ pub struct UserData { } impl UserData { - pub async fn language_of(user: &User, pool: &MySqlPool) -> String { - let user_id = user.id.as_u64().to_owned(); + pub async fn language_of(user: U, pool: &MySqlPool) -> String + where + U: Into, + { + let user_id = user.into().as_u64().to_owned(); match sqlx::query!( " -SELECT IF(language IS NULL, ?, language) AS language FROM users WHERE user = ? +SELECT language FROM users WHERE user = ? ", - *LOCAL_LANGUAGE, user_id ) .fetch_one(pool) .await { - Ok(r) => r.language.unwrap(), + Ok(r) => r.language, Err(_) => LOCAL_LANGUAGE.clone(), } } + pub async fn timezone_of(user: U, pool: &MySqlPool) -> Tz + where + U: Into, + { + let user_id = user.into().as_u64().to_owned(); + + match sqlx::query!( + " +SELECT timezone FROM users WHERE user = ? + ", + user_id + ) + .fetch_one(pool) + .await + { + Ok(r) => r.timezone, + + Err(_) => LOCAL_TIMEZONE.clone(), + } + .parse() + .unwrap() + } + pub async fn from_user( user: &User, ctx: impl CacheHttp,