Streamixph05/Rstream
0
1# database.py (UPDATED VERSION)2 3import motor.motor_asyncio4from config import Config5 6class Database:7 def __init__(self):8 self._client = None9 self.db = None10 self.collection = None11 if not Config.DATABASE_URL:12 print("WARNING: DATABASE_URL not set. Links will not be permanent.")13 14 async def connect(self):15 """Database se connection banata hai."""16 if Config.DATABASE_URL:17 print("Connecting to the database...")18 self._client = motor.motor_asyncio.AsyncIOMotorClient(Config.DATABASE_URL)19 self.db = self._client["StreamLinksDB"]20 self.collection = self.db["links"]21 print("✅ Database connection established.")22 else:23 self.db = None24 self.collection = None25 26 async def disconnect(self):27 """Database connection ko band karta hai."""28 if self._client:29 self._client.close()30 print("Database connection closed.")31 32 async def save_link(self, unique_id, message_id):33 if self.collection is not None:34 await self.collection.insert_one({'_id': unique_id, 'message_id': message_id})35 36 async def get_link(self, unique_id):37 if self.collection is not None:38 doc = await self.collection.find_one({'_id': unique_id})39 return doc.get('message_id') if doc else None40 return None41 42db = Database()43 