Skip to content

Commit

Permalink
Implement logging for currency framework module
Browse files Browse the repository at this point in the history
  • Loading branch information
notsniped committed Mar 22, 2024
1 parent 747fe2f commit cb2ee02
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions framework/isobot/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,35 @@ def __init__(self, db_path: str, log_path: str):
def get_time(self):
return datetime.datetime.now().strftime("%H:%M:%S")

def log(self, data: str):
"""Logs new information to the currency log."""
with open(self.log_path, 'a', encoding="utf-8") as f:
f.write(f'{self.get_time()} framework.isobot.currency {data}\n')
f.close()
return 0

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["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)
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["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)
self.log(f"User({user}): Added {amount} coins to bank")
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["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)
self.log(f"User({user}): Removed {amount} coins from wallet")
return 0

def bank_remove(self, user: discord.User, amount: int) -> int:
Expand All @@ -79,6 +80,7 @@ def bank_remove(self, user: discord.User, amount: int) -> int:
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()
self.log(f"")
return 0

def reset(self, user: discord.User) -> int:
Expand All @@ -91,6 +93,7 @@ def reset(self, user: discord.User) -> int:
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Wiped all currency data\n')
f.close()
self.log(f"")
return 0

def deposit(self, user: discord.User, amount: int) -> int:
Expand All @@ -103,6 +106,7 @@ def deposit(self, user: discord.User, amount: int) -> int:
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')
f.close()
self.log(f"")
return 0

def withdraw(self, user: discord.User, amount: int) -> int:
Expand All @@ -115,6 +119,7 @@ def withdraw(self, user: discord.User, amount: int) -> int:
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:
Expand All @@ -124,15 +129,14 @@ def treasury_add(self, amount: int) -> int:
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)
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)
self.log(f"Treasury: Removed {amount} coins from treasury")
return 0

def get_wallet(self, user: discord.User) -> int:
Expand Down

0 comments on commit cb2ee02

Please sign in to comment.