Update bot/database.py
Browse files- bot/database.py +97 -62
bot/database.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
from motor.motor_asyncio import AsyncIOMotorClient
|
| 3 |
-
from utils.logger import log
|
| 4 |
|
| 5 |
class Database:
|
| 6 |
def __init__(self):
|
|
@@ -10,95 +9,131 @@ class Database:
|
|
| 10 |
async def connect(self):
|
| 11 |
uri = os.environ.get("MONGO_URI")
|
| 12 |
if not uri:
|
| 13 |
-
raise ValueError("MONGO_URI
|
| 14 |
self.client = AsyncIOMotorClient(uri)
|
| 15 |
-
self.db = self.client.
|
| 16 |
-
log("✅ Connected to MongoDB.")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
doc = await self.db.settings.find_one({"_id": "bot_settings"})
|
| 21 |
-
if doc:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
async def save_setting(self, key: str, value):
|
| 36 |
-
if self.db is None:
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
| 39 |
async def get_unavailable_set(self) -> set:
|
| 40 |
if self.db is None: return set()
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
async def add_unavailable(self, username: str):
|
| 45 |
-
if self.db is None:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
async def clear_unavailable(self):
|
| 49 |
-
if self.db is None:
|
| 50 |
-
|
| 51 |
|
|
|
|
| 52 |
async def get_custom_snipes(self) -> list:
|
| 53 |
if self.db is None: return []
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
async def add_custom_snipe(self, username: str):
|
| 58 |
-
if self.db is None:
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
async def remove_custom_snipe(self, username: str):
|
| 62 |
-
if self.db is None:
|
| 63 |
-
|
| 64 |
|
|
|
|
| 65 |
async def get_watchdog(self) -> dict:
|
| 66 |
if self.db is None: return {}
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
async def remove_watchdog(self, username: str):
|
| 75 |
-
if self.db is None:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
async def get_admins(self) -> set:
|
| 79 |
-
if self.db is None: return set()
|
| 80 |
-
docs = await self.db.admins.find({}, {"user_id": 1}).to_list(length=None)
|
| 81 |
-
return {doc["user_id"] for doc in docs}
|
| 82 |
-
|
| 83 |
-
async def add_admin(self, user_id: int):
|
| 84 |
-
if self.db is None: return
|
| 85 |
-
await self.db.admins.update_one({"user_id": user_id}, {"$set": {"user_id": user_id}}, upsert=True)
|
| 86 |
-
|
| 87 |
-
async def remove_admin(self, user_id: int):
|
| 88 |
-
if self.db is None: return
|
| 89 |
-
await self.db.admins.delete_one({"user_id": user_id})
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
async def add_fragcheck(self, username: str):
|
| 97 |
-
if self.db is None:
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
async def remove_fragcheck(self, username: str):
|
| 101 |
-
if self.db is None:
|
| 102 |
-
|
| 103 |
|
| 104 |
db = Database()
|
|
|
|
| 1 |
import os
|
| 2 |
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
|
| 3 |
|
| 4 |
class Database:
|
| 5 |
def __init__(self):
|
|
|
|
| 9 |
async def connect(self):
|
| 10 |
uri = os.environ.get("MONGO_URI")
|
| 11 |
if not uri:
|
| 12 |
+
raise ValueError("MONGO_URI not set.")
|
| 13 |
self.client = AsyncIOMotorClient(uri)
|
| 14 |
+
self.db = self.client.username_sniper
|
|
|
|
| 15 |
|
| 16 |
+
# --- SETTINGS MANAGER ---
|
| 17 |
+
async def load_settings(self, state_dict: dict):
|
| 18 |
+
if self.db is None: return
|
| 19 |
doc = await self.db.settings.find_one({"_id": "bot_settings"})
|
| 20 |
+
if not doc: return
|
| 21 |
+
|
| 22 |
+
# Ensures all custom settings survive a bot restart
|
| 23 |
+
keys = [
|
| 24 |
+
"checking_active", "sniping_active", "cycle_paused",
|
| 25 |
+
"check_interval", "snipe_interval", "autoclaim_on", "db_filter_on",
|
| 26 |
+
"total_checked", "total_available", "total_claimed",
|
| 27 |
+
"channel_claim_priority", "preferred_channel_account",
|
| 28 |
+
"watchdog_poll_interval", "fragment_sniping_active",
|
| 29 |
+
"fragment_autoclaim_on", "fragment_workers", "custom_claim_message",
|
| 30 |
+
"mute_minor_alerts"
|
| 31 |
+
]
|
| 32 |
+
for key in keys:
|
| 33 |
+
if key in doc:
|
| 34 |
+
state_dict[key] = doc[key]
|
| 35 |
|
| 36 |
async def save_setting(self, key: str, value):
|
| 37 |
+
if self.db is not None:
|
| 38 |
+
await self.db.settings.update_one(
|
| 39 |
+
{"_id": "bot_settings"},
|
| 40 |
+
{"$set": {key: value}},
|
| 41 |
+
upsert=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# --- ADMIN MANAGER ---
|
| 45 |
+
async def add_admin(self, user_id: int):
|
| 46 |
+
if self.db is not None:
|
| 47 |
+
await self.db.admins.update_one(
|
| 48 |
+
{"user_id": user_id},
|
| 49 |
+
{"$set": {"user_id": user_id}},
|
| 50 |
+
upsert=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
async def get_admins(self) -> set:
|
| 54 |
+
if self.db is None: return set()
|
| 55 |
+
cursor = self.db.admins.find({}, {"user_id": 1})
|
| 56 |
+
docs = await cursor.to_list(length=None)
|
| 57 |
+
return {doc["user_id"] for doc in docs}
|
| 58 |
|
| 59 |
+
# --- UNAVAILABLE SET ---
|
| 60 |
async def get_unavailable_set(self) -> set:
|
| 61 |
if self.db is None: return set()
|
| 62 |
+
cursor = self.db.unavailable.find({}, {"username": 1})
|
| 63 |
+
docs = await cursor.to_list(length=None)
|
| 64 |
+
return {doc["username"] for doc in docs}
|
| 65 |
|
| 66 |
async def add_unavailable(self, username: str):
|
| 67 |
+
if self.db is not None:
|
| 68 |
+
await self.db.unavailable.update_one(
|
| 69 |
+
{"username": username},
|
| 70 |
+
{"$set": {"username": username}},
|
| 71 |
+
upsert=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
async def remove_unavailable(self, username: str):
|
| 75 |
+
if self.db is not None:
|
| 76 |
+
await self.db.unavailable.delete_one({"username": username})
|
| 77 |
|
| 78 |
async def clear_unavailable(self):
|
| 79 |
+
if self.db is not None:
|
| 80 |
+
await self.db.unavailable.delete_many({})
|
| 81 |
|
| 82 |
+
# --- CUSTOM SNIPES ---
|
| 83 |
async def get_custom_snipes(self) -> list:
|
| 84 |
if self.db is None: return []
|
| 85 |
+
cursor = self.db.custom_snipes.find({}, {"username": 1})
|
| 86 |
+
docs = await cursor.to_list(length=None)
|
| 87 |
+
return [doc["username"] for doc in docs]
|
| 88 |
|
| 89 |
async def add_custom_snipe(self, username: str):
|
| 90 |
+
if self.db is not None:
|
| 91 |
+
await self.db.custom_snipes.update_one(
|
| 92 |
+
{"username": username},
|
| 93 |
+
{"$set": {"username": username}},
|
| 94 |
+
upsert=True
|
| 95 |
+
)
|
| 96 |
|
| 97 |
async def remove_custom_snipe(self, username: str):
|
| 98 |
+
if self.db is not None:
|
| 99 |
+
await self.db.custom_snipes.delete_one({"username": username})
|
| 100 |
|
| 101 |
+
# --- WATCHDOG ---
|
| 102 |
async def get_watchdog(self) -> dict:
|
| 103 |
if self.db is None: return {}
|
| 104 |
+
cursor = self.db.watchdog.find({}, {"username": 1, "added_by": 1})
|
| 105 |
+
docs = await cursor.to_list(length=None)
|
| 106 |
+
return {doc["username"]: doc.get("added_by") for doc in docs}
|
| 107 |
+
|
| 108 |
+
async def add_watchdog(self, username: str, user_id: int):
|
| 109 |
+
if self.db is not None:
|
| 110 |
+
await self.db.watchdog.update_one(
|
| 111 |
+
{"username": username},
|
| 112 |
+
{"$set": {"username": username, "added_by": user_id}},
|
| 113 |
+
upsert=True
|
| 114 |
+
)
|
| 115 |
|
| 116 |
async def remove_watchdog(self, username: str):
|
| 117 |
+
if self.db is not None:
|
| 118 |
+
await self.db.watchdog.delete_one({"username": username})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
# --- FRAGMENT CHECK ---
|
| 121 |
+
async def get_fragcheck(self) -> list:
|
| 122 |
+
if self.db is None: return []
|
| 123 |
+
cursor = self.db.fragcheck.find({}, {"username": 1})
|
| 124 |
+
docs = await cursor.to_list(length=None)
|
| 125 |
+
return [doc["username"] for doc in docs]
|
| 126 |
|
| 127 |
async def add_fragcheck(self, username: str):
|
| 128 |
+
if self.db is not None:
|
| 129 |
+
await self.db.fragcheck.update_one(
|
| 130 |
+
{"username": username},
|
| 131 |
+
{"$set": {"username": username}},
|
| 132 |
+
upsert=True
|
| 133 |
+
)
|
| 134 |
|
| 135 |
async def remove_fragcheck(self, username: str):
|
| 136 |
+
if self.db is not None:
|
| 137 |
+
await self.db.fragcheck.delete_one({"username": username})
|
| 138 |
|
| 139 |
db = Database()
|