84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
mod bib;
|
|
mod minecraft;
|
|
mod users;
|
|
|
|
use chrono::Datelike;
|
|
use core::str;
|
|
|
|
use crate::{Context, Data, Error};
|
|
|
|
type EmbedField = (String, String, bool);
|
|
|
|
const DAY_TERMS: [&str; 3] = ["today", "tomorrow", "overmorrow"];
|
|
|
|
#[derive(Clone)]
|
|
pub struct HumanNaiveDate(chrono::NaiveDate);
|
|
|
|
impl str::FromStr for HumanNaiveDate {
|
|
type Err = chrono::format::ParseError;
|
|
|
|
fn from_str(s: &str) -> chrono::format::ParseResult<Self> {
|
|
if let Some(days_to_add) = DAY_TERMS.iter().position(|term| s.to_lowercase() == *term) {
|
|
let now = chrono::Local::now().naive_local().date();
|
|
|
|
// days_to_add will never be greater than 2
|
|
Ok(HumanNaiveDate(
|
|
now + chrono::Duration::days(days_to_add.try_into().unwrap()),
|
|
))
|
|
} else if let Ok(weekday) = s.parse::<chrono::Weekday>() {
|
|
let now = chrono::Local::now().naive_local().date();
|
|
let cur_day = now.weekday();
|
|
let cur_day_index = cur_day.num_days_from_monday();
|
|
let parsed_day_index = weekday.num_days_from_monday();
|
|
|
|
let days_to_add = if cur_day_index <= parsed_day_index {
|
|
parsed_day_index - cur_day_index
|
|
} else {
|
|
7 - (cur_day_index - parsed_day_index)
|
|
};
|
|
|
|
Ok(HumanNaiveDate(
|
|
now + chrono::Duration::days(days_to_add.into()),
|
|
))
|
|
} else {
|
|
chrono::NaiveDate::from_str(s).map(HumanNaiveDate)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<HumanNaiveDate> for chrono::NaiveDate {
|
|
fn from(val: HumanNaiveDate) -> chrono::NaiveDate {
|
|
val.0
|
|
}
|
|
}
|
|
|
|
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
|
|
vec![
|
|
help(),
|
|
bib::bib(),
|
|
minecraft::mc(),
|
|
users::register(),
|
|
users::registered(),
|
|
]
|
|
}
|
|
|
|
/// 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: "Brought to you by Doofenshmirtz Evil Incorporated.",
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await?;
|
|
Ok(())
|
|
}
|