use a database :D
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
Reference in New Issue
Block a user