Cleanup and refactoring wallets

+ clippy + fmt
This commit is contained in:
2022-12-20 19:30:56 -07:00
parent 137f01a9b2
commit d8489d336f
7 changed files with 127 additions and 71 deletions
+21 -18
View File
@@ -1,5 +1,6 @@
use crate::config::AlbumConfig;
use crate::error::Error;
use crate::imgur::get_album_images;
use crate::{command, group, GlobalData};
use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult};
@@ -38,29 +39,33 @@ async fn add_album(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
let global_data = data.get_mut::<GlobalData>().unwrap();
let old_config = global_data.cfg.clone();
let images = match get_album_images(&global_data.cfg.imgur_client_id, &album_id).await {
Ok(img) => img,
Err(e) => {
msg.reply(
&ctx.http,
"Error adding album, check your link and try again",
)
.await?;
println!("Error adding album: {}", e);
return Ok(());
}
};
global_data.cfg.albums.push(AlbumConfig {
album_id,
name: album_name.clone(),
});
global_data.cfg.save(&global_data.args.cfg_path).await?;
global_data
.bot_state
.albums
.insert(album_name.clone(), images);
if global_data.reload().await.is_err() {
global_data.cfg = old_config;
global_data.cfg.save(&global_data.args.cfg_path).await?;
msg.reply(
&ctx.http,
"Error adding album, check your link and try again",
)
msg.reply(&ctx.http, format!("{} album added!", album_name))
.await?;
} else {
msg.reply(&ctx.http, format!("{} album added!", album_name))
.await?;
}
Ok(())
}
@@ -82,9 +87,7 @@ async fn remove_album(ctx: &Context, msg: &Message, args: Args) -> CommandResult
.albums
.retain(|album| !album.name.eq_ignore_ascii_case(&album_name));
global_data.cfg.save(&global_data.args.cfg_path).await?;
global_data.reload().await?;
global_data.bot_state.albums.remove(&album_name);
msg.reply(&ctx.http, format!("{} album removed!", album_name))
.await?;
+25 -45
View File
@@ -1,5 +1,6 @@
use crate::config::{BotConfig, GlobalData, Wallet};
use crate::config::GlobalData;
use crate::error::Error;
use crate::wallet::WalletError;
use crate::{command, group};
use rand::{thread_rng, Rng};
use serenity::client::Context;
@@ -12,18 +13,6 @@ use serenity::prelude::Mentionable;
#[commands(balance, gift)]
pub struct FrenCoin;
pub fn get_user_wallet(cfg: &mut BotConfig, discord_id: UserId) -> &mut Wallet {
if let Some(user_ndx) = cfg.wallets.iter().position(|u| u.owner == discord_id) {
&mut cfg.wallets[user_ndx]
} else {
cfg.wallets.push(Wallet {
owner: discord_id,
coin_count: 100,
});
cfg.wallets.last_mut().unwrap()
}
}
#[command]
#[description("Get your current balance")]
#[aliases("audit")]
@@ -34,7 +23,7 @@ async fn balance(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let user = args.parse::<UserId>().unwrap_or(msg.author.id);
let wallet = get_user_wallet(&mut global_data.cfg, user);
let wallet = global_data.cfg.wallet_manager.get_user_wallet(user);
msg.reply(
&ctx.http,
@@ -51,8 +40,6 @@ async fn balance(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
)
.await?;
global_data.cfg.save(&global_data.args.cfg_path).await?;
Ok(())
}
@@ -73,47 +60,40 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
args.advance();
let amount = args.parse::<i64>()?;
let amount = args.parse::<u32>()?;
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
if let Err(e) = global_data
.cfg
.wallet_manager
.transfer_funds(msg.author.id, target, amount)
{
let author_wallet = get_user_wallet(&mut global_data.cfg, msg.author.id);
if author_wallet.coin_count < amount {
if let WalletError::NotEnoughFunds = e {
msg.reply(
&ctx.http,
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.",
)
.await?;
return Ok(());
}
author_wallet.coin_count -= amount;
}
{
let target_wallet = get_user_wallet(&mut global_data.cfg, target);
target_wallet.coin_count += amount;
}
let target_user = msg
.guild(&ctx.cache)
.unwrap()
.member(&ctx.http, target)
} else {
let target_user = msg
.guild(&ctx.cache)
.unwrap()
.member(&ctx.http, target)
.await?;
msg.reply(
&ctx.http,
format!(
"You have gifted {} {} fren coins!",
target_user.mention(),
amount
),
)
.await?;
msg.reply(
&ctx.http,
format!(
"You have gifted {} {} fren coins!",
target_user.mention(),
amount
),
)
.await?;
}
global_data.cfg.save(&global_data.args.cfg_path).await?;
Ok(())
}
@@ -134,7 +114,7 @@ pub async fn give_coin(
let global_data = data.get_mut::<GlobalData>().unwrap();
{
let wallet = get_user_wallet(&mut global_data.cfg, user);
let wallet = global_data.cfg.wallet_manager.get_user_wallet(user);
wallet.coin_count += numer_of_coins;
}
+8
View File
@@ -123,6 +123,14 @@ pub async fn after(
Ok(()) => {
println!("Processed command '{}'", command_name);
give_coin(ctx, msg.author.id, 0.10, 10).await.unwrap();
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
global_data
.cfg
.save(&global_data.args.cfg_path)
.await
.expect("Error saving config");
}
Err(why) => {
println!("Command '{}' returned error {:?}", command_name, why);