-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from PyBotDevs/user-data-library
Migrate UserData system completely to a framework module
- Loading branch information
Showing
3 changed files
with
60 additions
and
32 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
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,42 @@ | ||
"""The framework module library used for managing general user data.""" | ||
|
||
# Imports | ||
import json | ||
|
||
# Functions | ||
class UserData(): | ||
"""Used to initialize the UserData system.""" | ||
def __init__(self): | ||
print("[framework/db/UserData] UserData library initialized.") | ||
|
||
def load(self) -> dict: | ||
"""Fetches and returns the latest data from the levelling database.""" | ||
with open("database/user_data.json", 'r', encoding="utf8") as f: db = json.load(f) | ||
return db | ||
|
||
def save(self, data: dict) -> int: | ||
"""Dumps all cached data to your local machine.""" | ||
with open("database/user_data.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4) | ||
return 0 | ||
|
||
def generate(self, user_id: int) -> int: | ||
"""Generates a new data key for the specified user.\n | ||
Returns `0` if the request was successful, returns `1` if the data key already exists.""" | ||
userdat = self.load() | ||
if str(user_id) not in userdat.keys(): | ||
userdat[str(user_id)] = {"work_job": None} | ||
self.save(userdat) | ||
return 0 | ||
else: return 1 | ||
|
||
def fetch(self, user_id: int, key: str) -> str: | ||
"""Fetches the vakue of a data key, from a specific user.""" | ||
userdat = self.load() | ||
return userdat[str(user_id)][key] | ||
|
||
def set(self, user_id: int, key: str, value) -> int: | ||
"""Sets a new value for a data key, for a specific user.""" | ||
userdat = self.load() | ||
userdat[str(user_id)][key] = value | ||
self.save(userdat) | ||
return 0 |
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