wip bump versions

This commit is contained in:
jude
2023-12-22 19:11:39 +00:00
parent e7803b98e8
commit cce0de7c75
9 changed files with 850 additions and 535 deletions

View File

@ -1,7 +1,7 @@
use std::time::{SystemTime, UNIX_EPOCH};
use chrono_tz::TZ_VARIANTS;
use poise::AutocompleteChoice;
use poise::serenity_prelude::AutocompleteChoice;
use crate::{models::CtxData, time_parser::natural_parser, Context};
@ -37,15 +37,9 @@ WHERE
.collect()
}
pub async fn time_hint_autocomplete(
ctx: Context<'_>,
partial: &str,
) -> Vec<AutocompleteChoice<String>> {
pub async fn time_hint_autocomplete(ctx: Context<'_>, partial: &str) -> Vec<AutocompleteChoice> {
if partial.is_empty() {
vec![AutocompleteChoice {
name: "Start typing a time...".to_string(),
value: "now".to_string(),
}]
vec![AutocompleteChoice::new("Start typing a time...".to_string(), "now".to_string())]
} else {
match natural_parser(partial, &ctx.timezone().await.to_string()).await {
Some(timestamp) => match SystemTime::now().duration_since(UNIX_EPOCH) {
@ -53,64 +47,49 @@ pub async fn time_hint_autocomplete(
let diff = timestamp - now.as_secs() as i64;
if diff < 0 {
vec![AutocompleteChoice {
name: "Time is in the past".to_string(),
value: "1 year ago".to_string(),
}]
vec![AutocompleteChoice::new(
"Time is in the past".to_string(),
"1 year ago".to_string(),
)]
} else {
if diff > 86400 {
vec![
AutocompleteChoice {
name: partial.to_string(),
value: partial.to_string(),
},
AutocompleteChoice {
name: format!(
AutocompleteChoice::new(partial.to_string(), partial.to_string()),
AutocompleteChoice::new(
format!(
"In approximately {} days, {} hours",
diff / 86400,
(diff % 86400) / 3600
),
value: partial.to_string(),
},
partial.to_string(),
),
]
} else if diff > 3600 {
vec![
AutocompleteChoice {
name: partial.to_string(),
value: partial.to_string(),
},
AutocompleteChoice {
name: format!("In approximately {} hours", diff / 3600),
value: partial.to_string(),
},
AutocompleteChoice::new(partial.to_string(), partial.to_string()),
AutocompleteChoice::new(
format!("In approximately {} hours", diff / 3600),
partial.to_string(),
),
]
} else {
vec![
AutocompleteChoice {
name: partial.to_string(),
value: partial.to_string(),
},
AutocompleteChoice {
name: format!("In approximately {} minutes", diff / 60),
value: partial.to_string(),
},
AutocompleteChoice::new(partial.to_string(), partial.to_string()),
AutocompleteChoice::new(
format!("In approximately {} minutes", diff / 60),
partial.to_string(),
),
]
}
}
}
Err(_) => {
vec![AutocompleteChoice {
name: partial.to_string(),
value: partial.to_string(),
}]
vec![AutocompleteChoice::new(partial.to_string(), partial.to_string())]
}
},
None => {
vec![AutocompleteChoice {
name: "Time not recognised".to_string(),
value: "now".to_string(),
}]
vec![AutocompleteChoice::new("Time not recognised".to_string(), "now".to_string())]
}
}
}

View File

@ -1,5 +1,5 @@
use lazy_regex::regex;
use poise::serenity_prelude::command::CommandOptionType;
use poise::serenity_prelude::CommandOptionType;
use regex::Captures;
use serde_json::{json, Value};

View File

@ -5,9 +5,7 @@ use chrono_tz::Tz;
use log::warn;
use num_integer::Integer;
use poise::{
serenity_prelude::{
builder::CreateEmbed, component::ButtonStyle, model::channel::Channel, ReactionType,
},
serenity_prelude::{builder::CreateEmbed, model::channel::Channel, ButtonStyle, ReactionType},
CreateReply, Modal,
};

View File

@ -1,8 +1,6 @@
// todo split pager out into a single struct
use chrono_tz::Tz;
use poise::serenity_prelude::{
builder::CreateComponents, model::application::component::ButtonStyle,
};
use poise::serenity_prelude::{ButtonStyle, CreateActionRow, CreateButton};
use serde::{Deserialize, Serialize};
use serde_repr::*;
@ -11,7 +9,7 @@ use crate::{component_models::ComponentDataModel, models::reminder::look_flags::
pub trait Pager {
fn next_page(&self, max_pages: usize) -> usize;
fn create_button_row(&self, max_pages: usize, comp: &mut CreateComponents);
fn create_button_row(&self, max_pages: usize) -> CreateActionRow;
}
#[derive(Serialize_repr, Deserialize_repr)]
@ -43,41 +41,33 @@ impl Pager for LookPager {
}
}
fn create_button_row(&self, max_pages: usize, comp: &mut CreateComponents) {
fn create_button_row(&self, max_pages: usize) -> CreateActionRow {
let next_page = self.next_page(max_pages);
let (page_first, page_prev, page_refresh, page_next, page_last) =
LookPager::buttons(self.flags, next_page, self.timezone);
comp.create_action_row(|row| {
row.create_button(|b| {
b.label("⏮️")
.style(ButtonStyle::Primary)
.custom_id(page_first.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("◀️")
.style(ButtonStyle::Secondary)
.custom_id(page_prev.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("🔁").style(ButtonStyle::Secondary).custom_id(page_refresh.to_custom_id())
})
.create_button(|b| {
b.label("")
.style(ButtonStyle::Secondary)
.custom_id(page_next.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
.create_button(|b| {
b.label("⏭️")
.style(ButtonStyle::Primary)
.custom_id(page_last.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
});
CreateActionRow::Buttons(vec![
CreateButton::new(page_first.to_custom_id())
.label("⏮️")
.style(ButtonStyle::Primary)
.disabled(next_page == 0),
CreateButton::new(page_prev.to_custom_id())
.label("◀️")
.style(ButtonStyle::Secondary)
.disabled(next_page == 0),
CreateButton::new(page_refresh.to_custom_id())
.label("🔁")
.style(ButtonStyle::Secondary),
CreateButton::new(page_next.to_custom_id())
.label("▶️")
.style(ButtonStyle::Secondary)
.disabled(next_page + 1 == max_pages),
CreateButton::new(page_last.to_custom_id())
.label("")
.style(ButtonStyle::Primary)
.disabled(next_page + 1 == max_pages),
])
}
}
@ -150,41 +140,33 @@ impl Pager for DelPager {
}
}
fn create_button_row(&self, max_pages: usize, comp: &mut CreateComponents) {
fn create_button_row(&self, max_pages: usize) -> CreateActionRow {
let next_page = self.next_page(max_pages);
let (page_first, page_prev, page_refresh, page_next, page_last) =
DelPager::buttons(next_page, self.timezone);
comp.create_action_row(|row| {
row.create_button(|b| {
b.label("⏮️")
.style(ButtonStyle::Primary)
.custom_id(page_first.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("◀️")
.style(ButtonStyle::Secondary)
.custom_id(page_prev.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("🔁").style(ButtonStyle::Secondary).custom_id(page_refresh.to_custom_id())
})
.create_button(|b| {
b.label("")
.style(ButtonStyle::Secondary)
.custom_id(page_next.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
.create_button(|b| {
b.label("⏭️")
.style(ButtonStyle::Primary)
.custom_id(page_last.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
});
CreateActionRow::Buttons(vec![
CreateButton::new(page_first.to_custom_id())
.label("⏮️")
.style(ButtonStyle::Primary)
.disabled(next_page == 0),
CreateButton::new(page_prev.to_custom_id())
.label("◀️")
.style(ButtonStyle::Secondary)
.disabled(next_page == 0),
CreateButton::new(page_refresh.to_custom_id())
.label("🔁")
.style(ButtonStyle::Secondary),
CreateButton::new(page_next.to_custom_id())
.label("▶️")
.style(ButtonStyle::Secondary)
.disabled(next_page + 1 == max_pages),
CreateButton::new(page_last.to_custom_id())
.label("")
.style(ButtonStyle::Primary)
.disabled(next_page + 1 == max_pages),
])
}
}
@ -233,41 +215,33 @@ impl Pager for TodoPager {
}
}
fn create_button_row(&self, max_pages: usize, comp: &mut CreateComponents) {
fn create_button_row(&self, max_pages: usize) -> CreateActionRow {
let next_page = self.next_page(max_pages);
let (page_first, page_prev, page_refresh, page_next, page_last) =
TodoPager::buttons(next_page, self.user_id, self.channel_id, self.guild_id);
comp.create_action_row(|row| {
row.create_button(|b| {
b.label("⏮️")
.style(ButtonStyle::Primary)
.custom_id(page_first.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("◀️")
.style(ButtonStyle::Secondary)
.custom_id(page_prev.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("🔁").style(ButtonStyle::Secondary).custom_id(page_refresh.to_custom_id())
})
.create_button(|b| {
b.label("")
.style(ButtonStyle::Secondary)
.custom_id(page_next.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
.create_button(|b| {
b.label("⏭️")
.style(ButtonStyle::Primary)
.custom_id(page_last.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
});
CreateActionRow::Buttons(vec![
CreateButton::new(page_first.to_custom_id())
.label("⏮️")
.style(ButtonStyle::Primary)
.disabled(next_page == 0),
CreateButton::new(page_prev.to_custom_id())
.label("◀️")
.style(ButtonStyle::Secondary)
.disabled(next_page == 0),
CreateButton::new(page_refresh.to_custom_id())
.label("🔁")
.style(ButtonStyle::Secondary),
CreateButton::new(page_next.to_custom_id())
.label("▶️")
.style(ButtonStyle::Secondary)
.disabled(next_page + 1 == max_pages),
CreateButton::new(page_last.to_custom_id())
.label("")
.style(ButtonStyle::Primary)
.disabled(next_page + 1 == max_pages),
])
}
}
@ -350,41 +324,33 @@ impl Pager for MacroPager {
}
}
fn create_button_row(&self, max_pages: usize, comp: &mut CreateComponents) {
fn create_button_row(&self, max_pages: usize) -> CreateActionRow {
let next_page = self.next_page(max_pages);
let (page_first, page_prev, page_refresh, page_next, page_last) =
MacroPager::buttons(next_page);
comp.create_action_row(|row| {
row.create_button(|b| {
b.label("⏮️")
.style(ButtonStyle::Primary)
.custom_id(page_first.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("◀️")
.style(ButtonStyle::Secondary)
.custom_id(page_prev.to_custom_id())
.disabled(next_page == 0)
})
.create_button(|b| {
b.label("🔁").style(ButtonStyle::Secondary).custom_id(page_refresh.to_custom_id())
})
.create_button(|b| {
b.label("")
.style(ButtonStyle::Secondary)
.custom_id(page_next.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
.create_button(|b| {
b.label("⏭️")
.style(ButtonStyle::Primary)
.custom_id(page_last.to_custom_id())
.disabled(next_page + 1 == max_pages)
})
});
CreateActionRow::Buttons(vec![
CreateButton::new(page_first.to_custom_id())
.label("⏮️")
.style(ButtonStyle::Primary)
.disabled(next_page == 0),
CreateButton::new(page_prev.to_custom_id())
.label("◀️")
.style(ButtonStyle::Secondary)
.disabled(next_page == 0),
CreateButton::new(page_refresh.to_custom_id())
.label("🔁")
.style(ButtonStyle::Secondary),
CreateButton::new(page_next.to_custom_id())
.label("▶️")
.style(ButtonStyle::Secondary)
.disabled(next_page + 1 == max_pages),
CreateButton::new(page_last.to_custom_id())
.label("")
.style(ButtonStyle::Primary)
.disabled(next_page + 1 == max_pages),
])
}
}