use a database :D

This commit is contained in:
2024-12-07 00:21:52 -05:00
parent 9fd85e38fb
commit 500c6a6eee
8 changed files with 112 additions and 65 deletions

View File

@@ -1,4 +1,3 @@
use super::get_user_wealth_mut;
use crate::common::{Context, Error};
use poise::serenity_prelude as serenity;
@@ -6,16 +5,15 @@ use poise::serenity_prelude as serenity;
#[poise::command(slash_command, prefix_command)]
pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> {
let user = user.as_ref().unwrap_or(ctx.author());
let mut users = ctx.data().users.lock().await;
let wealth = get_user_wealth_mut(&mut users, user.id);
let wealth = super::get_balance(user.id, &ctx.data()).await?;
ctx.reply(format!("{} **{}** token(s).",
if user.id == ctx.author().id {
"You have".to_string()
} else {
format!("{} has", user.name)
}, *wealth)).await?;
}, wealth)).await?;
Ok(())
}

View File

@@ -1,30 +1,29 @@
use crate::{Context, Error};
use super::get_user_wealth_mut;
use poise::serenity_prelude as serenity;
/// Generously donate your tokens to someone else
#[poise::command(slash_command, prefix_command)]
pub async fn give(ctx: Context<'_>, user: serenity::User, amount: usize) -> Result<(), Error> {
pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32) -> Result<(), Error> {
if user.bot {
ctx.reply("Don't waste your token(s) by giving them to a bot!").await?;
ctx.reply("Don't waste your tokens by giving them to a bot!").await?;
return Ok(());
}
let mut users = ctx.data().users.lock().await;
let author_wealth = get_user_wealth_mut(&mut users, ctx.author().id);
let data = ctx.data();
if *author_wealth < amount {
ctx.reply(format!("You only have **{}** token(s) and cannot give away **{}**.",
*author_wealth, amount)).await?;
return Ok(());
let author_balance = super::get_balance(ctx.author().id, &data).await?;
if author_balance < amount {
ctx.reply(format!("You do not have a high enough balance (**{author_balance}**) to complete this transaction.")).await?;
} else {
let author_new_balance = author_balance - amount;
let reciever_new_balance = super::get_balance(user.id, &data).await? + amount;
super::change_balance(user.id, reciever_new_balance, &data).await?;
super::change_balance(ctx.author().id, author_new_balance, data).await?;
ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
}
*author_wealth -= amount;
let receiver_wealth = get_user_wealth_mut(&mut users, user.id);
*receiver_wealth += amount;
ctx.reply(format!("You've given **{}** **{}** token(s).", user.name, amount)).await?;
Ok(())
}

View File

@@ -1,14 +1,35 @@
use std::collections::HashMap;
use poise::serenity_prelude::UserId;
pub mod balance;
pub mod give;
pub mod wager;
pub(self) fn get_user_wealth_mut(users: &mut HashMap<UserId, usize>, id: UserId) -> &mut usize {
if users.get(&id).is_none() {
users.insert(id, 100);
}
use crate::common::{Data, Error};
use poise::serenity_prelude::UserId;
use sqlx::Row;
users.get_mut(&id).unwrap()
pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
let db = &data.database;
let row = sqlx::query("SELECT balance FROM bank WHERE id = $1")
.bind(id.get() as i64)
.fetch_one(db).await.ok();
let balance = if let Some(row) = row {
row.try_get("balance")?
} else {
100
};
Ok(balance)
}
pub async fn change_balance(id: UserId, balance: i32, data: &Data) -> Result<(), Error> {
let db = &data.database;
sqlx::query("INSERT INTO bank (id, balance) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance")
.bind(id.get() as i64)
.bind(balance)
.execute(db).await?;
Ok(())
}

View File

@@ -1,32 +1,32 @@
use crate::common::{Context, Error};
use super::get_user_wealth_mut;
/// Put forward an amount of tokens to either lose or earn
#[poise::command(slash_command, prefix_command)]
pub async fn wager(
ctx: Context<'_>,
#[min = 1]
amount: usize) -> Result<(), Error>
amount: i32) -> Result<(), Error>
{
let mut users = ctx.data().users.lock().await;
let data = ctx.data();
let mut wealth = super::get_balance(ctx.author().id, &data).await?;
let wealth = get_user_wealth_mut(&mut users, ctx.author().id);
if *wealth < amount {
if wealth < amount {
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
*wealth)).await?;
wealth)).await?;
return Ok(());
}
if rand::random() {
*wealth += amount;
wealth += amount;
ctx.reply(format!("You just gained **{}** token(s)! You now have **{}**.",
amount, *wealth)).await?;
amount, wealth)).await?;
} else {
*wealth -= amount;
wealth -= amount;
ctx.reply(format!("You've lost **{}** token(s), you now have **{}**.",
amount, *wealth)).await?;
amount, wealth)).await?;
}
super::change_balance(ctx.author().id, wealth, &data).await?;
Ok(())
}