Refactor project structure and update README; remove unused dependencies from requirements.txt and enhance database management in sql_commands.py.
This commit is contained in:
+64
-68
@@ -1,49 +1,39 @@
|
||||
import logging
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from utils.bank_functions import *
|
||||
from discord.ext import commands
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from random import randint
|
||||
from typing import Literal
|
||||
import datetime
|
||||
|
||||
from utils.bank_functions import (
|
||||
bank_data,
|
||||
create_account,
|
||||
reset_bank,
|
||||
update_daily_timestamp,
|
||||
update_money,
|
||||
)
|
||||
from utils.sql_commands import DatabaseManager
|
||||
|
||||
|
||||
async def check_transfer(
|
||||
ctx: commands.Context,
|
||||
payer_balance: int,
|
||||
receiver_balance: int,
|
||||
member: discord.Member,
|
||||
amount: int,
|
||||
) -> bool:
|
||||
"""Check if a transfer is valid.
|
||||
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."
|
||||
|
||||
Args:
|
||||
- ctx (commands.Context): The context of the invoked command.
|
||||
- payer_balance (int): The balance of the payer.
|
||||
- receiver_balance (int): The balance of the receiver.
|
||||
- member (discord.Member): The member to check against.
|
||||
- amount (int): The amount to transfer.
|
||||
if payer_balance is None:
|
||||
return False, "Your account does not exist."
|
||||
|
||||
Returns:
|
||||
- bool: Whether the transfer is valid.
|
||||
"""
|
||||
if payer_balance is None or receiver_balance is None:
|
||||
await ctx.reply("Bank account doesn't exist.")
|
||||
return False
|
||||
if payer_balance >= amount > 0:
|
||||
payer_balance = payer_balance - amount
|
||||
receiver_balance = receiver_balance + amount
|
||||
if ctx.author.id != member.id:
|
||||
return True
|
||||
else:
|
||||
await ctx.reply("You cannot give yourself money.")
|
||||
return False
|
||||
else:
|
||||
await ctx.reply(
|
||||
f"You do not have {int(amount):,}<:flooney:1194943899765051473>.\nYou have {int(payer_balance):,}<:flooney:1194943899765051473>."
|
||||
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 False
|
||||
|
||||
return True, ""
|
||||
|
||||
|
||||
class Economy(commands.Cog):
|
||||
@@ -213,13 +203,18 @@ class Economy(commands.Cog):
|
||||
payer_wallet = int((await bank_data(ctx.author)).get("WALLET", 0))
|
||||
receiver_wallet = int((await bank_data(target)).get("WALLET", 0))
|
||||
|
||||
if await check_transfer(ctx, payer_wallet, receiver_wallet, target, amount):
|
||||
await update_money(target, wallet=amount)
|
||||
await update_money(ctx.author, wallet=-amount)
|
||||
valid, reason = validate_transfer(
|
||||
payer_wallet, ctx.author.id, target.id, amount
|
||||
)
|
||||
if not valid:
|
||||
return await ctx.reply(reason, mention_author=False)
|
||||
|
||||
await ctx.reply(
|
||||
f"You gave {target} {amount:,} flooneys.", mention_author=False
|
||||
)
|
||||
await update_money(target, wallet=amount)
|
||||
await update_money(ctx.author, wallet=-amount)
|
||||
|
||||
await ctx.reply(
|
||||
f"You gave {target} {amount:,} flooneys.", mention_author=False
|
||||
)
|
||||
|
||||
@commands.command(name="transfer")
|
||||
async def _transfer(self, ctx, target: discord.Member, amount: int):
|
||||
@@ -229,13 +224,18 @@ class Economy(commands.Cog):
|
||||
payer_bank = int((await bank_data(ctx.author)).get("BANK", 0))
|
||||
receiver_bank = int((await bank_data(target)).get("BANK", 0))
|
||||
|
||||
if await check_transfer(ctx, payer_bank, receiver_bank, target, amount):
|
||||
await update_money(target, bank=amount)
|
||||
await update_money(ctx.author, bank=-amount)
|
||||
valid, reason = validate_transfer(
|
||||
payer_bank, ctx.author.id, target.id, amount
|
||||
)
|
||||
if not valid:
|
||||
return await ctx.reply(reason, mention_author=False)
|
||||
|
||||
await ctx.reply(
|
||||
f"Transferred {amount:,} flooneys to {target}.", 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
|
||||
)
|
||||
|
||||
@commands.command(
|
||||
name="deposit",
|
||||
@@ -248,7 +248,7 @@ class Economy(commands.Cog):
|
||||
|
||||
user_data = await bank_data(ctx.author)
|
||||
if user_data is None:
|
||||
await create_account(ctx)
|
||||
await create_account(ctx.author)
|
||||
user_data = await bank_data(ctx.author)
|
||||
|
||||
wallet_balance = int(user_data.get("WALLET", 0))
|
||||
@@ -268,7 +268,7 @@ class Economy(commands.Cog):
|
||||
|
||||
user_data = await bank_data(ctx.author)
|
||||
if user_data is None:
|
||||
await create_account(ctx)
|
||||
await create_account(ctx.author)
|
||||
user_data = await bank_data(ctx.author)
|
||||
|
||||
bank_balance = int(user_data.get("BANK", 0))
|
||||
@@ -328,7 +328,7 @@ class Economy(commands.Cog):
|
||||
title=f"Top {len(leaderboard_entries)} Richest Users - Leaderboard",
|
||||
description="\n".join(leaderboard_entries),
|
||||
color=discord.Color(0x00FF00),
|
||||
timestamp=datetime.datetime.utcnow(),
|
||||
timestamp=datetime.utcnow(),
|
||||
)
|
||||
embed.set_footer(text=f"GLOBAL - {ctx.guild.name}")
|
||||
await ctx.reply(embed=embed, mention_author=False)
|
||||
@@ -353,36 +353,32 @@ class Economy(commands.Cog):
|
||||
try:
|
||||
user_data = await bank_data(ctx.author)
|
||||
last_claim = user_data.get("DAILY")
|
||||
now = discord.utils.utcnow() # Modern, timezone-aware
|
||||
now = discord.utils.utcnow()
|
||||
|
||||
# Convert last_claim to datetime if it's a timestamp (int or float)
|
||||
if last_claim:
|
||||
if isinstance(last_claim, (int, float)):
|
||||
last_claim_dt = datetime.datetime.fromtimestamp(
|
||||
last_claim, tz=datetime.timezone.utc
|
||||
)
|
||||
last_claim_dt = datetime.fromtimestamp(last_claim, tz=timezone.utc)
|
||||
elif isinstance(last_claim, str):
|
||||
try:
|
||||
last_claim_dt = datetime.datetime.fromtimestamp(
|
||||
float(last_claim), tz=datetime.timezone.utc
|
||||
last_claim_dt = datetime.fromtimestamp(
|
||||
float(last_claim), tz=timezone.utc
|
||||
)
|
||||
except Exception:
|
||||
except ValueError:
|
||||
last_claim_dt = None
|
||||
elif isinstance(last_claim, datetime.datetime):
|
||||
# If it's naive, make it aware
|
||||
if last_claim.tzinfo is None:
|
||||
last_claim_dt = last_claim.replace(tzinfo=datetime.timezone.utc)
|
||||
else:
|
||||
last_claim_dt = last_claim
|
||||
elif isinstance(last_claim, datetime):
|
||||
last_claim_dt = (
|
||||
last_claim.replace(tzinfo=timezone.utc)
|
||||
if last_claim.tzinfo is None
|
||||
else last_claim
|
||||
)
|
||||
else:
|
||||
last_claim_dt = None
|
||||
else:
|
||||
last_claim_dt = None
|
||||
|
||||
if not last_claim_dt or (now - last_claim_dt).total_seconds() >= 86400:
|
||||
if not last_claim_dt or (now - last_claim_dt) >= timedelta(days=1):
|
||||
daily_reward = randint(200, 1000)
|
||||
await update_money(ctx.author, daily_reward)
|
||||
# Save the new timestamp as a float (UNIX time)
|
||||
await update_money(ctx.author, wallet=daily_reward)
|
||||
await update_daily_timestamp(ctx.author, now)
|
||||
await ctx.reply(
|
||||
f"Your daily pocket money is {daily_reward:,}<:flooney:1194943899765051473>",
|
||||
|
||||
Reference in New Issue
Block a user