Skip to content

Commit

Permalink
Merge pull request #20 from perpetualcacophony/januannie
Browse files Browse the repository at this point in the history
merge januannie command
  • Loading branch information
perpetualcacophony committed Jul 14, 2024
2 parents 6605481 + 340dc7f commit 37b12f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ list! {
version,
pub wordle,
help,
eightball
eightball,
januannie
}

trait LogCommands {
Expand Down
53 changes: 53 additions & 0 deletions src/commands/januannie.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::utils::{poise::ContextExt, Context};
use chrono::NaiveDate;

#[poise::command(slash_command, prefix_command)]
pub async fn januannie(ctx: Context<'_>) -> crate::Result<()> {
let result: crate::utils::poise::CommandResult = try {
let text = text(chrono::Utc::now().date_naive());
ctx.reply_ext(text).await?;
};
result?;
Ok(())
}

const JAN_1_2025: NaiveDate =
NaiveDate::from_ymd_opt(2025, 01, 01).expect("2025-01-01 should be a valid date");

fn text(current: NaiveDate) -> String {
let days_until = JAN_1_2025.signed_duration_since(current).num_days();
let days_text = if days_until == 1 {
format!("{days_until} day")
} else {
format!("{days_until} days")
};

format!("{days_text} until Januannie!")
}

#[cfg(test)]
mod tests {
#[test]
fn one_day_until() {
assert_eq!(
super::text(
super::JAN_1_2025
.checked_sub_days(chrono::Days::new(1))
.unwrap()
),
"1 day until Januannie!"
)
}

#[test]
fn one_week_until() {
assert_eq!(
super::text(
super::JAN_1_2025
.checked_sub_days(chrono::Days::new(7))
.unwrap(),
),
"7 days until Januannie!"
)
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(associated_type_defaults)]
#![feature(try_blocks)]
#![feature(min_specialization)]
#![feature(const_option)]

/// Functionality called from Discord.
mod discord;
Expand Down

0 comments on commit 37b12f6

Please sign in to comment.