feat(discord): Moved most strings to Strings

- Added Error::AccountIsSelf variant
- /balance give to self now returns error
This commit is contained in:
Alexey 2025-12-17 14:43:40 +03:00
commit 787118309a
7 changed files with 247 additions and 85 deletions

View file

@ -1,23 +1,24 @@
use poise::serenity_prelude::UserId;
use poise::serenity_prelude::User;
use squad_quest::{SquadObject, account::Account, map::Map};
use crate::{Context, Error, account::{account_full_balance, account_rooms_value, account_user_id, fetch_or_init_account}};
use crate::{Context, Error, account::{account_full_balance, account_user_id, fetch_or_init_account}, strings::StringFormatter};
async fn account_balance_string(ctx: &Context<'_>, account: &Account, map: &Map) -> String {
let rooms_value = account_rooms_value(account, map);
let full_balance = account_full_balance(account, map);
async fn account_balance_string(
ctx: &Context<'_>,
account: &Account,
map: &Map,
mut formatter: StringFormatter
) -> String {
let account_id = account_user_id(&account);
let Ok(user) = account_id
.to_user(ctx)
.await else {
let Ok(user) = account_id.to_user(ctx).await else {
return String::new();
};
let name = user.display_name();
format!("\n{name}: **{full_balance}** points (**{balance}** on balance \
+ **{rooms_value}** unlocked rooms networth)",
balance = account.balance,
)
let strings = &ctx.data().strings;
formatter = formatter.user(&user).balance(account, map);
formatter.fmt(&strings.scoreboard.line_format)
}
#[poise::command(
@ -28,11 +29,11 @@ async fn account_balance_string(ctx: &Context<'_>, account: &Account, map: &Map)
)]
pub async fn reset(
ctx: Context<'_>,
who: UserId,
who: User,
) -> Result<(), Error> {
let accounts = ctx.data().config.load_accounts();
let acc_id = format!("{}", who.get());
let acc_id = format!("{}", who.id.get());
if let None = accounts.iter().find(|a| a.id == acc_id) {
return Err(Error::AccountNotFound);
@ -42,7 +43,10 @@ pub async fn reset(
path.push(format!("{acc_id}.toml"));
Account::delete(path)?;
let reply_string = "User was successfully reset.".to_string();
let strings = &ctx.data().strings;
let formatter = strings.formatter().user(&who);
let reply_string = formatter.fmt(&strings.account.reset);
ctx.reply(reply_string).await?;
Ok(())
@ -59,6 +63,9 @@ pub async fn scoreboard(
let map_path = ctx.data().config.full_map_path();
let map = Map::load(map_path).expect("map.toml should exist");
let strings = &ctx.data().strings;
let mut formatter = strings.formatter();
let mut accounts = ctx.data().config.load_accounts();
accounts.sort_by(|a,b| {
let a_balance = account_full_balance(a, &map);
@ -68,13 +75,14 @@ pub async fn scoreboard(
let this_user = ctx.author().id;
let mut reply_string = "Current scoreboard:".to_string();
let mut reply_string = formatter.fmt(&strings.scoreboard.header);
for account in accounts {
let user_id = account_user_id(&account);
let mut str = account_balance_string(&ctx, &account, &map).await;
let mut str = account_balance_string(&ctx, &account, &map, formatter.clone()).await;
if user_id == this_user {
str = format!("__{str}__ << You");
formatter = formatter.text(&str);
str = formatter.fmt(&strings.scoreboard.you_format);
}
reply_string.push_str(&str);
@ -104,16 +112,20 @@ pub async fn balance(
)]
pub async fn give(
ctx: Context<'_>,
who: UserId,
who: User,
amount: u32,
) -> Result<(), Error> {
if ctx.author() == &who {
return Err(Error::AccountIsSelf);
}
let config = &ctx.data().config;
let mut accounts = config.load_accounts();
let user_id = format!("{}", ctx.author().id.get());
let mut user_account = fetch_or_init_account(config, user_id);
let who_id = format!("{}", who.get());
let who_id = format!("{}", who.id.get());
let Some(other_account) = accounts.iter_mut().find(|a| a.id == who_id ) else {
return Err(Error::AccountNotFound);
};
@ -129,8 +141,13 @@ pub async fn give(
user_account.save(accounts_path.clone())?;
other_account.save(accounts_path)?;
let reply_string = format!("Given money to user.\n\
Your new balance: {} points.", user_account.balance);
let strings = &ctx.data().strings;
let formatter = strings.formatter()
.user(&who)
.value(amount)
.current_balance(&user_account);
let reply_string = formatter.fmt(&strings.account.give_pt);
ctx.reply(reply_string).await?;
Ok(())
@ -144,12 +161,12 @@ pub async fn give(
)]
pub async fn set(
ctx: Context<'_>,
who: UserId,
who: User,
amount: u32,
) -> Result<(), Error> {
let mut accounts = ctx.data().config.load_accounts();
let who_id = format!("{}", who.get());
let who_id = format!("{}", who.id.get());
let Some(account) = accounts.iter_mut().find(|a| a.id == who_id ) else {
return Err(Error::AccountNotFound);
};
@ -157,8 +174,13 @@ pub async fn set(
account.balance = amount;
let accounts_path = ctx.data().config.full_accounts_path();
account.save(accounts_path)?;
let strings = &ctx.data().strings;
let formatter = strings.formatter()
.user(&who)
.current_balance(&account);
let reply_string = format!("Set user balance to {amount}.");
let reply_string = formatter.fmt(&strings.account.set_pt);
ctx.reply(reply_string).await?;
Ok(())