diff --git a/src/discord/mod.rs b/src/discord/mod.rs index d43a412..23d3f9e 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -22,8 +22,9 @@ use serenity::framework::standard::{ help_commands, Args, CommandGroup, CommandResult, HelpOptions, }; use serenity::model::channel::{Message, ReactionType}; +use serenity::model::gateway::Activity; use serenity::model::id::UserId; -use serenity::model::prelude::{GuildId, Ready}; +use serenity::model::prelude::{GuildId, OnlineStatus, Ready}; use serenity::prelude::EventHandler; use std::collections::HashSet; use std::time::Duration; @@ -38,21 +39,20 @@ impl EventHandler for Handler { async fn cache_ready(&self, ctx: Context, _guilds: Vec) { tokio::spawn(async move { loop { + { + println!("Restocking shop..."); + restock_shop(&ctx).await.unwrap(); + } + + tokio::time::sleep(Duration::from_secs(60 * 60)).await; { { println!("Reloading config..."); let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); - global_data.reload().await.unwrap(); } } - - { - println!("Restocking shop..."); - restock_shop(&ctx).await.unwrap(); - } - tokio::time::sleep(Duration::from_secs(60 * 60)).await; } }); } @@ -118,8 +118,14 @@ impl EventHandler for Handler { .unwrap(); } - async fn ready(&self, _: Context, ready: Ready) { + async fn ready(&self, ctx: Context, ready: Ready) { println!("Connected as {}", ready.user.name); + + ctx.set_presence( + Some(Activity::listening("to your deepest secrets")), + OnlineStatus::Online, + ) + .await; } } diff --git a/src/discord/shop.rs b/src/discord/shop.rs index 5866413..a3824d9 100644 --- a/src/discord/shop.rs +++ b/src/discord/shop.rs @@ -15,7 +15,7 @@ use std::hash::Hasher; use tokio::io::AsyncWriteExt; #[group] -#[commands(shop, buy, inventory, use_item)] +#[commands(shop, buy, inventory, use_item, sell_item)] pub struct Shop; #[command] @@ -43,7 +43,7 @@ async fn shop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { } #[command] -#[description("Open your inventory, is specifically can not be rebound to the B key")] +#[description("Open your inventory, it specifically can not be rebound to the B key")] async fn inventory(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); @@ -120,7 +120,7 @@ async fn buy(ctx: &Context, msg: &Message, args: Args) -> CommandResult { #[command] #[only_in(guilds)] #[aliases("use")] -#[description("Buying something?")] +#[description("Use it or lose it")] async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); @@ -219,6 +219,62 @@ async fn use_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { Ok(()) } +#[command] +#[only_in(guilds)] +#[aliases("sell")] +#[description("Sell it or smell it")] +async fn sell_item(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let mut data = ctx.data.write().await; + let global_data = data.get_mut::().unwrap(); + + let item = match args.rest().parse::() { + Ok(i) => i, + Err(_) => { + msg.reply(&ctx.http, "I don't know what the heck that is tbh.") + .await?; + return Ok(()); + } + }; + + match User::item_transaction( + &global_data.db, + msg.author.id, + ctx.cache.current_user_id(), + item, + Operation::Sell, + ) { + Ok(_) => { + msg.reply(&ctx.http, "I guess I could buy that...").await?; + } + Err(err) => { + if let Error::UserError(err) = err { + match err { + UserError::NotEnoughFunds => { + msg.reply(&ctx.http, "What do I look like a charity service?") + .await?; + } + UserError::InvalidTarget => { + msg.reply(&ctx.http, "If you got here, that means I'm not real...????") + .await?; + } + UserError::InventoryError(InventoryError::NotEnoughItems) => { + msg.reply( + &ctx.http, + "You don't have enough of that item to sell it lmao", + ) + .await?; + } + _ => {} + } + } else { + return Err(err.into()); + } + } + } + + Ok(()) +} + pub async fn restock_shop(ctx: &Context) -> Result<(), CommandError> { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); diff --git a/src/inventory/mod.rs b/src/inventory/mod.rs index ae24906..452c80b 100644 --- a/src/inventory/mod.rs +++ b/src/inventory/mod.rs @@ -102,7 +102,7 @@ impl InventorySlot { let per_value = (hash as f64) / (u64::MAX as f64); - ((2000.0 * per_value) as i64) - 1000 + ((2000.0 * per_value) as i64) - 500 } else { 0 } @@ -150,7 +150,13 @@ impl InventoryManager { }), Some(slot) => { slot.item_data = item_data; - slot.quantity += quantity; + + // Only one NFT at a time + if item_type != ItemType::Nft { + slot.quantity += quantity; + } else { + slot.quantity = 1; + } } } }