2022-10-20 13:24:32 +03:00
|
|
|
from typing import Union
|
2022-10-17 00:30:07 +03:00
|
|
|
from ujson import JSONDecodeError as JSONDecodeError
|
|
|
|
from ujson import loads, dumps
|
|
|
|
|
|
|
|
from sys import exit
|
2022-10-20 13:24:32 +03:00
|
|
|
from os import kill, sep
|
2022-10-17 00:30:07 +03:00
|
|
|
from os import name as osname
|
|
|
|
from traceback import print_exc
|
|
|
|
|
|
|
|
from modules.logging import logWrite
|
|
|
|
|
|
|
|
def jsonLoad(filename):
|
|
|
|
"""Loads arg1 as json and returns its contents"""
|
|
|
|
with open(filename, "r", encoding='utf8') as file:
|
|
|
|
try:
|
|
|
|
output = loads(file.read())
|
|
|
|
except JSONDecodeError:
|
|
|
|
logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{print_exc()}")
|
|
|
|
raise
|
|
|
|
except FileNotFoundError:
|
|
|
|
logWrite(f"Could not load json file {filename}: file does not seem to exist!\n{print_exc()}")
|
|
|
|
raise
|
|
|
|
return output
|
|
|
|
|
|
|
|
def jsonSave(contents, filename):
|
|
|
|
"""Dumps dict/list arg1 to file arg2"""
|
|
|
|
try:
|
|
|
|
with open(filename, "w", encoding='utf8') as file:
|
|
|
|
file.write(dumps(contents, ensure_ascii=False, indent=4))
|
|
|
|
except Exception as exp:
|
|
|
|
logWrite(f"Could not save json file {filename}: {exp}\n{print_exc()}")
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
def configSet(key: str, value, *args: str, file: str = "config"):
|
2022-10-17 00:30:07 +03:00
|
|
|
"""Set key to a value
|
|
|
|
Args:
|
|
|
|
* key (str): The last key of the keys path.
|
|
|
|
* value (str/int/float/list/dict/None): Some needed value.
|
|
|
|
* *args (str): Path to key like: dict[args][key].
|
2022-10-20 13:24:32 +03:00
|
|
|
* file (str): User ID to save. Saved to config if not provided. Defaults to "config".
|
2022-10-17 00:30:07 +03:00
|
|
|
"""
|
2022-10-20 13:24:32 +03:00
|
|
|
if file == "config":
|
|
|
|
filepath = ""
|
2022-11-13 14:40:49 +02:00
|
|
|
this_dict = jsonLoad(f"{filepath}{file}.json")
|
|
|
|
if this_dict["debug"] is True:
|
2022-10-26 15:54:55 +03:00
|
|
|
try:
|
|
|
|
this_dict = jsonLoad("config_debug.json")
|
|
|
|
file = "config_debug"
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Debug mode is set but config_debug.json is not there! Falling back to config.json", flush=True)
|
2022-10-20 13:24:32 +03:00
|
|
|
else:
|
|
|
|
filepath = f"data{sep}users{sep}"
|
2022-11-13 14:40:49 +02:00
|
|
|
this_dict = jsonLoad(f"{filepath}{file}.json")
|
2022-10-17 00:30:07 +03:00
|
|
|
string = "this_dict"
|
|
|
|
for arg in args:
|
|
|
|
string += f'["{arg}"]'
|
|
|
|
if type(value) in [str]:
|
|
|
|
string += f'["{key}"] = "{value}"'
|
|
|
|
else:
|
|
|
|
string += f'["{key}"] = {value}'
|
|
|
|
exec(string)
|
2022-10-20 13:24:32 +03:00
|
|
|
jsonSave(this_dict, f"{filepath}{file}.json")
|
2022-10-17 00:30:07 +03:00
|
|
|
return
|
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
def configGet(key: str, *args: str, file: str = "config"):
|
2022-10-17 00:30:07 +03:00
|
|
|
"""Get value of the config key
|
|
|
|
Args:
|
|
|
|
* key (str): The last key of the keys path.
|
|
|
|
* *args (str): Path to key like: dict[args][key].
|
2022-10-20 13:24:32 +03:00
|
|
|
* file (str): User ID to load. Loads config if not provided. Defaults to "config".
|
2022-10-17 00:30:07 +03:00
|
|
|
Returns:
|
|
|
|
* any: Value of provided key
|
|
|
|
"""
|
2022-10-20 13:24:32 +03:00
|
|
|
if file == "config":
|
2022-10-23 13:13:33 +03:00
|
|
|
try:
|
|
|
|
this_dict = jsonLoad("config.json")
|
|
|
|
except FileNotFoundError:
|
2022-10-26 14:30:24 +03:00
|
|
|
print("Config file not found! Copy config_example.json to config.json, configure it and rerun the bot!", flush=True)
|
2022-10-23 13:13:33 +03:00
|
|
|
exit()
|
2022-11-13 14:40:49 +02:00
|
|
|
if this_dict["debug"] is True:
|
2022-10-26 15:54:55 +03:00
|
|
|
try:
|
|
|
|
this_dict = jsonLoad("config_debug.json")
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Debug mode is set but config_debug.json is not there! Falling back to config.json", flush=True)
|
2022-10-20 13:24:32 +03:00
|
|
|
else:
|
|
|
|
this_dict = jsonLoad(f"data{sep}users{sep}{file}.json")
|
2022-10-17 00:30:07 +03:00
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
return this_key[key]
|
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
def locale(key: str, *args: str, locale=configGet("locale")) -> Union[str, list, dict]:
|
|
|
|
"""Get value of locale string
|
|
|
|
Args:
|
|
|
|
* key (str): The last key of the locale's keys path.
|
|
|
|
* *args (list): Path to key like: dict[args][key].
|
|
|
|
* locale (str): Locale to looked up in. Defaults to config's locale value.
|
|
|
|
Returns:
|
|
|
|
* any: Value of provided locale key
|
|
|
|
"""
|
|
|
|
if (locale == None):
|
|
|
|
locale = configGet("locale")
|
2022-10-17 00:30:07 +03:00
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
try:
|
|
|
|
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
|
|
|
|
except FileNotFoundError:
|
|
|
|
try:
|
|
|
|
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{configGet("locale")}.json')
|
|
|
|
except FileNotFoundError:
|
|
|
|
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
|
2022-10-17 00:30:07 +03:00
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
2022-10-17 00:30:07 +03:00
|
|
|
|
2022-10-20 13:24:32 +03:00
|
|
|
try:
|
|
|
|
return this_key[key]
|
|
|
|
except KeyError:
|
|
|
|
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
|
2022-10-17 00:30:07 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
from psutil import Process
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
# print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
|
|
|
|
print("Missing dependencies! Please install all needed dependencies and run the bot again!")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
def killProc(pid):
|
|
|
|
if osname == "posix":
|
|
|
|
from signal import SIGKILL # type: ignore
|
|
|
|
kill(pid, SIGKILL)
|
|
|
|
else:
|
|
|
|
p = Process(pid)
|
|
|
|
p.kill()
|