Moved bot inventory to the db
+ Refactored item and shop handling a bit + fmt + clippy
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::config::{Channel, GlobalData};
|
||||
use crate::error::Error;
|
||||
use crate::user::{give_funds, try_take_funds, UserError};
|
||||
use crate::user::{User, UserError};
|
||||
use crate::{command, group};
|
||||
use rand::seq::IteratorRandom;
|
||||
use rand::{thread_rng, Rng};
|
||||
@@ -133,7 +133,7 @@ async fn add_bet(
|
||||
|
||||
let global = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
try_take_funds(&global.db, bet.author, bet.amount).map_err(|err| match err {
|
||||
User::try_take_funds(&global.db, bet.author, bet.amount).map_err(|err| match err {
|
||||
Error::UserError(e) => RaceError::BetFundError(e),
|
||||
_ => panic!("Recv'ed error when trying to bet: {}", err),
|
||||
})?;
|
||||
@@ -296,7 +296,7 @@ async fn race(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
winner_msg.mention(&winner);
|
||||
winner_msg.push_line("");
|
||||
|
||||
give_funds(&global_data.db, winner, payout as u32)?;
|
||||
User::give_funds(&global_data.db, winner, payout as u32)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::config::GlobalData;
|
||||
use crate::error::Error;
|
||||
use crate::user::{get_user, give_funds, transfer_funds, UserError};
|
||||
use crate::user::{User, UserError};
|
||||
use crate::{command, group};
|
||||
use rand::{thread_rng, Rng};
|
||||
use serenity::client::Context;
|
||||
@@ -23,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(&global_data.db, user)?;
|
||||
let wallet = User::get_user(&global_data.db, user)?;
|
||||
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
@@ -72,7 +72,7 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
if let Err(e) = transfer_funds(&global_data.db, msg.author.id, target, amount) {
|
||||
if let Err(e) = User::transfer_funds(&global_data.db, msg.author.id, target, amount) {
|
||||
if let Error::UserError(UserError::NotEnoughFunds) = e {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
@@ -116,7 +116,7 @@ pub async fn give_coin(
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
give_funds(&global_data.db, user, number_of_coins as u32)?;
|
||||
User::give_funds(&global_data.db, user, number_of_coins as u32)?;
|
||||
|
||||
global_data
|
||||
.cfg
|
||||
|
||||
+2
-2
@@ -77,7 +77,7 @@ pub fn render_random(
|
||||
|
||||
Ok(tera::Tera::one_off(
|
||||
template,
|
||||
&tera::Context::from_serialize(&random_ctx)?,
|
||||
&tera::Context::from_serialize(random_ctx)?,
|
||||
false,
|
||||
)?)
|
||||
}
|
||||
@@ -153,7 +153,7 @@ pub async fn add_random(ctx: &Context, msg: &Message, mut args: Args) -> Command
|
||||
#[command]
|
||||
#[example("8ball Funny Response haha")]
|
||||
pub async fn list_random(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
if args.len() < 1 {
|
||||
if args.is_empty() {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
"Look kid, you need to provide both the random name",
|
||||
|
||||
+38
-43
@@ -1,7 +1,7 @@
|
||||
use crate::discord::motivate::create_image;
|
||||
use crate::error::Error;
|
||||
use crate::inventory::{InventoryError, ItemData, ItemType};
|
||||
use crate::user::{get_user, give_item, try_take_funds, try_use_item, UserError};
|
||||
use crate::inventory::{InventoryError, ItemData, ItemType, Operation};
|
||||
use crate::user::{User, UserError};
|
||||
use crate::{command, group, GlobalData};
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::{thread_rng, Rng};
|
||||
@@ -23,7 +23,9 @@ async fn shop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
if global_data.cfg.bot_inventory.inventory.is_empty() {
|
||||
let bot_user = User::get_user(&global_data.db, ctx.cache.current_user_id())?;
|
||||
|
||||
if bot_user.inventory.inventory.is_empty() {
|
||||
msg.reply(&ctx.http, "Sorry shop is closed until we get more wares.")
|
||||
.await?;
|
||||
} else {
|
||||
@@ -31,7 +33,7 @@ async fn shop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
|
||||
inv_msg.push_bold_line("Fren has wares if you have coin:");
|
||||
|
||||
inv_msg.push_safe(global_data.cfg.bot_inventory.list_items(true));
|
||||
inv_msg.push_safe(bot_user.inventory.list_items(true));
|
||||
|
||||
msg.reply(&ctx.http, inv_msg.build()).await?;
|
||||
}
|
||||
@@ -47,7 +49,7 @@ async fn inventory(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
|
||||
let mut inv_msg = MessageBuilder::new();
|
||||
|
||||
let user = get_user(&global_data.db, msg.author.id)?;
|
||||
let user = User::get_user(&global_data.db, msg.author.id)?;
|
||||
|
||||
if user.inventory.inventory.is_empty() {
|
||||
msg.reply(&ctx, "Sorry your inventory is empty.").await?;
|
||||
@@ -86,38 +88,31 @@ async fn buy(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(item_slot) = global_data.cfg.bot_inventory.get_item(item) {
|
||||
if let Err(err) = try_take_funds(&global_data.db, msg.author.id, item_slot.value() as u32) {
|
||||
msg.reply(
|
||||
&ctx.http,
|
||||
format!(
|
||||
"I see why you don't get to the cloud district very often: {}",
|
||||
err
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
let res = User::item_transaction(
|
||||
&global_data.db,
|
||||
ctx.cache.current_user_id(),
|
||||
msg.author.id,
|
||||
item,
|
||||
Operation::Buy,
|
||||
);
|
||||
|
||||
if let Err(err) = res {
|
||||
if let Error::UserError(err) = err {
|
||||
let resp = match err {
|
||||
UserError::NotEnoughFunds => "Sorry Link, but I can't give store credit. Come back when you are mhhmm richer.".to_string(),
|
||||
UserError::InvalidTarget => "I don't know who you are or how you got here.".to_string(),
|
||||
UserError::InventoryError(err) => format!("Something went wrong with that item: {}", err)
|
||||
};
|
||||
|
||||
msg.reply(&ctx.http, resp).await?;
|
||||
} else {
|
||||
return Err(err.into());
|
||||
}
|
||||
} else {
|
||||
msg.reply(&ctx.http, "I don't have that in stock you goob")
|
||||
msg.reply(&ctx, format!("Congrats, you now own a '{}'", item))
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let item_data = match global_data.cfg.bot_inventory.try_take_item(item, 1) {
|
||||
Ok(i) => i,
|
||||
Err(err) => {
|
||||
msg.reply(&ctx.http, format!("I can't just sell you that: {}", err))
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
give_item(&global_data.db, msg.author.id, item, 1, item_data)?;
|
||||
|
||||
msg.reply(&ctx, format!("Congrats, you now own a '{}'", item))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -144,7 +139,7 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
}
|
||||
};
|
||||
|
||||
let item_data = match try_use_item(&global_data.db, msg.author.id, item) {
|
||||
let item_data = match User::try_use_item(&global_data.db, msg.author.id, item) {
|
||||
Ok(i) => i,
|
||||
Err(err) => {
|
||||
if let Error::UserError(UserError::InventoryError(InventoryError::NotEnoughItems)) = err
|
||||
@@ -227,19 +222,19 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
global_data.cfg.bot_inventory.inventory.clear();
|
||||
let mut bot_user = User::get_user(&global_data.db, ctx.cache.current_user_id())?;
|
||||
|
||||
global_data.cfg.bot_inventory.give_item(
|
||||
bot_user.inventory.inventory.clear();
|
||||
|
||||
bot_user.inventory.give_item(
|
||||
ItemType::TheConceptOfLove,
|
||||
thread_rng().gen_range(0..5),
|
||||
None,
|
||||
);
|
||||
global_data.cfg.bot_inventory.give_item(
|
||||
ItemType::GoodFortune,
|
||||
thread_rng().gen_range(0..10),
|
||||
None,
|
||||
);
|
||||
global_data.cfg.bot_inventory.give_item(
|
||||
bot_user
|
||||
.inventory
|
||||
.give_item(ItemType::GoodFortune, thread_rng().gen_range(0..10), None);
|
||||
bot_user.inventory.give_item(
|
||||
ItemType::CancelInsurance,
|
||||
thread_rng().gen_range(0..10),
|
||||
None,
|
||||
@@ -272,7 +267,7 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
|
||||
let mut file = tokio::fs::File::create(path.clone()).await?;
|
||||
file.write_all(&nft).await?;
|
||||
|
||||
global_data.cfg.bot_inventory.give_item(
|
||||
bot_user.inventory.give_item(
|
||||
ItemType::Nft,
|
||||
1,
|
||||
Some(ItemData::Nft(path.to_str().unwrap().to_string())),
|
||||
@@ -280,7 +275,7 @@ pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> {
|
||||
break;
|
||||
}
|
||||
|
||||
global_data.cfg.save(&global_data.args.cfg_path).await?;
|
||||
global_data.db.insert::<User>(bot_user)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user