From 8e09c19bd2285164fd9fc636d6e9c7b956a030fc Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Tue, 10 Dec 2024 15:36:17 -0500 Subject: [PATCH] g alias for give, better transaction usage --- src/commands/gambling/give.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/commands/gambling/give.rs b/src/commands/gambling/give.rs index 9cab9ec..3be37b3 100644 --- a/src/commands/gambling/give.rs +++ b/src/commands/gambling/give.rs @@ -2,8 +2,8 @@ use crate::{Context, Error}; 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, #[min = 1] amount: i32) -> Result<(), Error> { +#[poise::command(slash_command, prefix_command, aliases("g"))] +pub async fn give(ctx: Context<'_>, user: serenity::User, amount: i32) -> Result<(), Error> { if user.bot { ctx.reply("Don't waste your tokens by giving them to a bot!").await?; 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 balance = super::get_balance(ctx.author().id, &mut *tx).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?; + if balance < amount { + ctx.reply(format!("You do not have a high enough balance (**{balance}**) to complete this transaction.")).await?; } else { - let author_new_balance = author_balance - amount; - let reciever_new_balance = super::get_balance(user.id, &mut *tx).await? + amount; - - super::change_balance(user.id, reciever_new_balance, &mut *tx).await?; - super::change_balance(ctx.author().id, author_new_balance, &mut *tx).await?; + super::change_balance(user.id, super::get_balance(user.id, &mut *tx).await? + amount, &mut *tx).await?; + super::change_balance(ctx.author().id, balance - amount, &mut *tx).await?; + tx.commit().await?; ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?; } - tx.commit().await?; - Ok(()) } \ No newline at end of file