use crate::config::GlobalData; use crate::models::lil_fren; use crate::models::lil_fren::{AliveState, LilFren, LilFrenState}; use crate::{command, group}; use rand::prelude::SliceRandom; use rand::{thread_rng, Rng}; use serenity::client::Context; use serenity::framework::standard::{Args, CommandResult}; use serenity::model::channel::Message; use serenity::model::misc::EmojiIdentifier; use serenity::utils::MessageBuilder; #[group] #[commands(adopt, checkup, feed, give_water, play, give_medicine)] pub struct Buddy; #[command] #[description("Adopt a new lil buddy")] #[only_in(guilds)] async fn adopt(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); if let Ok(emoji) = args.parse::() { if let Some(lil_fren) = LilFren::get_lil_fren(&global_data.db)? { if lil_fren.is_alive() == AliveState::Alive { msg.reply( &ctx.http, "Your buddy is still alive, please take care of him :)", ) .await?; return Ok(()); } } LilFren::create_new_lil_fren(&global_data.db, emoji.id)?; let guild = msg.guild_id.unwrap(); let emoji = guild.emoji(&ctx.http, emoji.id).await?; let mut msg_builder = MessageBuilder::new(); msg_builder.push("Congrats on adopting "); msg_builder.emoji(&emoji); msg_builder.push("!"); msg.reply(&ctx.http, msg_builder.build()).await?; } else { msg.reply(&ctx.http, "Sorry whatever that is not adoptable.") .await?; } Ok(()) } #[command] #[only_in(guilds)] #[description("Checkup on your lil buddy")] async fn checkup(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); let lil_fren = LilFren::get_lil_fren(&global_data.db)?; if let Some(lil_fren) = lil_fren { let guild = msg.guild_id.unwrap(); let alive_state = lil_fren.is_alive(); if alive_state == AliveState::Alive { let state_msg = match lil_fren.state { LilFrenState::Standing => "Little buddy is standing.", LilFrenState::TaxFraud => "Your buddy is comiting tax fraud", LilFrenState::Sick => "Oh no! Buddy is sick!", LilFrenState::Dancing => "Buddy got some moves!", LilFrenState::Sleep => "Shhh, buddy is asleep", LilFrenState::Magic => "Wizard Buddy loves casting spells", LilFrenState::AttemptingToCircumventDeath => { "Buddy appears to be researching how to be come immortal" } LilFrenState::AttemptingToNotCauseAResonanceCascade => { "Lil buddy is trying to prevent a resonance cascade" } LilFrenState::Mining => "Lil buddy found some diamonds!", LilFrenState::PhasedIntoYourReality => { "Lil Buddy has phased into your reality, they will be back soon" } }; msg.reply(&ctx.http, state_msg).await?; msg.reply(&ctx.http, lil_fren.draw(ctx, &guild).await) .await?; } else { let resp = match alive_state { AliveState::Alive => "", AliveState::DiedOfBoredom => "Your little buddy died of boredom RIP", AliveState::DiedOfHunger => "Your little buddy died of hunger RIP", AliveState::DiedOfThirst => "Your little buddy died of thirst", }; msg.reply(&ctx.http, resp).await?; } } else { msg.reply( &ctx.http, "Sorry you have no little buddy right now. Please adopt!", ) .await?; } Ok(()) } #[command] #[description("Feed lil buddy")] async fn feed(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); if let Ok(food_emoji) = args.parse::() { let lil_fren = LilFren::get_lil_fren(&global_data.db)?; if let Some(mut lil_fren) = lil_fren { if lil_fren.is_alive() == AliveState::Alive { let feed: f32 = thread_rng().gen_range(-0.1..=1.0); lil_fren.hunger = (lil_fren.hunger + feed).clamp(-1.0, 1.0); let guild = msg.guild_id.unwrap(); let lil_fren_emoji = guild.emoji(&ctx.http, lil_fren.emoji).await?; if feed > 0.0 { msg.reply(&ctx.http, "Lil buddy seemed to like that!") .await?; } else { msg.reply(&ctx.http, "Lil buddy DID not like that!").await?; } msg.reply(&ctx.http, lil_fren::draw_feed(&lil_fren_emoji, &food_emoji)) .await?; global_data.db.insert(lil_fren)?; } else { msg.reply(&ctx.http, "You may want to check on your buddy...") .await?; } } else { msg.reply(&ctx.http, "Sorry no little buddy found!").await?; } } else { msg.reply(&ctx.http, "Please provide something to feed lil buddy") .await?; } Ok(()) } #[command] #[description("Give lil buddy some water")] async fn give_water(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); let lil_fren = LilFren::get_lil_fren(&global_data.db)?; if let Some(mut lil_fren) = lil_fren { if lil_fren.is_alive() == AliveState::Alive { lil_fren.thirst = (lil_fren.thirst + 0.5).clamp(-1.0, 1.0); let guild = msg.guild_id.unwrap(); let lil_fren_emoji = guild.emoji(&ctx.http, lil_fren.emoji).await?; msg.reply(&ctx.http, "Lil buddy be drinking!").await?; global_data.db.insert(lil_fren)?; let drink = if thread_rng().gen_bool(0.20) { ":beer:" } else { ":cup_with_straw:" }; msg.reply(&ctx.http, lil_fren::draw_feed(&lil_fren_emoji, drink)) .await?; } else { msg.reply(&ctx.http, "You may want to check on your buddy...") .await?; } } else { msg.reply(&ctx.http, "Sorry no little buddy found!").await?; } Ok(()) } #[command] #[description("Play with buddy")] async fn play(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); let lil_fren = LilFren::get_lil_fren(&global_data.db)?; if let Some(mut lil_fren) = lil_fren { if lil_fren.is_alive() == AliveState::Alive { lil_fren.entertainment = (lil_fren.entertainment + thread_rng().gen_range(0.10..1.0)).clamp(-1.0, 1.0); let guild = msg.guild_id.unwrap(); let lil_fren_emoji = guild.emoji(&ctx.http, lil_fren.emoji).await?; global_data.db.insert(lil_fren)?; let toy = global_data.cfg.toys.choose(&mut thread_rng()).unwrap(); msg.reply(&ctx.http, lil_fren::draw_feed(&lil_fren_emoji, toy)) .await?; } else { msg.reply(&ctx.http, "You may want to check on your buddy...") .await?; } } else { msg.reply(&ctx.http, "Sorry no little buddy found!").await?; } Ok(()) } #[command] #[description("Help heal buddy")] async fn give_medicine(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); let lil_fren = LilFren::get_lil_fren(&global_data.db)?; if let Some(mut lil_fren) = lil_fren { if lil_fren.is_alive() == AliveState::Alive { let guild = msg.guild_id.unwrap(); let reply = if lil_fren.state == LilFrenState::Sick { lil_fren.state = LilFrenState::Standing; "Little buddy feels better now!" } else { "Little buddy is now addicted to cough drops, I hope you are happy" }; let lil_fren_emoji = guild.emoji(&ctx.http, lil_fren.emoji).await?; global_data.db.insert(lil_fren)?; msg.reply(&ctx.http, reply).await?; msg.reply(&ctx.http, lil_fren::draw_feed(&lil_fren_emoji, ":pill:")) .await?; } else { msg.reply(&ctx.http, "You may want to check on your buddy...") .await?; } } else { msg.reply(&ctx.http, "Sorry no little buddy found!").await?; } Ok(()) }