feat: Add bank and wallet balance commands with improved transfer validation

- Added /bank and /wallet commands to check user balances
- Enhanced transfer validation to check for valid number input
- Included debug token logging in main function (to be removed in production)
- Added account creation logic for new users in balance commands
- Improved error handling for invalid transfer amounts
This commit is contained in:
2026-06-03 11:56:09 +00:00
parent 4b07ca86b9
commit b315069b1c
4 changed files with 168 additions and 84 deletions
+95 -19
View File
@@ -15,25 +15,28 @@ from utils.bank_functions import (
from utils.sql_commands import DatabaseManager
def validate_transfer(
payer_balance: int, author_id: int, receiver_id: int, amount: int
) -> tuple[bool, str]:
if amount <= 0:
return False, "Please enter an amount greater than 0."
if payer_balance is None:
return False, "Your account does not exist."
if author_id == receiver_id:
return False, "You cannot give yourself money."
if payer_balance < amount:
return False, (
f"You do not have {amount:,}<:flooney:1194943899765051473>. "
f"You have {payer_balance:,}<:flooney:1194943899765051473>."
)
return True, ""
def validate_transfer(
payer_balance: int, author_id: int, receiver_id: int, amount: int
) -> tuple[bool, str]:
# Validate amount is a positive number
if not isinstance(amount, (int, float)):
return False, "Amount must be a number"
if amount <= 0:
return False, "Please enter an amount greater than 0."
if payer_balance is None:
return False, "Your account does not exist."
if author_id == receiver_id:
return False, "You cannot give yourself money."
if payer_balance < amount:
return False, (
f"You do not have {amount:,}<:flooney:1194943899765051473>. "
f"You have {payer_balance:,}<:flooney:1194943899765051473>."
)
return True, ""
class Economy(commands.Cog):
@@ -400,6 +403,79 @@ class Economy(commands.Cog):
mention_author=False,
)
@commands.command(name="bank", brief="Check your bank balance", description="Check your bank balance.")
async def _bank(self, ctx: commands.Context, member: discord.Member | None = None):
"""Check your bank balance."""
target: discord.Member | discord.User = member or ctx.author
# Ensure the target is not a bot
if target.bot:
return
# Get the user's data
user_data = await bank_data(target)
wallet_balance = user_data.get("WALLET", 0)
bank_balance = user_data.get("BANK", 0)
# Create an account if one does not exist
if bank_balance is None or wallet_balance is None:
await create_account(target)
user_data = await bank_data(target)
wallet_balance = user_data.get("WALLET", 0)
bank_balance = user_data.get("BANK", 0)
# Reply with the user's bank balance
await ctx.reply(
f"{target.mention} has {bank_balance:,}<:flooney:1194943899765051473> in their bank."
)
@commands.command(name="wallet", brief="Check your wallet balance", description="Check your wallet balance.")
async def _wallet(self, ctx: commands.Context, member: discord.Member | None = None):
"""Check your wallet balance."""
target: discord.Member | discord.User = member or ctx.author
# Ensure the target is not a bot
if target.bot:
return
# Get the user's data
user_data = await bank_data(target)
wallet_balance = user_data.get("WALLET", 0)
bank_balance = user_data.get("BANK", 0)
# Create an account if one does not exist
if bank_balance is None or wallet_balance is None:
await create_account(target)
user_data = await bank_data(target)
wallet_balance = user_data.get("WALLET", 0)
bank_balance = user_data.get("BANK", 0)
# Reply with the user's wallet balance
await ctx.reply(
f"{target.mention} has {wallet_balance:,}<:flooney:1194943899765051473> in their wallet."
)
@commands.command(name="transfer", brief="Transfer money from bank to another user", description="Transfer money from your bank to another user's bank.")
async def _transfer(self, ctx, target: discord.Member, amount: int):
"""
Transfer money from your bank account to another user's bank account.
"""
payer_bank = int((await bank_data(ctx.author)).get("BANK", 0))
receiver_bank = int((await bank_data(target)).get("BANK", 0))
valid, reason = validate_transfer(
payer_bank, ctx.author.id, target.id, amount
)
if not valid:
return await ctx.reply(reason, mention_author=False)
await update_money(target, bank=amount)
await update_money(ctx.author, bank=-amount)
await ctx.reply(
f"Transferred {amount:,} flooneys to {target}.", mention_author=False
)
r"""
.----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. |