feat: Account initialization
- discord: /answer now DM-s user and updated user balance
This commit is contained in:
parent
1ae57ad358
commit
fb9fcfe6d7
3 changed files with 50 additions and 1 deletions
12
discord/src/account.rs
Normal file
12
discord/src/account.rs
Normal file
|
|
@ -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()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use poise::serenity_prelude::{Attachment, ComponentInteractionCollector, CreateActionRow, CreateAttachment, CreateButton, CreateMessage, EditMessage, Mentionable};
|
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(
|
#[poise::command(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
|
|
@ -103,6 +104,41 @@ pub async fn answer(
|
||||||
|
|
||||||
let builder = EditMessage::new().content(content).components(Vec::new());
|
let builder = EditMessage::new().content(content).components(Vec::new());
|
||||||
message.edit(ctx, builder).await?;
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use crate::config::{ConfigImpl, DiscordConfig};
|
||||||
mod commands;
|
mod commands;
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod account;
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue