File size: 3,008 Bytes
85445bc
 
 
 
 
3cbcb98
5735e26
3cbcb98
 
 
 
fa7edd5
3cbcb98
85445bc
3cbcb98
85445bc
 
3cbcb98
fa7edd5
 
 
 
3cbcb98
 
 
 
 
12160f5
60e1fd1
3cbcb98
 
 
 
 
12160f5
 
 
3cbcb98
 
 
 
5735e26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cbcb98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
from os import getenv

from dotenv import load_dotenv

import datetime
from datetime import datetime as dt
import time

from motor import motor_asyncio
from motor.core import AgnosticClient
from motor.motor_asyncio import AsyncIOMotorClient

from logger import LOGS

load_dotenv()
MONGO_URL = os.environ["MONGO_URL"]

client_lang = AsyncIOMotorClient(MONGO_URL)
dbs = client_lang["Akeno"]
users_collection = dbs["gemini_model"]

class Database:
    def __init__(self, uri: str) -> None:
        self.client: AgnosticClient = motor_asyncio.AsyncIOMotorClient(uri)
        self.db = self.client["Akeno"]

        self.user_blacklists = self.db["user_blacklist"]
        self.backup_chatbot = self.db["google_genai"]

    async def connect(self):
        try:
            await self.client.admin.command("ping")
            LOGS.info(f"Database Connection Established!")
            await self.user_blacklists.create_index("unfreeze_at")
            await self.user_blacklists.create_index("is_frozen")
            LOGS.info(f"Database Create index Connected!")
        except Exception as e:
            LOGS.info(f"DatabaseErr: {e} ")
            quit(1)

    async def process_unfreezes(client):
        while True:
            now = dt.now()
            expired = await self.user_blacklists.find({
                "is_frozen": True,
                "unfreeze_at": {"$lte": now}
            })
            for user in expired:
                await self.user_blacklists.update_one(
                    {"_id": user["_id"]},
                    {"$set": {"is_frozen": False}}
                )
                try:
                    await client.send_message(
                        user["user_id"],
                        "✅ Your restrictions have been lifted"
                    )
                except:
                    pass

            await asyncio.sleep(300)

    async def _close(self):
        await self.client.close()

    def get_datetime(self) -> str:
        return datetime.datetime.now().strftime("%d/%m/%Y - %H:%M")

    async def _update_chatbot_chat_in_db(self, user_id, chatbot_chat):
        await self.backup_chatbot.update_one(
            {"user_id": user_id},
            {"$set": {"chatbot_chat": chatbot_chat}},
            upsert=True
        )

    async def _get_chatbot_chat_from_db(self, user_id):
        user_data = await self.backup_chatbot.find_one({"user_id": user_id})
        return user_data.get("chatbot_chat", []) if user_data else []

    async def _clear_chatbot_history_in_db(self, user_id):
        unset_clear = {"chatbot_chat": None}
        return await self.backup_chatbot.update_one({"user_id": user_id}, {"$unset": unset_clear})

    async def _clear_chatbot_database(self, user_id):
        result = await self._clear_chatbot_history_in_db(user_id)
        if result.modified_count > 0:
            return "Chat history cleared successfully."
        else:
            return "No chat history found to clear."

db = Database(MONGO_URL)