This repository has been archived on 2024-10-14. You can view files and clone it, but cannot push or open issues or pull requests.
AutoZoomDiscord/modules/functions_bot.py

98 lines
3.1 KiB
Python
Raw Normal View History

2022-09-14 14:02:06 +03:00
# -*- coding: utf-8 -*-
2022-09-15 01:20:38 +03:00
"""Essential set of functions needed for discord/telegram bots and other types of apps"""
2022-09-14 13:48:58 +03:00
from modules.functions import jsonLoad, jsonSave
2022-09-15 01:20:38 +03:00
from os import sep
2022-09-14 13:48:58 +03:00
2022-09-15 01:20:38 +03:00
def configGet(key: str, *args: str):
"""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].
Returns:
* any: Value of provided key
"""
this_dict = jsonLoad("config.json")
this_key = this_dict
2022-09-14 13:48:58 +03:00
for dict_key in args:
2022-09-15 01:20:38 +03:00
this_key = this_key[dict_key]
return this_key[key]
2022-09-14 13:48:58 +03:00
2022-09-15 01:20:38 +03:00
def configAppend(key: str, value, *args: str):
"""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].
"""
this_dict = jsonLoad("config.json")
string = "this_dict"
for arg in args:
string += f'["{arg}"]'
if type(value) in [str]:
string += f'["{key}"].append("{value}")'
else:
string += f'["{key}"].append({value})'
exec(string)
jsonSave(this_dict, "config.json")
return
2022-09-14 13:48:58 +03:00
def configRemove(key, value):
config = jsonLoad("config.json")
config[key].remove(value)
jsonSave("config.json", config)
2022-09-15 01:20:38 +03:00
def locale(key: str, *args: str, locale=configGet("locale")):
"""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")
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:
try:
this_dict = jsonLoad(f'{configGet("locale_fallback", "locations")}{sep}{configGet("locale")}.json')
except:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
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-09-14 13:48:58 +03:00
def userSet(userid, key, value):
2022-09-15 01:20:38 +03:00
user = jsonLoad(f"data{sep}users{sep}{userid}.json")
2022-09-14 13:48:58 +03:00
user[key] = value
2022-09-15 01:20:38 +03:00
jsonSave(f"data{sep}users{sep}{userid}.json", user)
2022-09-14 13:48:58 +03:00
def userGet(userid, key):
try:
2022-09-15 01:20:38 +03:00
return jsonLoad(f"data{sep}users{sep}{userid}.json")[key]
2022-09-14 13:48:58 +03:00
except KeyError:
return None
except FileNotFoundError:
return None
def userClear(userid, key):
try:
2022-09-15 01:20:38 +03:00
user = jsonLoad(f"data{sep}users{sep}{userid}.json")
2022-09-14 13:48:58 +03:00
del user[key]
2022-09-15 01:20:38 +03:00
jsonSave(f"data{sep}users{sep}{userid}.json", user)
2022-09-14 13:48:58 +03:00
except KeyError:
pass