Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 54 additions & 39 deletions framework/isobot/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,56 +37,67 @@ def __init__(self, db_path: str, log_path: str):
self.db_path = db_path
self.log_path = log_path
print(f"[Framework/Loader] {Colors.green}CurrencyAPI initialized.{Colors.end}")

def get_time(self):

def save(self, data: dict) -> int:
"""Dumps all cached data to your local machine."""
with open(self.db_path, 'w+', encoding="utf8") as f: json.dump(data, f)
return 0

def load(self) -> dict:
"""Loads the database from your local machine onto memory."""
with open(self.db_path, 'r', encoding="utf-8") as f: data = json.load(f)
return data

def get_time(self) -> str:
"""Returns the current time in a formatted state."""
return datetime.datetime.now().strftime("%H:%M:%S")

def add(self, user: discord.User, amount: int) -> int:
"""Adds balance to the specified user."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["wallet"][str(user)] += int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Added {amount} coins to wallet\n')
f.close()
self.save(currency)
return 0

def bank_add(self, user: discord.User, amount: int) -> int:
"""Adds balance to the specified user's bank account."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["bank"][str(user)] += int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Added {amount} coins to bank\n')
f.close()
self.save(currency)
return 0

def remove(self, user: discord.User, amount: int) -> int:
"""Removes balance from the specified user."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["wallet"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Removed {amount} coins from wallet\n')
f.close()
self.save(currency)
return 0

def bank_remove(self, user: discord.User, amount: int) -> int:
"""Removes balance from the specified user's bank account."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["bank"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
self.save(currency)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Removed {amount} coins from bank\n')
f.close()
return 0

def reset(self, user: discord.User) -> int:
"""Resets the specified user's balance."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["wallet"][str(user)] = 0
currency["bank"][str(user)] = 0
with open(self.db_path, 'w+') as f: json.dump(currency, f)
self.save(currency)
print(f"[Framework/CurrencyAPI] Currency data for \"{user}\" has been wiped")
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Wiped all currency data\n')
Expand All @@ -95,10 +106,10 @@ def reset(self, user: discord.User) -> int:

def deposit(self, user: discord.User, amount: int) -> int:
"""Moves a specified amount of coins to the user's bank."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["bank"][str(user)] += int(amount)
currency["wallet"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
self.save(currency)
print(f"[Framework/CurrencyAPI] Moved {amount} coins to bank. User: {user} [{user}]")
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Moved {amount} coins from wallet to bank\n')
Expand All @@ -107,74 +118,78 @@ def deposit(self, user: discord.User, amount: int) -> int:

def withdraw(self, user: discord.User, amount: int) -> int:
"""Moves a specified amount of coins to the user's wallet."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
currency["wallet"][str(user)] += int(amount)
currency["bank"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
self.save(currency)
print(f"[Framework/CurrencyAPI] Moved {amount} coins to wallet. User: {user} [{user}]")
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Moved {amount} coins from bank to wallet\n')
f.close()
return 0

def treasury_add(self, amount: int) -> int:
with open(self.db_path, 'r') as f: currency = json.load(f)
"""Adds a specified amount of coins to the treasury."""
currency = self.load()
currency["treasury"] += int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
self.save(currency)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency Treasury: Added {amount} coins to treasury\n')
f.close()
return 0

def treasury_remove(self, amount: int) -> int:
with open(self.db_path, 'r') as f: currency = json.load(f)
"""Removes a specified amount of coins from the treasury."""
currency = self.load()
currency["treasury"] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency Treasury: Removed {amount} coins from treasury\n')
f.close()
self.save(currency)
return 0

def get_wallet(self, user: discord.User) -> int:
"""Returns the amount of coins in the user's wallet."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
return int(currency["wallet"][str(user)])

def get_bank(self, user: discord.User) -> int:
"""Returns the amount of coins in the user's bank account."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
return int(currency["bank"][str(user)])

def get_treasury(self) -> int:
"""Returns the amount of coins in the treasury."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
return int(currency["treasury"])

def get_user_networth(self, user: discord.User) -> int:
"""Returns the net-worth of the user."""
with open(self.db_path, 'r') as f: currency = json.load(f)
nw = int(currency["wallet"][str(user)]) + int(currency["bank"][str(user)])
currency = self.load()
nw = int(currency["wallet"][str(user)]) + int(currency["bank"][str(user)])
return nw

def get_user_count(self) -> int:
"""Returns the total number of users cached in the currency database."""
with open(self.db_path, 'r') as f: currency = json.load(f)
currency = self.load()
users = 0
for x in currency["wallet"].keys(): users += 1
return users

def new_wallet(self, user: int) -> int:
"""Makes a new key for a user wallet in the currency database."""
with open(self.db_path, 'r') as f: currency = json.load(f)
if str(id) not in currency['wallet']:
currency['wallet'][str(id)] = 5000
currency = self.load()
if str(user) not in currency['wallet']:
currency['wallet'][str(user)] = 5000
self.save(currency)
return 0

def new_bank(self, user: int) -> int:
"""Makes a new key for a user bank account in the currency database."""
with open(self.db_path, 'r') as f: currency = json.load(f)
if str(id) not in currency['bank']:
currency['bank'][str(id)] = 0
currency = self.load()
if str(user) not in currency['bank']:
currency['bank'][str(user)] = 0
self.save(currency)
return 0

def delete_user(self, user: int) -> int:
Expand Down