From 74874c6e991765ca809fbe3a4c633ba514bf3428 Mon Sep 17 00:00:00 2001 From: jellywx Date: Tue, 19 Jan 2021 12:19:20 +0000 Subject: [PATCH] timezones are now queried at launch rather than every time the timezone command is used --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/moderation_cmds.rs | 19 ++++++++++--------- src/main.rs | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 467d253..45e9a32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1314,7 +1314,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "1.4.1" +version = "1.4.2" dependencies = [ "Inflector", "chrono", diff --git a/Cargo.toml b/Cargo.toml index db3cc76..ac4b108 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "1.4.1" +version = "1.4.2" authors = ["jellywx "] edition = "2018" diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 50a8eed..3b9a85a 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -25,7 +25,7 @@ use crate::{ framework::SendIterator, get_ctx_data, models::{ChannelData, GuildData, UserData}, - FrameworkCtx, + FrameworkCtx, PopularTimezones, }; use std::{collections::HashMap, iter, time::Duration}; @@ -177,20 +177,21 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) { &GuildData::prefix_from_id(msg.guild_id, &pool).await, ); - let popular_timezones = sqlx::query!( - "SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 20" - ) - .fetch_all(&pool) - .await - .unwrap(); + let popular_timezones = ctx + .data + .read() + .await + .get::() + .cloned() + .unwrap(); let popular_timezones_iter = popular_timezones.iter().map(|t| { ( - t.timezone.clone(), + t.to_string(), format!( "🕗 `{}`", Utc::now() - .with_timezone(&t.timezone.parse::().unwrap()) + .with_timezone(t) .format(user_data.meridian().fmt_str_short()) .to_string() ), diff --git a/src/main.rs b/src/main.rs index e8d923a..2b83ff9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,8 @@ use serenity::futures::TryFutureExt; use inflector::Inflector; use log::info; +use chrono_tz::Tz; + struct SQLPool; impl TypeMapKey for SQLPool { @@ -60,6 +62,12 @@ impl TypeMapKey for FrameworkCtx { type Value = Arc>; } +struct PopularTimezones; + +impl TypeMapKey for PopularTimezones { + type Value = Arc>; +} + struct Handler; #[async_trait] @@ -249,9 +257,20 @@ async fn main() -> Result<(), Box> { ))) .unwrap(); + let popular_timezones = sqlx::query!( + "SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 21" + ) + .fetch_all(&pool) + .await + .unwrap() + .iter() + .map(|t| t.timezone.parse::().unwrap()) + .collect::>(); + let mut data = client.data.write().await; data.insert::(pool); + data.insert::(Arc::new(popular_timezones)); data.insert::(Arc::new(reqwest::Client::new())); data.insert::(framework_arc); data.insert::(Arc::new(language_manager))