-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathdb.py
More file actions
125 lines (102 loc) Β· 3.9 KB
/
db.py
File metadata and controls
125 lines (102 loc) Β· 3.9 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# ============================================================
#Group Manager Bot
# Author: LearningBotsOfficial (https://github.com/LearningBotsOfficial)
# Support: https://t.me/LearningBotsCommunity
# Channel: https://t.me/learning_bots
# YouTube: https://youtube.com/@learning_bots
# License: Open-source (keep credits, no resale)
# ============================================================
import motor.motor_asyncio
from config import MONGO_URI, DB_NAME
import logging
# setup logging
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s] %(asctime)s - %(message)s'
)
try:
client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_URI)
db = client[DB_NAME]
logging.info("β
MongoDB connected successfully!")
except Exception as e:
logging.error(f"β Failed to connect to MongoDB: {e}")
# ==========================================================
# π’ WELCOME MESSAGE SYSTEM
# ==========================================================
async def set_welcome_message(chat_id, text: str):
await db.welcome.update_one(
{"chat_id": chat_id},
{"$set": {"message": text}},
upsert=True
)
async def get_welcome_message(chat_id):
data = await db.welcome.find_one({"chat_id": chat_id})
return data.get("message") if data else None
async def set_welcome_status(chat_id, status: bool):
await db.welcome.update_one(
{"chat_id": chat_id},
{"$set": {"enabled": status}},
upsert=True
)
async def get_welcome_status(chat_id) -> bool:
data = await db.welcome.find_one({"chat_id": chat_id})
if not data: # default ON
return True
return bool(data.get("enabled", True))
# ==========================================================
# π LOCK SYSTEM
# ==========================================================
async def set_lock(chat_id, lock_type, status: bool):
await db.locks.update_one(
{"chat_id": chat_id},
{"$set": {f"locks.{lock_type}": status}},
upsert=True
)
async def get_locks(chat_id):
data = await db.locks.find_one({"chat_id": chat_id})
return data.get("locks", {}) if data else {}
# ==========================================================
# β οΈ WARN SYSTEM
# ==========================================================
async def add_warn(chat_id: int, user_id: int) -> int:
data = await db.warns.find_one({"chat_id": chat_id, "user_id": user_id})
warns = data.get("count", 0) + 1 if data else 1
await db.warns.update_one(
{"chat_id": chat_id, "user_id": user_id},
{"$set": {"count": warns}},
upsert=True
)
return warns
async def get_warns(chat_id: int, user_id: int) -> int:
data = await db.warns.find_one({"chat_id": chat_id, "user_id": user_id})
return data.get("count", 0) if data else 0
async def reset_warns(chat_id: int, user_id: int):
await db.warns.update_one(
{"chat_id": chat_id, "user_id": user_id},
{"$set": {"count": 0}},
upsert=True
)
# ==========================================================
# π§Ή CLEANUP UTILS (Optional)
# ==========================================================
async def clear_group_data(chat_id: int):
await db.welcome.delete_one({"chat_id": chat_id})
await db.locks.delete_one({"chat_id": chat_id})
await db.warns.delete_many({"chat_id": chat_id})
# ==========================================================
# π€ USER SYSTEM (for broadcast)
# ==========================================================
async def add_user(user_id, first_name):
await db.users.update_one(
{"user_id": user_id},
{"$set": {"first_name": first_name}},
upsert=True
)
async def get_all_users():
cursor = db.users.find({}, {"_id": 0, "user_id": 1})
users = []
async for document in cursor:
# Make sure the document has 'user_id'
if "user_id" in document:
users.append(document["user_id"])
return users