File size: 1,061 Bytes
862ffc4
 
466ac17
 
862ffc4
 
466ac17
 
862ffc4
 
 
86ea757
862ffc4
 
 
 
86ea757
862ffc4
 
 
 
 
 
43457e6
862ffc4
 
 
 
 
86ea757
43457e6
862ffc4
86ea757
 
466ac17
862ffc4
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
from motor.motor_asyncio import AsyncIOMotorClient
from core.config import logger, MONGO_URI


class DatabaseManager:
    """MongoDB 连接管理"""

    def __init__(self):
        self.client = None
        self.eco = None
        self.auth = None

    async def connect(self):
        if not MONGO_URI:
            logger.warning("[DB] MONGO_URI 未设置")
            return False
        try:
            self.client = AsyncIOMotorClient(
                MONGO_URI,
                serverSelectionTimeoutMS=5000,
                maxPoolSize=5,
                minPoolSize=1,
                maxIdleTimeMS=30000,
            )
            db = self.client["cs2_bot_db"]
            self.eco = db["economy"]
            self.auth = db["authorized_channels"]
            await self.client.admin.command("ping")
            logger.info("[DB] MongoDB 连接成功")
            return True
        except Exception as e:
            logger.error("[DB] 连接失败: %s", e)
            return False


db = DatabaseManager()