From 3ec3d762fcd574bf958b8451619db69a0cf7f870 Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Sat, 10 Dec 2022 11:37:15 +0100 Subject: [PATCH] Started a complete refactor --- .gitignore | 3 ++- config_example.json | 8 +++++++- data/.gitkeep | 0 data/sponsor_default.json | 9 -------- data/sponsors/.gitkeep | 0 data/user_default.json | 25 ---------------------- data/users/.gitkeep | 0 main.py | 16 --------------- modules/classes/holo_user.py | 2 ++ modules/database.py | 36 ++++++++++++++++++++++++++++++++ modules/utils.py | 40 ++++++++++++++++++++---------------- 11 files changed, 69 insertions(+), 70 deletions(-) delete mode 100644 data/.gitkeep delete mode 100644 data/sponsor_default.json delete mode 100644 data/sponsors/.gitkeep delete mode 100644 data/user_default.json delete mode 100644 data/users/.gitkeep create mode 100644 modules/classes/holo_user.py create mode 100644 modules/database.py diff --git a/.gitignore b/.gitignore index 39c4532..bcc8d4c 100644 --- a/.gitignore +++ b/.gitignore @@ -163,4 +163,5 @@ TASK.md inline_bot.py data/applications.json !data/cache/avatars/.gitkeep -data/cache/avatars/* \ No newline at end of file +data/cache/avatars/* +.vscode \ No newline at end of file diff --git a/config_example.json b/config_example.json index 785a6a4..95dcce2 100644 --- a/config_example.json +++ b/config_example.json @@ -17,12 +17,18 @@ "api_hash": "", "bot_token": "" }, + "database": { + "user": null, + "password": null, + "host": "127.0.0.1", + "port": 27017, + "name": "holochecker" + }, "logging": { "size": 512, "location": "logs" }, "locations": { - "data": "data", "cache": "cache", "locale": "locale" }, diff --git a/data/.gitkeep b/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/data/sponsor_default.json b/data/sponsor_default.json deleted file mode 100644 index 1d13728..0000000 --- a/data/sponsor_default.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "filling": false, - "applied": false, - "approved": false, - "stage": 0, - "paid": null, - "expires": null, - "nickname": null -} \ No newline at end of file diff --git a/data/sponsors/.gitkeep b/data/sponsors/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/data/user_default.json b/data/user_default.json deleted file mode 100644 index d4e522c..0000000 --- a/data/user_default.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "stage": 0, - "reapply": false, - "link": null, - "sent": false, - "confirmed": false, - "approved": false, - "refused": false, - "telegram_id": null, - "telegram_name": null, - "telegram_phone": null, - "telegram_locale": null, - "application": { - "1": null, - "2": null, - "3": null, - "4": null, - "5": null, - "6": null, - "7": null, - "8": null, - "9": null, - "10": null - } -} \ No newline at end of file diff --git a/data/users/.gitkeep b/data/users/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/main.py b/main.py index 14da1f9..a2c161f 100644 --- a/main.py +++ b/main.py @@ -14,22 +14,6 @@ from pyrogram import idle pid = getpid() -for entry in [f"{configGet('data', 'locations')}{sep}applications.json", f"{configGet('data', 'locations')}{sep}warnings.json"]: - mode = 'r' if path.exists(entry) else 'w' - with open(entry, mode) as f: - try: - f.write("{}") - except: - pass - -for entry in [f"{configGet('data', 'locations')}{sep}messages.json"]: - mode = 'r' if path.exists(entry) else 'w' - with open(entry, mode) as f: - try: - f.write("[]") - except: - pass - # Importing from modules.commands.application import * from modules.commands.applications import * diff --git a/modules/classes/holo_user.py b/modules/classes/holo_user.py new file mode 100644 index 0000000..308f291 --- /dev/null +++ b/modules/classes/holo_user.py @@ -0,0 +1,2 @@ +class HoloUser(): + pass \ No newline at end of file diff --git a/modules/database.py b/modules/database.py new file mode 100644 index 0000000..e42cc89 --- /dev/null +++ b/modules/database.py @@ -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") \ No newline at end of file diff --git a/modules/utils.py b/modules/utils.py index cebdded..685ad2c 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -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: