Started a complete refactor
This commit is contained in:
2
modules/classes/holo_user.py
Normal file
2
modules/classes/holo_user.py
Normal file
@@ -0,0 +1,2 @@
|
||||
class HoloUser():
|
||||
pass
|
36
modules/database.py
Normal file
36
modules/database.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from pymongo import MongoClient
|
||||
from ujson import loads
|
||||
|
||||
with open("config.json", "r", encoding="utf-8") as f:
|
||||
db_config = loads(f.read())["database"]
|
||||
f.close()
|
||||
|
||||
if db_config["user"] is not None and db_config["password"] is not None:
|
||||
con_string = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(
|
||||
db_config["user"],
|
||||
db_config["password"],
|
||||
db_config["host"],
|
||||
db_config["port"],
|
||||
db_config["name"]
|
||||
)
|
||||
else:
|
||||
con_string = 'mongodb://{0}:{1}/{2}'.format(
|
||||
db_config["host"],
|
||||
db_config["port"],
|
||||
db_config["name"]
|
||||
)
|
||||
|
||||
db_client = MongoClient(con_string)
|
||||
db = db_client.get_database(name=db_config["name"])
|
||||
|
||||
collections = db.list_collection_names()
|
||||
|
||||
for collection in ["users", "context", "messages", "warnings", "subscriptions"]:
|
||||
if not collection in collections:
|
||||
db.create_collection(collection)
|
||||
|
||||
col_users = db.get_collection("users")
|
||||
col_context = db.get_collection("context")
|
||||
col_messages = db.get_collection("messages")
|
||||
col_warnings = db.get_collection("warnings")
|
||||
col_subscriptions = db.get_collection("subscriptions")
|
@@ -1,5 +1,6 @@
|
||||
from typing import Any, Union
|
||||
from pyrogram.enums.chat_type import ChatType
|
||||
from pyrogram.types import User
|
||||
from ujson import JSONDecodeError as JSONDecodeError
|
||||
from ujson import loads, dumps
|
||||
|
||||
@@ -49,11 +50,11 @@ def nested_set(dic, keys, value, create_missing=True):
|
||||
def configSet(keys: list, value: Any, file: str = "config", create_missing=True):
|
||||
"""Set config's value to provided one
|
||||
|
||||
Args:
|
||||
* keys (list): List of keys from the highest one to target
|
||||
* value (Any): Needed value
|
||||
* file (str, optional): File (if not config). Defaults to "config".
|
||||
* create_missing (bool, optional): Create missing items on the way. Defaults to True.
|
||||
### Args:
|
||||
* keys (`list`): List of keys from the highest one to target
|
||||
* value (`Any`): Needed value
|
||||
* file (`str`, optional): File (if not config). Defaults to "config".
|
||||
* create_missing (`bool`, optional): Create missing items on the way. Defaults to True.
|
||||
"""
|
||||
if file == "config":
|
||||
filepath = ""
|
||||
@@ -74,11 +75,11 @@ def configSet(keys: list, value: Any, file: str = "config", create_missing=True)
|
||||
|
||||
def configGet(key: str, *args: str, file: str = "config"):
|
||||
"""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].
|
||||
* file (str): User ID to load. Loads config if not provided. Defaults to "config".
|
||||
Returns:
|
||||
### Args:
|
||||
* key (`str`): The last key of the keys path.
|
||||
* *args (`str`): Path to key like: dict[args][key].
|
||||
* file (`str`): User ID to load. Loads config if not provided. Defaults to "config".
|
||||
### Returns:
|
||||
* any: Value of provided key
|
||||
"""
|
||||
if file == "config":
|
||||
@@ -99,16 +100,19 @@ def configGet(key: str, *args: str, file: str = "config"):
|
||||
this_key = this_key[dict_key]
|
||||
return this_key[key]
|
||||
|
||||
def locale(key: str, *args: str, locale=configGet("locale")) -> Union[str, list, dict]:
|
||||
def locale(key: str, *args: str, locale: Union[str, User] = configGet("locale")) -> Any:
|
||||
"""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
|
||||
### Args:
|
||||
* key (`str`): The last key of the locale's keys path.
|
||||
* *args (`list`): Path to key like: dict[args][key].
|
||||
* locale (`Union[str, User]`): Locale to looked up in. Provide User to get his `.language_code`. Defaults to config's locale value.
|
||||
### Returns:
|
||||
* any: Value of provided locale key. In normal case must be `str`, `dict` or `list`.
|
||||
"""
|
||||
if (locale == None):
|
||||
if isinstance(locale, User):
|
||||
locale = locale.language_code
|
||||
|
||||
if locale is None:
|
||||
locale = configGet("locale")
|
||||
|
||||
try:
|
||||
|
Reference in New Issue
Block a user