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.
AutoZoomTelegram/modules/functions_bot.py

180 lines
5.9 KiB
Python
Raw Normal View History

2022-09-14 14:01:31 +03:00
# -*- coding: utf-8 -*-
2022-09-15 01:16:38 +03:00
"""Essential set of functions needed for discord/telegram bots and other types of apps"""
2022-09-15 13:30:25 +03:00
from typing import Union
2022-09-14 14:01:31 +03:00
from modules.functions import jsonLoad, jsonSave
2022-09-15 13:45:29 +03:00
from os import sep, listdir
2022-09-14 14:01:31 +03:00
2022-09-15 13:30:25 +03:00
def configGet(key: str, *args: str) -> any:
2022-09-15 01:16:38 +03:00
"""Get value of the config key
2022-09-15 13:30:25 +03:00
### Args:
2022-09-15 01:16:38 +03:00
* key (str): The last key of the keys path.
* *args (str): Path to key like: dict[args][key].
2022-09-15 13:30:25 +03:00
### Returns:
2022-09-15 01:16:38 +03:00
* any: Value of provided key
"""
this_dict = jsonLoad("config.json")
this_key = this_dict
2022-09-14 14:01:31 +03:00
for dict_key in args:
2022-09-15 01:16:38 +03:00
this_key = this_key[dict_key]
return this_key[key]
2022-09-14 14:01:31 +03:00
2022-09-15 13:45:29 +03:00
2022-09-15 13:55:33 +03:00
def configAppend(key: str, value: Union[str, float, int, bool, dict, list, None], *args: str) -> None:
2022-09-15 01:16:38 +03:00
"""Set key to a value
2022-09-15 13:30:25 +03:00
### Args:
2022-09-15 01:16:38 +03:00
* key (str): The last key of the keys path.
2022-09-15 13:30:25 +03:00
* value (Union[str, float, int, bool, dict, list, NoneType]): Some needed value.
2022-09-15 01:16:38 +03:00
* *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 14:01:31 +03:00
2022-09-15 13:45:29 +03:00
2022-09-15 13:55:33 +03:00
def configRemove(key: str, value: Union[str, float, int, bool, dict, list, None]) -> None:
2022-09-15 13:30:25 +03:00
"""Remove value from config's list key
### Args:
* key (str): The last key of the keys path.
* value (Union[str, float, int, bool, dict, list, NoneType]): Some needed value.
"""
2022-09-14 14:01:31 +03:00
config = jsonLoad("config.json")
config[key].remove(value)
jsonSave("config.json", config)
2022-09-15 13:30:25 +03:00
def locale(key: str, *args: str, locale=configGet("locale")) -> str:
2022-09-15 01:16:38 +03:00
"""Get value of locale string
2022-09-15 13:30:25 +03:00
### Args:
2022-09-15 01:16:38 +03:00
* 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.
2022-09-15 13:30:25 +03:00
### Returns:
2022-09-15 01:16:38 +03:00
* any: Value of provided locale key
"""
if (locale == None):
locale = configGet("locale")
2022-09-15 13:45:29 +03:00
locales = configGet("locales")
2022-09-15 01:16:38 +03:00
try:
2022-09-15 13:45:29 +03:00
this_dict = jsonLoad(f'{locales}{sep}{locale}.json')
2022-09-15 01:16:38 +03:00
except FileNotFoundError:
try:
2022-09-15 13:45:29 +03:00
this_dict = jsonLoad(f'{locales}{sep}{configGet("locale")}.json')
2022-09-15 01:16:38 +03:00
except FileNotFoundError:
2022-09-15 13:30:25 +03:00
return f'⚠ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
2022-09-15 01:16:38 +03:00
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
try:
return this_key[key]
except KeyError:
2022-09-15 13:30:25 +03:00
return f'⚠ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
2022-09-15 01:16:38 +03:00
2022-09-15 13:55:33 +03:00
def userSet(userid: Union[str, int], key: str, value: Union[str, float, int, bool, dict, list, None]) -> None:
2022-09-15 13:30:25 +03:00
"""Set user's variable
### Args:
* userid (Union[str, int]): ID of a user.
* key (str): Key of a user's variable.
* value (Union[str, float, int, bool, dict, list, NoneType]): Some needed value.
2022-09-15 13:45:29 +03:00
"""
data = configGet("data")
user = jsonLoad(f"{data}{sep}users{sep}{userid}.json")
2022-09-14 14:01:31 +03:00
user[key] = value
2022-09-15 13:45:29 +03:00
jsonSave(f"{data}{sep}users{sep}{userid}.json", user)
2022-09-14 14:01:31 +03:00
2022-09-15 13:30:25 +03:00
def userGet(userid: Union[str, int], key: str) -> any:
"""Get user's variable
### Args:
* userid (Union[str, int]): ID of a user.
* key (str): Key of a user's variable.
### Returns:
* any: Value of requested key or None
2022-09-15 13:45:29 +03:00
"""
data = configGet("data")
2022-09-14 14:01:31 +03:00
try:
2022-09-15 13:45:29 +03:00
return jsonLoad(f"{data}{sep}users{sep}{userid}.json")[key]
2022-09-14 14:01:31 +03:00
except KeyError:
return None
except FileNotFoundError:
return None
2022-09-15 13:45:29 +03:00
2022-09-15 13:30:25 +03:00
def userClear(userid: Union[str, int], key: str) -> None:
"""Clear user's variable
### Args:
* userid (Union[str, int]): ID of a user.
* key (str): Key of a user's variable.
2022-09-15 13:45:29 +03:00
"""
data = configGet("data")
2022-09-14 14:01:31 +03:00
try:
2022-09-15 13:45:29 +03:00
user = jsonLoad(f"{data}{sep}users{sep}{userid}.json")
2022-09-14 14:01:31 +03:00
del user[key]
2022-09-15 13:45:29 +03:00
jsonSave(f"{data}{sep}users{sep}{userid}.json", user)
2022-09-14 14:01:31 +03:00
except KeyError:
2022-09-15 13:45:29 +03:00
pass
2022-09-15 13:55:33 +03:00
def localeName(command: str, option: Union[str, None]):
2022-09-15 13:45:29 +03:00
"""Get name of a command or command's option
### Args:
* command (str): Command that is set in locale file
* option (Union[str, NoneType]): Option's name or None (if command's name requested)
### Returns:
* str: Name of a command or an option
"""
output = {}
locales = configGet("locales")
for entry in listdir(locales):
if entry.endswith(".json"):
if entry.replace(".json", "") != configGet("locale"):
all_commands = locale("cmd", locale=entry.replace(".json", ""))
if option != None:
output[entry.replace(".json", "")] = all_commands[command]["options"][option]["name"]
else:
output[entry.replace(".json", "")] = all_commands[command]["name"]
return output
2022-09-15 13:55:33 +03:00
def localeDescription(command: str, option: Union[str, None]):
2022-09-15 13:45:29 +03:00
"""Get description of a command or command's option
### Args:
* command (str): Command that is set in locale file
* option (Union[str, NoneType]): Option's name or None (if command's description requested)
### Returns:
* str: Description of a command or an option
"""
output = {}
locales = configGet("locales")
for entry in listdir(locales):
if entry.endswith(".json"):
if entry.replace(".json", "") != configGet("locale"):
all_commands = locale("cmd", locale=entry.replace(".json", ""))
if option != None:
output[entry.replace(".json", "")] = all_commands[command]["options"][option]["description"]
else:
output[entry.replace(".json", "")] = all_commands[command]["description"]
return output