randydev commited on
Commit
87453e3
·
verified ·
1 Parent(s): 964cf89
Files changed (1) hide show
  1. database.py +0 -86
database.py DELETED
@@ -1,86 +0,0 @@
1
- import os
2
- from os import getenv
3
-
4
- from dotenv import load_dotenv
5
-
6
- import datetime
7
- from datetime import datetime as dt
8
- import time
9
-
10
- from motor import motor_asyncio
11
- from motor.core import AgnosticClient
12
- from motor.motor_asyncio import AsyncIOMotorClient
13
-
14
- from logger import LOGS
15
-
16
- load_dotenv()
17
- MONGO_URL = os.environ["MONGO_URL"]
18
-
19
- client_lang = AsyncIOMotorClient(MONGO_URL)
20
- dbs = client_lang["Akeno"]
21
- users_collection = dbs["gemini_model"]
22
-
23
- class Database:
24
- def __init__(self, uri: str) -> None:
25
- self.client: AgnosticClient = motor_asyncio.AsyncIOMotorClient(uri)
26
- self.db = self.client["Akeno"]
27
-
28
- self.user_premium = self.db["user_premium"]
29
- self.user_blacklists = self.db["user_blacklist"]
30
- self.backup_chatbot = self.db["google_genai"]
31
- self.cohere = self.db["cohere"]
32
-
33
- async def connect(self):
34
- try:
35
- await self.client.admin.command("ping")
36
- LOGS.info(f"Database Connection Established!")
37
- await self.user_premium.create_index("user_id")
38
- await self.user_premium.create_index("last_reset")
39
- await self.user_premium.create_index("premium_expiry")
40
- await self.user_blacklists.create_index("unfreeze_at")
41
- await self.user_blacklists.create_index("is_frozen")
42
- LOGS.info(f"Database Create index full Connected!")
43
- except Exception as e:
44
- LOGS.info(f"DatabaseErr: {e} ")
45
- quit(1)
46
-
47
- async def _close(self):
48
- await self.client.close()
49
-
50
- def get_datetime(self) -> str:
51
- return datetime.datetime.now().strftime("%d/%m/%Y - %H:%M")
52
-
53
- async def _get_cohere_chat_from_db(self, user_id):
54
- user_data = await self.cohere.find_one({"user_id": user_id})
55
- return user_data.get("cohere_chat", []) if user_data else []
56
-
57
- async def _update_cohere_chat_in_db(self, user_id, cohere_chat):
58
- await self.cohere.update_one(
59
- {"user_id": user_id},
60
- {"$set": {"cohere_chat": cohere_chat}},
61
- upsert=True
62
- )
63
-
64
- async def _update_chatbot_chat_in_db(self, user_id, chatbot_chat):
65
- await self.backup_chatbot.update_one(
66
- {"user_id": user_id},
67
- {"$set": {"chatbot_chat": chatbot_chat}},
68
- upsert=True
69
- )
70
-
71
- async def _get_chatbot_chat_from_db(self, user_id):
72
- user_data = await self.backup_chatbot.find_one({"user_id": user_id})
73
- return user_data.get("chatbot_chat", []) if user_data else []
74
-
75
- async def _clear_chatbot_history_in_db(self, user_id):
76
- unset_clear = {"chatbot_chat": None}
77
- return await self.backup_chatbot.update_one({"user_id": user_id}, {"$unset": unset_clear})
78
-
79
- async def _clear_chatbot_database(self, user_id):
80
- result = await self._clear_chatbot_history_in_db(user_id)
81
- if result.modified_count > 0:
82
- return "Chat history cleared successfully."
83
- else:
84
- return "No chat history found to clear."
85
-
86
- db = Database(MONGO_URL)