diff --git a/discord/src/account.rs b/discord/src/account.rs new file mode 100644 index 0000000..50ebb03 --- /dev/null +++ b/discord/src/account.rs @@ -0,0 +1,12 @@ +use squad_quest::{account::Account, config::Config}; + +pub fn fetch_or_init_account(conf: &Config, id: String) -> Account { + let accounts = conf.load_accounts(); + match accounts.iter().find(|a| a.id == id) { + Some(a) => a.clone(), + None => Account { + id, + ..Default::default() + }, + } +} diff --git a/discord/src/commands/answer.rs b/discord/src/commands/answer.rs index beaa7bd..f2b0816 100644 --- a/discord/src/commands/answer.rs +++ b/discord/src/commands/answer.rs @@ -1,6 +1,7 @@ use poise::serenity_prelude::{Attachment, ComponentInteractionCollector, CreateActionRow, CreateAttachment, CreateButton, CreateMessage, EditMessage, Mentionable}; +use squad_quest::SquadObject; -use crate::{Context, Error}; +use crate::{Context, Error, account::fetch_or_init_account}; #[poise::command( prefix_command, @@ -103,6 +104,41 @@ pub async fn answer( let builder = EditMessage::new().content(content).components(Vec::new()); message.edit(ctx, builder).await?; + + let content: String; + if is_approved { + let mut no_errors = true; + let mut account = fetch_or_init_account(&ctx.data().config, ctx.author().id.to_string()); + if let Err(error) = quest.complete_for_account(&mut account) { + eprintln!("{error}"); + no_errors = false; + }; + let path = ctx.data().config.full_accounts_path(); + if let Err(error) = account.save(path) { + eprintln!("{error}"); + no_errors = false; + }; + + if no_errors { + content = format!("Your answer to the quest #{quest_id} has been approved.\n\ + You gained {reward} points.\n\ + Your balance is now {balance} points", + reward = quest.reward, + balance = account.balance + ); + } else { + content = format!("Your answer to the quest #{quest_id} has been approved, \ + but some server error happened. \ + Please contact administrator for details." + ); + } + } else { + content = format!("Your answer to the quest #{quest_id} has been rejected."); + }; + let dm_builder = CreateMessage::new().content(content); + ctx.author().dm(ctx, dm_builder).await?; + + return Ok(()); } diff --git a/discord/src/main.rs b/discord/src/main.rs index 5792186..2d5fd94 100644 --- a/discord/src/main.rs +++ b/discord/src/main.rs @@ -10,6 +10,7 @@ use crate::config::{ConfigImpl, DiscordConfig}; mod commands; mod cli; mod config; +mod account; struct Data { pub config: Config,