Added a sell command

+ Added a bot status
+ Fixed NFTs so you can only have one at a time
+ clippy + fmt
This commit is contained in:
Joey Hines 2023-01-21 13:23:11 -07:00
parent 9f5d68f4b2
commit 6896ee610c
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
3 changed files with 82 additions and 14 deletions

View File

@ -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<GuildId>) {
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::<GlobalData>().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;
}
}

View File

@ -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::<GlobalData>().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::<GlobalData>().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::<GlobalData>().unwrap();
let item = match args.rest().parse::<ItemType>() {
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::<GlobalData>().unwrap();

View File

@ -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;
}
}
}
}