Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- bot.py +0 -0
- database.py +270 -0
- requirements.txt +9 -0
bot.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
database.py
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# database.py
|
| 2 |
+
import os
|
| 3 |
+
import datetime
|
| 4 |
+
from motor.motor_asyncio import AsyncIOMotorClient
|
| 5 |
+
|
| 6 |
+
# MongoDB Connection
|
| 7 |
+
MONGO_URL = os.getenv("MONGO_URL", "mongodb://localhost:27017")
|
| 8 |
+
DB_NAME = os.getenv("DB_NAME", "razor_x_bot")
|
| 9 |
+
|
| 10 |
+
client = AsyncIOMotorClient(
|
| 11 |
+
MONGO_URL,
|
| 12 |
+
serverSelectionTimeoutMS=30000
|
| 13 |
+
)
|
| 14 |
+
db = client[DB_NAME]
|
| 15 |
+
|
| 16 |
+
# Collections
|
| 17 |
+
users_col = db["users"]
|
| 18 |
+
keys_col = db["keys"]
|
| 19 |
+
proxies_col = db["proxies"]
|
| 20 |
+
sites_col = db["sites"]
|
| 21 |
+
cards_col = db["cards"]
|
| 22 |
+
global_sites_col = db["global_sites"]
|
| 23 |
+
joined_col = db["joined_users"]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
async def init_db():
|
| 27 |
+
try:
|
| 28 |
+
await users_col.create_index("user_id", unique=True)
|
| 29 |
+
await keys_col.create_index("key", unique=True)
|
| 30 |
+
await proxies_col.create_index([("user_id", 1), ("proxy_url", 1)])
|
| 31 |
+
await sites_col.create_index([("user_id", 1), ("site", 1)])
|
| 32 |
+
await global_sites_col.create_index("site", unique=True)
|
| 33 |
+
await cards_col.create_index("created_at")
|
| 34 |
+
await joined_col.create_index("user_id", unique=True)
|
| 35 |
+
print("✅ RAZOR X Database initialized successfully!")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"⚠️ DB init warning: {e}")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
async def ensure_user(user_id: int):
|
| 41 |
+
existing = await users_col.find_one({"user_id": user_id})
|
| 42 |
+
if not existing:
|
| 43 |
+
await users_col.insert_one({
|
| 44 |
+
"user_id": user_id,
|
| 45 |
+
"plan": "Bronze",
|
| 46 |
+
"expiry": None,
|
| 47 |
+
"banned": False,
|
| 48 |
+
"banned_by": None,
|
| 49 |
+
"created_at": datetime.datetime.utcnow()
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
async def get_user_plan(user_id: int) -> str:
|
| 54 |
+
user = await users_col.find_one({"user_id": user_id})
|
| 55 |
+
if not user:
|
| 56 |
+
return "Bronze"
|
| 57 |
+
plan = user.get("plan", "Bronze")
|
| 58 |
+
expiry = user.get("expiry")
|
| 59 |
+
if expiry and datetime.datetime.utcnow() > expiry:
|
| 60 |
+
await users_col.update_one(
|
| 61 |
+
{"user_id": user_id},
|
| 62 |
+
{"$set": {"plan": "Bronze", "expiry": None}}
|
| 63 |
+
)
|
| 64 |
+
return "Bronze"
|
| 65 |
+
return plan
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
async def set_user_plan(user_id: int, plan: str, days: int = 0):
|
| 69 |
+
expiry = None
|
| 70 |
+
if days > 0:
|
| 71 |
+
expiry = datetime.datetime.utcnow() + datetime.timedelta(days=days)
|
| 72 |
+
await users_col.update_one(
|
| 73 |
+
{"user_id": user_id},
|
| 74 |
+
{"$set": {
|
| 75 |
+
"plan": plan,
|
| 76 |
+
"expiry": expiry,
|
| 77 |
+
"premium_days": days,
|
| 78 |
+
"updated_at": datetime.datetime.utcnow()
|
| 79 |
+
}},
|
| 80 |
+
upsert=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
async def is_premium_user(user_id: int) -> bool:
|
| 85 |
+
plan = await get_user_plan(user_id)
|
| 86 |
+
return plan in ["Core", "Elite", "Root", "X"]
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
async def is_banned_user(user_id: int) -> bool:
|
| 90 |
+
user = await users_col.find_one({"user_id": user_id})
|
| 91 |
+
return user.get("banned", False) if user else False
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
async def mark_user_joined(user_id: int):
|
| 95 |
+
await joined_col.update_one(
|
| 96 |
+
{"user_id": user_id},
|
| 97 |
+
{"$set": {"user_id": user_id, "joined_at": datetime.datetime.utcnow()}},
|
| 98 |
+
upsert=True
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
async def is_user_marked_joined(user_id: int) -> bool:
|
| 103 |
+
doc = await joined_col.find_one({"user_id": user_id})
|
| 104 |
+
return doc is not None
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
async def remove_joined_mark(user_id: int):
|
| 108 |
+
await joined_col.delete_one({"user_id": user_id})
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
async def add_proxy_db(user_id: int, proxy_data: dict):
|
| 112 |
+
proxy_doc = {
|
| 113 |
+
"user_id": user_id,
|
| 114 |
+
"ip": proxy_data.get("ip"),
|
| 115 |
+
"port": proxy_data.get("port"),
|
| 116 |
+
"username": proxy_data.get("username"),
|
| 117 |
+
"password": proxy_data.get("password"),
|
| 118 |
+
"proxy_url": proxy_data.get("proxy_url"),
|
| 119 |
+
"proxy_type": proxy_data.get("type", "http"),
|
| 120 |
+
"added_at": datetime.datetime.utcnow()
|
| 121 |
+
}
|
| 122 |
+
await proxies_col.insert_one(proxy_doc)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
async def get_all_user_proxies(user_id: int):
|
| 126 |
+
cursor = proxies_col.find({"user_id": user_id}).sort("added_at", 1)
|
| 127 |
+
return await cursor.to_list(length=200)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
async def get_proxy_count(user_id: int) -> int:
|
| 131 |
+
return await proxies_col.count_documents({"user_id": user_id})
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
async def get_random_proxy(user_id: int):
|
| 135 |
+
import random
|
| 136 |
+
proxies = await get_all_user_proxies(user_id)
|
| 137 |
+
if not proxies:
|
| 138 |
+
return None
|
| 139 |
+
return random.choice(proxies)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
async def remove_proxy_by_index(user_id: int, index: int):
|
| 143 |
+
proxies = await get_all_user_proxies(user_id)
|
| 144 |
+
if 0 <= index < len(proxies):
|
| 145 |
+
proxy = proxies[index]
|
| 146 |
+
await proxies_col.delete_one({"_id": proxy["_id"]})
|
| 147 |
+
return proxy
|
| 148 |
+
return None
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
async def remove_proxy_by_url(user_id: int, proxy_url: str):
|
| 152 |
+
result = await proxies_col.delete_one({
|
| 153 |
+
"user_id": user_id,
|
| 154 |
+
"proxy_url": proxy_url
|
| 155 |
+
})
|
| 156 |
+
return result.deleted_count > 0
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
async def clear_all_proxies(user_id: int) -> int:
|
| 160 |
+
result = await proxies_col.delete_many({"user_id": user_id})
|
| 161 |
+
return result.deleted_count
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
async def add_site_db(user_id: int, site: str) -> bool:
|
| 165 |
+
existing = await sites_col.find_one({"user_id": user_id, "site": site})
|
| 166 |
+
if existing:
|
| 167 |
+
return False
|
| 168 |
+
await sites_col.insert_one({
|
| 169 |
+
"user_id": user_id,
|
| 170 |
+
"site": site,
|
| 171 |
+
"added_at": datetime.datetime.utcnow()
|
| 172 |
+
})
|
| 173 |
+
return True
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
async def get_user_sites(user_id: int):
|
| 177 |
+
cursor = sites_col.find({"user_id": user_id})
|
| 178 |
+
docs = await cursor.to_list(length=50000)
|
| 179 |
+
return [doc["site"] for doc in docs]
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
async def remove_site_db(user_id: int, site: str) -> bool:
|
| 183 |
+
result = await sites_col.delete_one({"user_id": user_id, "site": site})
|
| 184 |
+
return result.deleted_count > 0
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
async def add_global_site(site: str) -> bool:
|
| 188 |
+
try:
|
| 189 |
+
await global_sites_col.insert_one({
|
| 190 |
+
"site": site,
|
| 191 |
+
"added_at": datetime.datetime.utcnow()
|
| 192 |
+
})
|
| 193 |
+
return True
|
| 194 |
+
except:
|
| 195 |
+
return False
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
async def get_global_sites():
|
| 199 |
+
cursor = global_sites_col.find()
|
| 200 |
+
docs = await cursor.to_list(length=10000)
|
| 201 |
+
return [doc["site"] for doc in docs]
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
async def remove_global_site(site: str) -> bool:
|
| 205 |
+
result = await global_sites_col.delete_one({"site": site})
|
| 206 |
+
return result.deleted_count > 0
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
async def save_card_to_db(card: str, status: str, gateway: str = "", extra: str = "", price: str = "-"):
|
| 210 |
+
await cards_col.insert_one({
|
| 211 |
+
"card": card,
|
| 212 |
+
"status": status,
|
| 213 |
+
"gateway": gateway,
|
| 214 |
+
"extra": extra,
|
| 215 |
+
"price": price,
|
| 216 |
+
"created_at": datetime.datetime.utcnow()
|
| 217 |
+
})
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
async def get_total_cards_count() -> int:
|
| 221 |
+
return await cards_col.count_documents({})
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
async def get_charged_count() -> int:
|
| 225 |
+
return await cards_col.count_documents({"status": "charged"})
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
async def get_approved_count() -> int:
|
| 229 |
+
return await cards_col.count_documents({"status": "approved"})
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
async def get_total_users() -> int:
|
| 233 |
+
return await users_col.count_documents({})
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
async def get_premium_count() -> int:
|
| 237 |
+
return await users_col.count_documents({
|
| 238 |
+
"plan": {"$in": ["Core", "Elite", "Root", "X"]}
|
| 239 |
+
})
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
async def get_all_premium_users():
|
| 243 |
+
cursor = users_col.find({"plan": {"$in": ["Core", "Elite", "Root", "X"]}})
|
| 244 |
+
return await cursor.to_list(length=1000)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
async def get_total_sites_count() -> int:
|
| 248 |
+
return await sites_col.count_documents({})
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
async def get_users_with_sites() -> int:
|
| 252 |
+
pipeline = [{"$group": {"_id": "$user_id"}}]
|
| 253 |
+
result = await sites_col.aggregate(pipeline).to_list(length=10000)
|
| 254 |
+
return len(result)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
async def get_sites_per_user():
|
| 258 |
+
pipeline = [
|
| 259 |
+
{"$group": {"_id": "$user_id", "cnt": {"$sum": 1}}},
|
| 260 |
+
{"$project": {"user_id": "$_id", "cnt": 1, "_id": 0}}
|
| 261 |
+
]
|
| 262 |
+
return await sites_col.aggregate(pipeline).to_list(length=1000)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
async def get_all_sites_detail():
|
| 266 |
+
cursor = sites_col.find().sort("user_id", 1)
|
| 267 |
+
return await cursor.to_list(length=10000)
|
| 268 |
+
|
| 269 |
+
async def Get_Sites_Per_User():
|
| 270 |
+
return await get_sites_per_user()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
telethon
|
| 2 |
+
aiohttp
|
| 3 |
+
aiofiles
|
| 4 |
+
motor
|
| 5 |
+
pymongo
|
| 6 |
+
dnspython
|
| 7 |
+
psutil
|
| 8 |
+
certifi
|
| 9 |
+
nest_asyncio
|