Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove overhead caused by indenting databases #316

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions framework/isobot/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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, indent=4)
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()
Expand All @@ -54,7 +54,7 @@ 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, indent=4)
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()
Expand All @@ -64,7 +64,7 @@ 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, indent=4)
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()
Expand All @@ -74,7 +74,7 @@ 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["bank"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
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 bank\n')
f.close()
Expand All @@ -85,7 +85,7 @@ def reset(self, user: discord.User) -> int:
with open(self.db_path, 'r') as f: currency = json.load(f)
currency["wallet"][str(user)] = 0
currency["bank"][str(user)] = 0
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
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')
Expand All @@ -97,7 +97,7 @@ def deposit(self, user: discord.User, amount: int) -> int:
with open(self.db_path, 'r') as f: currency = json.load(f)
currency["bank"][str(user)] += int(amount)
currency["wallet"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
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')
Expand All @@ -109,7 +109,7 @@ def withdraw(self, user: discord.User, amount: int) -> int:
with open(self.db_path, 'r') as f: currency = json.load(f)
currency["wallet"][str(user)] += int(amount)
currency["bank"][str(user)] -= int(amount)
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
with open(self.db_path, 'w+') as f: json.dump(currency, f)
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')
Expand All @@ -119,7 +119,7 @@ def withdraw(self, user: discord.User, amount: int) -> int:
def treasury_add(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, indent=4)
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: Added {amount} coins to treasury\n')
f.close()
Expand All @@ -128,7 +128,7 @@ def treasury_add(self, amount: int) -> int:
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, indent=4)
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()
Expand Down
14 changes: 7 additions & 7 deletions framework/isobot/db/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class Automod():
"""Initializes the Automod database system."""
def __init__(self):
print("[framework/db/Automod] Automod db library initialized.")

def load(self) -> dict:
"""Fetches and returns the latest data from the items database."""
with open("database/automod.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/automod.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
with open("database/automod.json", 'w+', encoding="utf8") as f: json.dump(data, f)
return 0

def generate(self, server_id: int) -> int:
Expand All @@ -34,33 +34,33 @@ def generate(self, server_id: int) -> int:
}
}
self.save(automod_config)

def fetch_config(self, server_id: int) -> dict:
"""Fetches and returns the specified server's automod configuration.\n\nReturns in raw `dict` format."""
automod_config = self.load()
return automod_config[str(server_id)]

def swearfilter_enabled(self, server_id: int, value: bool) -> int:
"""Sets a `bool` value to define whether the server's swear-filter is enabled or not."""
automod_config = self.load()
automod_config[str(server_id)]["swear_filter"]["enabled"] = value
self.save(automod_config)
return 0

def swearfilter_usedefaultkeywords(self, server_id: int, enabled: bool) -> int:
"""Sets a `bool` value to define whether the server's swear-filter will use default keywords."""
automod_config = self.load()
automod_config[str(server_id)]["swear_filter"]["keywords"]["use_default"] = enabled
self.save(automod_config)
return 0

def swearfilter_addkeyword(self, server_id: int, keyword: str) -> int:
"""Adds a new custom keyword for the server's automod configuration."""
automod_config = self.load()
automod_config[str(server_id)]["swear_filter"]["keywords"]["custom"].append(keyword)
self.save(automod_config)
return 0

def swearfilter_removekeyword(self, server_id: int, keyword_id: int) -> int:
"""Removes a keyword (using id) from the server's automod configuration."""
automod_config = self.load()
Expand Down
2 changes: 1 addition & 1 deletion framework/isobot/db/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def load(self) -> dict:

def save(self, data: dict) -> int:
"""Dumps all cached data to your local machine."""
with open("database/items.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
with open("database/items.json", 'w+', encoding="utf8") as f: json.dump(data, f)
return 0

def generate(self, user_id: int) -> int:
Expand Down
4 changes: 2 additions & 2 deletions framework/isobot/db/levelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def load(self) -> dict:

def save(self, data: dict) -> int:
"""Dumps all cached data to your local machine."""
with open("database/levels.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
with open("database/levels.json", 'w+', encoding="utf8") as f: json.dump(data, f)
return 0

def generate(self, user_id: int) -> int:
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_xp(self, user_id: int) -> int:
"""Fetches a user's current xp."""
levels = self.load()
return levels[str(user_id)]["xp"]

def get_raw(self):
"""Fetches and returns the raw json data in the levelling database."""
levels = self.load()
Expand Down
8 changes: 4 additions & 4 deletions framework/isobot/db/userdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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)
with open("database/user_data.json", 'w+', encoding="utf8") as f: json.dump(data, f)
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."""
Expand All @@ -33,7 +33,7 @@ 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()
Expand Down
2 changes: 1 addition & 1 deletion framework/isobot/db/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def load(self) -> dict:

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)
with open("database/weather.json", 'w+', encoding="utf-8") as f: json.dump(data, f)
return 0

def new(self, user_id: User):
Expand Down
Loading