-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import logging | ||
from datetime import datetime | ||
from typing import Union | ||
|
||
import dataset | ||
import discord | ||
|
||
from tools import database | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Bank: | ||
def __init__(self, user: Union[discord.User, discord.Member, discord.ClientUser]): | ||
# Note: discord.ClientUser is the bot itself. | ||
if not isinstance(user, (discord.User, discord.Member, discord.ClientUser)): | ||
raise TypeError( | ||
f"Object given was not a User nor Member object.\n\tType given: {type(user)}" | ||
) | ||
self.user = user | ||
self.balance = self.__balance__() | ||
|
||
def __str__(self) -> str: | ||
"""<name>'s balance is $<balance>.""" | ||
|
||
return f"{self.user.name}'s balance is **$`{self.balance}`**." | ||
|
||
def __format__(self, format_spec: str) -> str: | ||
"""Returns balance's value as to the specified format.""" | ||
|
||
return format(self.balance, format_spec) | ||
|
||
def __get__(self) -> float: | ||
"""Returns balance's value as a float.""" | ||
|
||
return float(self.balance) | ||
|
||
def __balance__(self) -> float: | ||
"""Get the balance of a user.""" | ||
|
||
with dataset.connect(database.get_db()) as db: | ||
# Find last bank transaction. | ||
statement = statement = f""" | ||
SELECT opening_balance, transaction_amount | ||
FROM bank | ||
WHERE author_id = {self.user.id} | ||
ORDER BY timestamp | ||
LIMIT 12 | ||
""" | ||
result = db.query(statement) | ||
db.close() | ||
|
||
for row in result: | ||
balance = row["opening_balance"] + row["transaction_amount"] | ||
break | ||
else: | ||
# If there was no result for the user, default balance is given. | ||
balance = 500 | ||
|
||
return float(balance) | ||
|
||
def add(self, amount: float, reason: str = "") -> "Bank": | ||
"""Add to a user's balance.""" | ||
|
||
if amount == 0: # Pointless, do nothing. | ||
return 0 | ||
|
||
self.__record_ledger__(amount, reason) | ||
self.balance += amount | ||
return self | ||
|
||
def subtract(self, amount: float, reason: str = "") -> "Bank": | ||
"""Subtract from a user's balance.""" | ||
|
||
if amount == 0: # Pointless, do nothing. | ||
return 0 | ||
|
||
self.__record_ledger__(amount, reason) | ||
self.balance -= amount | ||
return self | ||
|
||
def set(self, amount: float, reason: str = "") -> "Bank": | ||
"""Set a user's balance.""" | ||
|
||
self.__record_ledger__(amount - self.balance, reason) # Because math | ||
self.balance = amount | ||
return self | ||
|
||
def __record_ledger__(self, amount: float, reason: str = "") -> None: | ||
with dataset.connect(database.get_db()) as db: | ||
# Find last bank transaction | ||
db["bank"].insert( | ||
dict( | ||
author_id=self.user.id, | ||
opening_balance=self.balance, | ||
transaction_amount=amount, | ||
reason=reason, | ||
timestamp=datetime.now(), | ||
) | ||
) | ||
db.commit() | ||
db.close() | ||
|
||
def name(self) -> str: | ||
"""Return bank owner's name.""" | ||
return self.user.name | ||
|
||
def id(self) -> int: | ||
"""Return bank owner's ID.""" | ||
return self.user.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters