Spaces:
Sleeping
Sleeping
Captain Ezio
commited on
Commit
·
4f146ce
1
Parent(s):
bf61649
useless
Browse files- Powers/database/lang_db.py +0 -93
Powers/database/lang_db.py
DELETED
@@ -1,93 +0,0 @@
|
|
1 |
-
from threading import RLock
|
2 |
-
from time import time
|
3 |
-
|
4 |
-
from Powers import LOGGER
|
5 |
-
from Powers.database import MongoDB
|
6 |
-
|
7 |
-
INSERTION_LOCK = RLock()
|
8 |
-
|
9 |
-
# Locall cache languages for users!!
|
10 |
-
LANG_CACHE = {}
|
11 |
-
|
12 |
-
|
13 |
-
class Langs(MongoDB):
|
14 |
-
"""Class for language options in bot."""
|
15 |
-
|
16 |
-
db_name = "langs"
|
17 |
-
|
18 |
-
def __init__(self, chat_id: int) -> None:
|
19 |
-
super().__init__(self.db_name)
|
20 |
-
self.chat_id = chat_id
|
21 |
-
self.chat_info = self.__ensure_in_db()
|
22 |
-
|
23 |
-
def get_chat_type(self):
|
24 |
-
return "supergroup" if str(self.chat_id).startswith("-100") else "user"
|
25 |
-
|
26 |
-
def set_lang(self, lang: str):
|
27 |
-
with INSERTION_LOCK:
|
28 |
-
global LANG_CACHE
|
29 |
-
LANG_CACHE[self.chat_id] = lang
|
30 |
-
self.chat_info["lang"] = lang
|
31 |
-
return self.update(
|
32 |
-
{"_id": self.chat_id},
|
33 |
-
{"lang": self.chat_info["lang"]},
|
34 |
-
)
|
35 |
-
|
36 |
-
def get_lang(self):
|
37 |
-
with INSERTION_LOCK:
|
38 |
-
return self.chat_info["lang"]
|
39 |
-
|
40 |
-
@staticmethod
|
41 |
-
def load_from_db():
|
42 |
-
with INSERTION_LOCK:
|
43 |
-
collection = MongoDB(Langs.db_name)
|
44 |
-
return collection.find_all()
|
45 |
-
|
46 |
-
def __ensure_in_db(self):
|
47 |
-
try:
|
48 |
-
chat_data = {"_id": self.chat_id, "lang": LANG_CACHE[self.chat_id]}
|
49 |
-
except KeyError:
|
50 |
-
chat_data = self.find_one({"_id": self.chat_id})
|
51 |
-
if not chat_data:
|
52 |
-
chat_type = self.get_chat_type()
|
53 |
-
new_data = {"_id": self.chat_id, "lang": "en", "chat_type": chat_type}
|
54 |
-
self.insert_one(new_data)
|
55 |
-
LOGGER.info(f"Initialized Language Document for chat {self.chat_id}")
|
56 |
-
return new_data
|
57 |
-
return chat_data
|
58 |
-
|
59 |
-
# Migrate if chat id changes!
|
60 |
-
def migrate_chat(self, new_chat_id: int):
|
61 |
-
old_chat_db = self.find_one({"_id": self.chat_id})
|
62 |
-
new_data = old_chat_db.update({"_id": new_chat_id})
|
63 |
-
self.insert_one(new_data)
|
64 |
-
self.delete_one({"_id": self.chat_id})
|
65 |
-
|
66 |
-
@staticmethod
|
67 |
-
def repair_db(collection):
|
68 |
-
all_data = collection.find_all()
|
69 |
-
keys = {"lang": "en", "chat_type": ""}
|
70 |
-
for data in all_data:
|
71 |
-
for key, val in keys.items():
|
72 |
-
try:
|
73 |
-
_ = data[key]
|
74 |
-
except KeyError:
|
75 |
-
LOGGER.warning(
|
76 |
-
f"Repairing Langs Database - setting '{key}:{val}' for {data['_id']}",
|
77 |
-
)
|
78 |
-
collection.update({"_id": data["_id"]}, {key: val})
|
79 |
-
|
80 |
-
|
81 |
-
def __pre_req_all_langs():
|
82 |
-
start = time()
|
83 |
-
LOGGER.info("Starting Langs Database Repair...")
|
84 |
-
collection = MongoDB(Langs.db_name)
|
85 |
-
Langs.repair_db(collection)
|
86 |
-
LOGGER.info(f"Done in {round((time() - start), 3)}s!")
|
87 |
-
|
88 |
-
|
89 |
-
def __load_lang_cache():
|
90 |
-
global LANG_CACHE
|
91 |
-
collection = MongoDB(Langs.db_name)
|
92 |
-
all_data = collection.find_all()
|
93 |
-
LANG_CACHE = {i["_id"]: i["lang"] for i in all_data}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|