no db pool
This commit is contained in:
@@ -5,8 +5,11 @@ 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 data = ctx.data();
|
||||
let mut db = data.database.lock().await;
|
||||
let db = db.as_mut();
|
||||
|
||||
let wealth = super::get_balance(user.id, &ctx.data()).await?;
|
||||
let wealth = super::get_balance(user.id, db).await?;
|
||||
|
||||
ctx.reply(format!("{} **{}** token(s).",
|
||||
if user.id == ctx.author().id {
|
||||
|
||||
@@ -10,17 +10,19 @@ pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32
|
||||
}
|
||||
|
||||
let data = ctx.data();
|
||||
let mut db = data.database.lock().await;
|
||||
let db = db.as_mut();
|
||||
|
||||
let author_balance = super::get_balance(ctx.author().id, &data).await?;
|
||||
let author_balance = super::get_balance(ctx.author().id, db).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;
|
||||
let reciever_new_balance = super::get_balance(user.id, db).await? + amount;
|
||||
|
||||
super::change_balance(user.id, reciever_new_balance, &data).await?;
|
||||
super::change_balance(ctx.author().id, author_new_balance, data).await?;
|
||||
super::change_balance(user.id, reciever_new_balance, db).await?;
|
||||
super::change_balance(ctx.author().id, author_new_balance, db).await?;
|
||||
|
||||
ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,11 @@ pub mod balance;
|
||||
pub mod give;
|
||||
pub mod wager;
|
||||
|
||||
use crate::common::{Data, Error};
|
||||
use crate::common::Error;
|
||||
use poise::serenity_prelude::UserId;
|
||||
use sqlx::Row;
|
||||
|
||||
pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
|
||||
let db = &data.database;
|
||||
use sqlx::{Row, PgConnection};
|
||||
|
||||
pub async fn get_balance(id: UserId, db: &mut PgConnection) -> Result<i32, Error> {
|
||||
let row = sqlx::query("SELECT balance FROM bank WHERE id = $1")
|
||||
.bind(id.get() as i64)
|
||||
.fetch_one(db).await.ok();
|
||||
@@ -23,9 +21,7 @@ pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
|
||||
Ok(balance)
|
||||
}
|
||||
|
||||
pub async fn change_balance(id: UserId, balance: i32, data: &Data) -> Result<(), Error> {
|
||||
let db = &data.database;
|
||||
|
||||
pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) -> Result<(), Error> {
|
||||
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)
|
||||
|
||||
@@ -8,7 +8,10 @@ pub async fn wager(
|
||||
amount: i32) -> Result<(), Error>
|
||||
{
|
||||
let data = ctx.data();
|
||||
let mut wealth = super::get_balance(ctx.author().id, &data).await?;
|
||||
let mut db = data.database.lock().await;
|
||||
let db = db.as_mut();
|
||||
|
||||
let mut wealth = super::get_balance(ctx.author().id, db).await?;
|
||||
|
||||
if wealth < amount {
|
||||
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
|
||||
@@ -26,7 +29,7 @@ pub async fn wager(
|
||||
amount, wealth)).await?;
|
||||
}
|
||||
|
||||
super::change_balance(ctx.author().id, wealth, &data).await?;
|
||||
super::change_balance(ctx.author().id, wealth, db).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user