Override timezone per command

Timezone option that will override the timezone on a per-command basis
This commit is contained in:
jude 2022-09-11 18:59:46 +01:00
parent ecaa382a1e
commit c8f646a8fa
4 changed files with 57 additions and 35 deletions

View File

@ -0,0 +1,35 @@
use chrono_tz::TZ_VARIANTS;
use crate::Context;
pub async fn timezone_autocomplete(ctx: Context<'_>, partial: &str) -> Vec<String> {
if partial.is_empty() {
ctx.data().popular_timezones.iter().map(|t| t.to_string()).collect::<Vec<String>>()
} else {
TZ_VARIANTS
.iter()
.filter(|tz| tz.to_string().contains(&partial))
.take(25)
.map(|t| t.to_string())
.collect::<Vec<String>>()
}
}
pub async fn macro_name_autocomplete(ctx: Context<'_>, partial: &str) -> Vec<String> {
sqlx::query!(
"
SELECT name
FROM macro
WHERE
guild_id = (SELECT id FROM guilds WHERE guild = ?)
AND name LIKE CONCAT(?, '%')",
ctx.guild_id().unwrap().0,
partial,
)
.fetch_all(&ctx.data().database)
.await
.unwrap_or_default()
.iter()
.map(|s| s.name.clone())
.collect()
}

View File

@ -1,3 +1,4 @@
mod autocomplete;
pub mod info_cmds; pub mod info_cmds;
pub mod moderation_cmds; pub mod moderation_cmds;
pub mod reminder_cmds; pub mod reminder_cmds;

View File

@ -8,6 +8,7 @@ use poise::{serenity_prelude::command::CommandOptionType, CreateReply};
use regex::Captures; use regex::Captures;
use serde_json::{json, Value}; use serde_json::{json, Value};
use super::autocomplete::{macro_name_autocomplete, timezone_autocomplete};
use crate::{ use crate::{
component_models::pager::{MacroPager, Pager}, component_models::pager::{MacroPager, Pager},
consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR},
@ -18,19 +19,6 @@ use crate::{
Context, Data, Error, GuildId, Context, Data, Error, GuildId,
}; };
async fn timezone_autocomplete(ctx: Context<'_>, partial: &str) -> Vec<String> {
if partial.is_empty() {
ctx.data().popular_timezones.iter().map(|t| t.to_string()).collect::<Vec<String>>()
} else {
TZ_VARIANTS
.iter()
.filter(|tz| tz.to_string().contains(&partial))
.take(25)
.map(|t| t.to_string())
.collect::<Vec<String>>()
}
}
/// Select your timezone /// Select your timezone
#[poise::command(slash_command, identifying_name = "timezone")] #[poise::command(slash_command, identifying_name = "timezone")]
pub async fn timezone( pub async fn timezone(
@ -206,25 +194,6 @@ Do not share it!
Ok(()) Ok(())
} }
async fn macro_name_autocomplete(ctx: Context<'_>, partial: &str) -> Vec<String> {
sqlx::query!(
"
SELECT name
FROM macro
WHERE
guild_id = (SELECT id FROM guilds WHERE guild = ?)
AND name LIKE CONCAT(?, '%')",
ctx.guild_id().unwrap().0,
partial,
)
.fetch_all(&ctx.data().database)
.await
.unwrap_or_default()
.iter()
.map(|s| s.name.clone())
.collect()
}
/// Record and replay command sequences /// Record and replay command sequences
#[poise::command( #[poise::command(
slash_command, slash_command,

View File

@ -14,6 +14,7 @@ use poise::{
AutocompleteChoice, CreateReply, Modal, AutocompleteChoice, CreateReply, Modal,
}; };
use super::autocomplete::timezone_autocomplete;
use crate::{ use crate::{
component_models::{ component_models::{
pager::{DelPager, LookPager, Pager}, pager::{DelPager, LookPager, Pager},
@ -592,7 +593,12 @@ pub async fn remind(
expires: Option<String>, expires: Option<String>,
#[description = "Set the TTS flag on the reminder message, similar to the /tts command"] #[description = "Set the TTS flag on the reminder message, similar to the /tts command"]
tts: Option<bool>, tts: Option<bool>,
#[description = "Set a timezone override for this reminder only"]
#[autocomplete = "timezone_autocomplete"]
timezone: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let tz = timezone.map(|t| t.parse::<Tz>().ok()).flatten();
if content.is_empty() { if content.is_empty() {
let data = ContentModal::execute(ctx).await?; let data = ContentModal::execute(ctx).await?;
@ -604,10 +610,20 @@ pub async fn remind(
interval, interval,
expires, expires,
tts, tts,
tz,
) )
.await .await
} else { } else {
create_reminder(Context::Application(ctx), time, content, channels, interval, expires, tts) create_reminder(
Context::Application(ctx),
time,
content,
channels,
interval,
expires,
tts,
tz,
)
.await .await
} }
} }
@ -620,6 +636,7 @@ async fn create_reminder(
interval: Option<String>, interval: Option<String>,
expires: Option<String>, expires: Option<String>,
tts: Option<bool>, tts: Option<bool>,
timezone: Option<Tz>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if interval.is_none() && expires.is_some() { if interval.is_none() && expires.is_some() {
ctx.say("`expires` can only be used with `interval`").await?; ctx.say("`expires` can only be used with `interval`").await?;
@ -630,7 +647,7 @@ async fn create_reminder(
ctx.defer().await?; ctx.defer().await?;
let user_data = ctx.author_data().await.unwrap(); let user_data = ctx.author_data().await.unwrap();
let timezone = ctx.timezone().await; let timezone = timezone.unwrap_or(ctx.timezone().await);
let time = natural_parser(&time, &timezone.to_string()).await; let time = natural_parser(&time, &timezone.to_string()).await;