refactor: move commands to separate module
This commit is contained in:
parent
2999ca2301
commit
38994a29a0
6 changed files with 47 additions and 24 deletions
66
src/commands/affluence.rs
Normal file
66
src/commands/affluence.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use crate::{Context, Error};
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use uuid::{uuid, Uuid};
|
||||
|
||||
const STERRE_BIB_ID: Uuid = uuid!("4737e57a-ee05-4f7b-901a-7bb541eeb297");
|
||||
const TIME_FORMAT: &str = "%H:%M";
|
||||
|
||||
/// List available timeslots for day
|
||||
#[poise::command(prefix_command, slash_command)]
|
||||
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 {
|
||||
let available_hours = resource.condensed_available_hours();
|
||||
|
||||
if available_hours.is_empty() {
|
||||
fields.push((
|
||||
resource.resource_name.clone(),
|
||||
"Nothing available.".to_string(),
|
||||
false,
|
||||
));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
fields.push((
|
||||
resource.resource_name.clone(),
|
||||
available_hours
|
||||
.into_iter()
|
||||
.map(|(start_block, duration)| {
|
||||
format!(
|
||||
"{} - {} ({:02}:{:02})",
|
||||
start_block.hour.format(TIME_FORMAT),
|
||||
(start_block.hour + duration).format(TIME_FORMAT),
|
||||
duration.num_hours(),
|
||||
duration.num_minutes() % 60
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n"),
|
||||
false,
|
||||
));
|
||||
}
|
||||
|
||||
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> {
|
||||
|
||||
// }
|
||||
27
src/commands/mod.rs
Normal file
27
src/commands/mod.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
mod affluence;
|
||||
|
||||
use crate::{Context, Data, Error};
|
||||
|
||||
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
|
||||
vec![help(), affluence::available()]
|
||||
}
|
||||
|
||||
/// Show this help menu
|
||||
#[poise::command(prefix_command, track_edits, slash_command)]
|
||||
pub async fn help(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Specific command to show help about"]
|
||||
#[autocomplete = "poise::builtins::autocomplete_command"]
|
||||
command: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
poise::builtins::help(
|
||||
ctx,
|
||||
command.as_deref(),
|
||||
poise::builtins::HelpConfiguration {
|
||||
extra_text_at_bottom: "This is an example bot made to showcase features of my custom Discord bot framework",
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue