g alias for give, better transaction usage

This commit is contained in:
2024-12-10 15:36:17 -05:00
parent 4fdabbecc4
commit 8e09c19bd2

View File

@@ -2,8 +2,8 @@ use crate::{Context, Error};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
/// Generously donate your tokens to someone else /// Generously donate your tokens to someone else
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command, aliases("g"))]
pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32) -> Result<(), Error> { pub async fn give(ctx: Context<'_>, user: serenity::User, amount: i32) -> Result<(), Error> {
if user.bot { if user.bot {
ctx.reply("Don't waste your tokens by giving them to a bot!").await?; ctx.reply("Don't waste your tokens by giving them to a bot!").await?;
return Ok(()); return Ok(());
@@ -20,22 +20,17 @@ pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32
} }
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
let balance = super::get_balance(ctx.author().id, &mut *tx).await?;
let author_balance = super::get_balance(ctx.author().id, &mut *tx).await?; if balance < amount {
ctx.reply(format!("You do not have a high enough balance (**{balance}**) to complete this transaction.")).await?;
if author_balance < amount {
ctx.reply(format!("You do not have a high enough balance (**{author_balance}**) to complete this transaction.")).await?;
} else { } else {
let author_new_balance = author_balance - amount; super::change_balance(user.id, super::get_balance(user.id, &mut *tx).await? + amount, &mut *tx).await?;
let reciever_new_balance = super::get_balance(user.id, &mut *tx).await? + amount; super::change_balance(ctx.author().id, balance - amount, &mut *tx).await?;
tx.commit().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?; ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
} }
tx.commit().await?;
Ok(()) Ok(())
} }