Files
DiscordBot/utils/bank_functions.py
T
Nobody2503 b315069b1c 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
2026-06-03 11:56:09 +00:00

96 lines
2.9 KiB
Python
Executable File

import discord
from utils.sql_commands import DatabaseManager
from typing import Dict
from datetime import datetime
db = DatabaseManager()
async def create_account(user: discord.Member | discord.User):
"""Create a new economy account for a user."""
db.insert(
"INSERT INTO economy (ID, WALLET, BANK) VALUES (%s, %s, %s)", (user.id, 0, 1000)
)
async def bank_data(user: discord.Member | discord.User) -> Dict[str, int]:
"""Get the bank data for a user. If the user has no account, create one.
Args:
user: The user to get the bank data for.
Returns:
A dictionary with the keys ``WALLET`` and ``BANK`` containing the user's wallet and bank balances.
"""
balance = db.fetch_one("SELECT * FROM economy WHERE ID = %s", (user.id,))
if balance is None:
await create_account(user)
return await bank_data(user)
# Ensure we return a dictionary with WALLET and BANK keys
return {
"WALLET": balance.get("WALLET", 0),
"BANK": balance.get("BANK", 0)
}
async def record_transaction(
user_id: int, transaction_type: str, amount: float
) -> None:
"""Record a transaction in the database.
Args:
user_id: The ID of the user who performed the transaction.
transaction_type: The type of transaction performed.
amount: The amount of the transaction.
"""
db.execute_query(
"INSERT INTO transactions (USERID, TYPE, AMOUNT) VALUES (%s, %s, %s)",
(user_id, transaction_type, amount),
)
async def update_money(
user: discord.Member | discord.User, bank: int = 0, wallet: int = 0
):
"""Update the bank balance of a user.
Args:
user: The user to update the balance for.
bank: The amount to add to the user's bank balance.
wallet: The amount to add to the user's wallet balance.
Returns:
A dictionary with the keys ``WALLET`` and ``BANK`` containing the updated balances.
"""
balance = await bank_data(user)
if bank or wallet:
new_wallet = balance.get("WALLET", 0) + wallet
new_bank = balance.get("BANK", 0) + bank
db.insert(
"INSERT INTO economy (ID, WALLET, BANK) VALUES (%s, %s, %s)",
(user.id, new_wallet, new_bank),
overwrite=True,
)
async def reset_bank(user: discord.Member | discord.User) -> None:
"""Reset the bank account for a user.
Args:
user (discord.Member | discord.User): The user whose bank account is to be reset.
Returns:
None
"""
db.delete("economy", {"ID": user.id})
await create_account(user)
async def update_daily_timestamp(user: discord.User | discord.Member, timestamp: datetime) -> None:
"""
Updates the DAILY_TIMESTAMP field for the user in the economy table.
Stores the timestamp as a float (UNIX time).
"""
db.execute_query("UPDATE economy SET DAILY = %s WHERE ID = %s",(timestamp.timestamp(), user.id))