Started a complete refactor

This commit is contained in:
Profitroll 2022-12-10 11:37:15 +01:00
parent 654daed8c2
commit 3ec3d762fc
11 changed files with 69 additions and 70 deletions

3
.gitignore vendored
View File

@ -163,4 +163,5 @@ TASK.md
inline_bot.py
data/applications.json
!data/cache/avatars/.gitkeep
data/cache/avatars/*
data/cache/avatars/*
.vscode

View File

@ -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"
},

View File

View File

@ -1,9 +0,0 @@
{
"filling": false,
"applied": false,
"approved": false,
"stage": 0,
"paid": null,
"expires": null,
"nickname": null
}

View File

View File

@ -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
}
}

View File

16
main.py
View File

@ -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 *

View File

@ -0,0 +1,2 @@
class HoloUser():
pass

36
modules/database.py Normal file
View 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")

View File

@ -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: