timezones are now queried at launch rather than every time the timezone command is used

This commit is contained in:
jellywx 2021-01-19 12:19:20 +00:00
parent 9003beb1bb
commit 74874c6e99
4 changed files with 31 additions and 11 deletions

2
Cargo.lock generated
View File

@ -1314,7 +1314,7 @@ dependencies = [
[[package]]
name = "reminder_rs"
version = "1.4.1"
version = "1.4.2"
dependencies = [
"Inflector",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "reminder_rs"
version = "1.4.1"
version = "1.4.2"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"

View File

@ -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::<PopularTimezones>()
.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::<Tz>().unwrap())
.with_timezone(t)
.format(user_data.meridian().fmt_str_short())
.to_string()
),

View File

@ -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<Box<dyn Framework + Send + Sync>>;
}
struct PopularTimezones;
impl TypeMapKey for PopularTimezones {
type Value = Arc<Vec<Tz>>;
}
struct Handler;
#[async_trait]
@ -249,9 +257,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
)))
.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::<Tz>().unwrap())
.collect::<Vec<Tz>>();
let mut data = client.data.write().await;
data.insert::<SQLPool>(pool);
data.insert::<PopularTimezones>(Arc::new(popular_timezones));
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
data.insert::<FrameworkCtx>(framework_arc);
data.insert::<LanguageManager>(Arc::new(language_manager))