From 818fdc74121882db2dbd1d3ce8944a543f270d3d Mon Sep 17 00:00:00 2001 From: wlinator Date: Thu, 22 Aug 2024 23:10:21 +0200 Subject: [PATCH] refactor give emote --- config/JSON/strings.json | 7 ++++++- modules/economy/give.py | 32 ++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/config/JSON/strings.json b/config/JSON/strings.json index 20313d0..3a08bd7 100644 --- a/config/JSON/strings.json +++ b/config/JSON/strings.json @@ -284,5 +284,10 @@ "xp_server_rank": "Server Rank: #{0}", "balance_cash": "**Cash**: ${0}", "balance_author": "{0}'s wallet", - "balance_footer": "Check out /daily" + "balance_footer": "check out /daily", + "give_error_self": "you can't give money to yourself.", + "give_error_bot": "you can't give money to a bot.", + "give_error_invalid_amount": "invalid amount.", + "give_error_insufficient_funds": "you don't have enough cash.", + "give_success": "**{0}** gave **${1}** to {2}." } \ No newline at end of file diff --git a/modules/economy/give.py b/modules/economy/give.py index b9e9acf..07ffdeb 100644 --- a/modules/economy/give.py +++ b/modules/economy/give.py @@ -2,24 +2,24 @@ import discord from discord.ext import commands from services.currency_service import Currency +from lib.constants import CONST +from lib.embed_builder import EmbedBuilder +from lib.exceptions.LumiExceptions import LumiException -async def cmd(ctx, user, amount): +async def cmd(ctx: commands.Context, user: discord.User, amount: int) -> None: if ctx.author.id == user.id: - raise commands.BadArgument("you can't give money to yourself.") - elif user.bot: - raise commands.BadArgument("you can't give money to a bot.") - elif amount <= 0: - raise commands.BadArgument("invalid amount.") + raise LumiException(CONST.STRINGS["give_error_self"]) + if user.bot: + raise LumiException(CONST.STRINGS["give_error_bot"]) + if amount <= 0: + raise LumiException(CONST.STRINGS["give_error_invalid_amount"]) - # Currency handler ctx_currency = Currency(ctx.author.id) target_currency = Currency(user.id) - author_balance = ctx_currency.balance - - if author_balance < amount or author_balance <= 0: - raise commands.BadArgument("you don't have enough cash.") + if ctx_currency.balance < amount: + raise LumiException(CONST.STRINGS["give_error_insufficient_funds"]) target_currency.add_balance(amount) ctx_currency.take_balance(amount) @@ -27,9 +27,13 @@ async def cmd(ctx, user, amount): ctx_currency.push() target_currency.push() - embed = discord.Embed( - color=discord.Color.green(), - description=f"**{ctx.author.name}** gave **${Currency.format(amount)}** to {user.name}.", + embed = EmbedBuilder.create_success_embed( + ctx, + description=CONST.STRINGS["give_success"].format( + ctx.author.name, + Currency.format(amount), + user.name, + ), ) await ctx.respond(embed=embed)