chore: some cleanup

This commit is contained in:
Jef Roosens 2023-05-13 10:04:36 +02:00
parent d1245ab365
commit 7c06906718
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
9 changed files with 305 additions and 233 deletions

View file

@ -1,8 +1,7 @@
use crate::{Context, Error};
use chrono::{NaiveDate, Duration};
use affluences_api::HourBlock;
use chrono::{Duration, NaiveDate};
use uuid::{uuid, Uuid};
use affluences_api::Hour;
use poise::serenity_prelude as serenity;
const STERRE_BIB_ID: Uuid = uuid!("4737e57a-ee05-4f7b-901a-7bb541eeb297");
const TIME_FORMAT: &'static str = "%H:%M";
@ -85,24 +84,25 @@ pub async fn getvotes(
/// List available timeslots for day
#[poise::command(prefix_command, slash_command)]
pub async fn available(
ctx: Context<'_>,
date: NaiveDate,
) -> Result<(), Error> {
pub async fn available(ctx: Context<'_>, date: NaiveDate) -> Result<(), Error> {
let client = &ctx.data().client;
let resources = client.available(STERRE_BIB_ID, date, 1).await?;
let mut fields: Vec<(String, String, bool)> = Default::default();
for resource in &resources {
if resource.hours.len() == 0 {
fields.push((resource.resource_name.clone(), "Nothing available.".to_string(), false));
fields.push((
resource.resource_name.clone(),
"Nothing available.".to_string(),
false,
));
continue
continue;
}
let mut lines: Vec<String> = Default::default();
let mut start_hour_opt: Option<&Hour> = None;
let mut start_hour_opt: Option<&HourBlock> = None;
let mut duration = Duration::seconds(0);
for hour in &resource.hours {
@ -111,7 +111,13 @@ pub async fn available(
duration = duration + Duration::minutes(hour.granularity.into());
} else {
let end_hour = start_hour.hour + duration;
lines.push(format!("{} - {} ({:02}:{:02})", start_hour.hour.format(TIME_FORMAT), end_hour.format(TIME_FORMAT), duration.num_hours(), duration.num_minutes() % 60));
lines.push(format!(
"{} - {} ({:02}:{:02})",
start_hour.hour.format(TIME_FORMAT),
end_hour.format(TIME_FORMAT),
duration.num_hours(),
duration.num_minutes() % 60
));
start_hour_opt = None;
}
} else if hour.state == 1 {
@ -123,16 +129,32 @@ pub async fn available(
// Print final entry if present
if let Some(start_hour) = start_hour_opt {
let end_hour = start_hour.hour + duration;
lines.push(format!("{} - {} ({:02}:{:02})", start_hour.hour.format(TIME_FORMAT), end_hour.format(TIME_FORMAT), duration.num_hours(), duration.num_minutes() % 60));
lines.push(format!(
"{} - {} ({:02}:{:02})",
start_hour.hour.format(TIME_FORMAT),
end_hour.format(TIME_FORMAT),
duration.num_hours(),
duration.num_minutes() % 60
));
}
fields.push((resource.resource_name.clone(), lines.join("\n"), false));
}
ctx.send(|f|
f.embed(|e|
e.description(format!("Available booking dates for {}.", date))
.fields(fields))).await?;
ctx.send(|f| {
f.embed(|e| {
e.description(format!("Available booking dates for {}.", date))
.fields(fields)
})
})
.await?;
Ok(())
}
// Create a reservation
// #[poise::command(prefix_command, slash_command)]
// pub async fn reserve(
// ctx: Context<'_>,
// date: NaiveDate,
// ) -> Result<(), Error> {

View file

@ -36,7 +36,12 @@ async fn main() {
// FrameworkOptions contains all of poise's configuration option in one struct
// Every option can be omitted to use its default value
let options = poise::FrameworkOptions {
commands: vec![commands::help(), commands::vote(), commands::getvotes(), commands::available()],
commands: vec![
commands::help(),
commands::vote(),
commands::getvotes(),
commands::available(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))),