-
-
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.
Merge pull request #310 from cyanogus/weather-db-library
Migrate weather db commands from weather cog to the framework
- Loading branch information
Showing
2 changed files
with
66 additions
and
19 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,53 @@ | ||
# Imports | ||
import json | ||
from discord import User | ||
|
||
# Classes | ||
class Colors: | ||
"""Contains general stdout colors.""" | ||
cyan = '\033[96m' | ||
red = '\033[91m' | ||
green = '\033[92m' | ||
end = '\033[0m' | ||
|
||
# Functions | ||
class Weather(Colors): | ||
"""Class to manage the weather db""" | ||
def __init__(self): | ||
print(f"[Framework/Loader] {Colors.green}Weather database initialized.{Colors.end}") | ||
|
||
def load(self) -> dict: | ||
"""Fetches and returns the latest data from the levelling database.""" | ||
with open("database/weather.json", 'r', encoding="utf-8") 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/weather.json", 'w+', encoding="utf-8") as f: json.dump(data, f, indent=4) | ||
return 0 | ||
|
||
def new(self, user_id: User): | ||
user_db = self.load() | ||
if str(user_id) not in user_db: user_db[str(user_id)] = {"location": None, "scale": "Celsius"} | ||
self.save(user_db) | ||
return 0 | ||
|
||
def set_scale(self, user_id: User, scale: str) -> int: | ||
user_db = self.load() | ||
user_db[str(user_id)]["scale"] = scale | ||
self.save(user_db) | ||
return 0 | ||
|
||
def set_default_location(self, user_id: User, location: str) -> int: | ||
user_db = self.load() | ||
user_db[str(user_id)]["location"] = location | ||
self.save(user_db) | ||
return 0 | ||
|
||
def get_scale(self, user_id: User): | ||
user_db = self.load() | ||
return user_db[str(user_id)]["scale"] | ||
|
||
def get_default_location(self, user_id: User): | ||
user_db = self.load() | ||
return user_db[str(user_id)]["location"] |