FrenBot/src/discord/birthday.rs
2023-04-21 15:34:14 -06:00

97 lines
2.9 KiB
Rust

use crate::models::birthday::BirthdayEntry;
use crate::{command, group, GlobalData};
use rand::{thread_rng, Rng};
use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
use serenity::model::id::UserId;
use serenity::utils::MessageBuilder;
#[group]
#[commands(add_birthday, list_birthdays)]
pub struct Birthday;
#[command]
#[description("Add your birthday to the bot. Please use American dates or i will cut u.")]
#[example("add_birthday 04/21/1997")]
pub async fn add_birthday(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if args.is_empty() {
msg.reply(
&ctx.http,
"Look just give me a day, doesn't matter. I'm not the IRS",
)
.await?;
return Ok(());
}
let date_split: Vec<&str> = args.rest().split('/').collect();
if date_split.len() < 3 {
msg.reply(&ctx.http, "Try again with a real date").await?;
return Ok(());
}
let month: u32 = date_split[0].parse().unwrap_or(0);
let day: u32 = date_split[1].parse().unwrap_or(0);
let year: i32 = date_split[2].parse().unwrap_or(0);
if month == 0 || day == 0 || year == 0 {
msg.reply(&ctx.http, "Try again with a real date").await?;
return Ok(());
}
if let Some(date) = chrono::NaiveDate::from_ymd_opt(year, month, day) {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
BirthdayEntry::add_birthday(&global_data.db, msg.author.id.0, date)?;
msg.reply(
&ctx.http,
format!(
"Thank you subject #{}, I am now {}% closer to making a full AI replica of you.",
msg.author.id.0,
thread_rng().gen_range(0.0..100.0)
),
)
.await?;
} else {
msg.reply(&ctx.http, "Try again with a real date").await?;
return Ok(());
}
Ok(())
}
#[command]
#[description("Add your birthday to the bot. Please use American dates or i will cut u.")]
pub async fn list_birthdays(ctx: &Context, msg: &Message) -> CommandResult {
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
let birthdays: Vec<BirthdayEntry> = global_data
.db
.filter(|_, _: &BirthdayEntry| true)?
.collect();
let mut msg_builder = MessageBuilder::new();
msg_builder.push_bold_line("All the birthdays I know:");
for birthday in birthdays {
let user = msg
.guild(&ctx.cache)
.unwrap()
.member(&ctx.http, UserId::from(birthday.discord_id))
.await?;
msg_builder.push_line(format!(
"* {} {}",
user.display_name(),
birthday.birthday.format("%m-%d-%Y")
));
}
msg.reply(&ctx.http, msg_builder.build()).await?;
Ok(())
}