From d8f8e49f31a6c2779cdfb1a5bac512edcd76ae3c Mon Sep 17 00:00:00 2001
From: profitroll <vozhd.kk@gmail.com>
Date: Mon, 14 Aug 2023 15:16:30 +0200
Subject: [PATCH] Migrate to async_pymongo

---
 classes/pyroclient.py |  8 +++++---
 classes/pyrouser.py   |  8 +++++---
 modules/database.py   | 18 +++++-------------
 requirements.txt      |  8 ++------
 4 files changed, 17 insertions(+), 25 deletions(-)

diff --git a/classes/pyroclient.py b/classes/pyroclient.py
index 3e67efe..70108a1 100644
--- a/classes/pyroclient.py
+++ b/classes/pyroclient.py
@@ -18,17 +18,19 @@ class PyroClient(PyroClient):
             * `PyroUser`: PyroUser object
         """
         if (
-            col_users.find_one({"id": user.id if isinstance(user, User) else user})
+            await col_users.find_one(
+                {"id": user.id if isinstance(user, User) else user}
+            )
             is None
         ):
-            col_users.insert_one(
+            await col_users.insert_one(
                 {
                     "id": user.id if isinstance(user, User) else user,
                     "locale": user.language_code if isinstance(user, User) else None,
                 }
             )
 
-        db_record = col_users.find_one(
+        db_record = await col_users.find_one(
             {"id": user.id if isinstance(user, User) else user}
         )
 
diff --git a/classes/pyrouser.py b/classes/pyrouser.py
index 05c86bc..62a327a 100644
--- a/classes/pyrouser.py
+++ b/classes/pyrouser.py
@@ -1,15 +1,17 @@
+from dataclasses import dataclass
 from typing import Union
 
-from attrs import define
 from bson import ObjectId
 
 from modules.database import col_users
 
 
-@define
+@dataclass
 class PyroUser:
     """Dataclass of DB entry of a user"""
 
+    __slots__ = ("_id", "id", "locale")
+
     _id: ObjectId
     id: int
     locale: Union[str, None]
@@ -20,4 +22,4 @@ class PyroUser:
         ### Args:
             * locale (`str`): New locale to be set
         """
-        col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
+        await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
diff --git a/modules/database.py b/modules/database.py
index e34f260..07f32e0 100644
--- a/modules/database.py
+++ b/modules/database.py
@@ -1,11 +1,9 @@
-"""Module that provides all database columns"""
+"""Module that provides all database collections"""
 
-from pymongo import MongoClient
-from ujson import loads
+from async_pymongo import AsyncClient
+from libbot.sync import config_get
 
-with open("config.json", "r", encoding="utf-8") as f:
-    db_config = loads(f.read())["database"]
-    f.close()
+db_config = config_get("database")
 
 if db_config["user"] is not None and db_config["password"] is not None:
     con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
@@ -20,13 +18,7 @@ else:
         db_config["host"], db_config["port"], db_config["name"]
     )
 
-db_client = MongoClient(con_string)
+db_client = AsyncClient(con_string)
 db = db_client.get_database(name=db_config["name"])
 
-collections = db.list_collection_names()
-
-for collection in ["users"]:
-    if collection not in collections:
-        db.create_collection(collection)
-
 col_users = db.get_collection("users")
diff --git a/requirements.txt b/requirements.txt
index f56d9e3..a22bdeb 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,13 +1,9 @@
 aiofiles~=23.2.1
-apscheduler~=3.10.1
-attrs~=23.1.0
-black~=23.7.0
+apscheduler~=3.10.3
 convopyro==0.5
 pykeyboard==0.1.5
-pymongo==4.4.1
-pyrogram==2.0.106
 tgcrypto==1.2.5
-ujson==5.8.0
 uvloop==0.17.0
 --extra-index-url https://git.end-play.xyz/api/packages/profitroll/pypi/simple
+async_pymongo==0.1.4
 libbot[speed,pyrogram]==2.0.1
\ No newline at end of file