diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py index de49e59..4d28ccf 100644 --- a/framework/isobot/currency.py +++ b/framework/isobot/currency.py @@ -37,8 +37,19 @@ 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 log(self, data: str): @@ -50,15 +61,15 @@ def log(self, data: str): 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) self.save(currency) self.log(f"User({user}): Added {amount} coins to wallet") 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) self.save(currency) self.log(f"User({user}): Added {amount} coins to bank") @@ -66,17 +77,17 @@ def bank_add(self, user: discord.User, amount: int) -> int: 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) self.save(currency) self.log(f"User({user}): Removed {amount} coins from wallet") 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() @@ -85,10 +96,10 @@ def bank_remove(self, user: discord.User, amount: int) -> int: 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') @@ -98,10 +109,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') @@ -111,29 +122,31 @@ 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() self.log(f"") 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() self.log(f"") 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) self.save(currency) self.log(f"Treasury: Removed {amount} coins from treasury") @@ -141,44 +154,46 @@ def treasury_remove(self, amount: int) -> int: 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: