better database handling, rwlock daily, some minor other improvements
This commit is contained in:
@@ -9,23 +9,23 @@ pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let data = ctx.data();
|
||||
let mut db = data.database.lock().await;
|
||||
let db = db.as_mut();
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
let author_balance = super::get_balance(ctx.author().id, db).await?;
|
||||
let author_balance = super::get_balance(ctx.author().id, &mut *tx).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, db).await? + amount;
|
||||
let reciever_new_balance = super::get_balance(user.id, &mut *tx).await? + amount;
|
||||
|
||||
super::change_balance(user.id, reciever_new_balance, db).await?;
|
||||
super::change_balance(ctx.author().id, author_new_balance, db).await?;
|
||||
super::change_balance(user.id, reciever_new_balance, &mut *tx).await?;
|
||||
super::change_balance(ctx.author().id, author_new_balance, &mut *tx).await?;
|
||||
|
||||
ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user