Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from aiohttp import web
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# β‘
|
| 12 |
from pyrogram import idle
|
| 13 |
|
| 14 |
# Strict Environment Validation Framework
|
|
@@ -24,11 +24,10 @@ from bot.database import db
|
|
| 24 |
from bot.state import state
|
| 25 |
from bot.client import bot, init_clients
|
| 26 |
from bot.scheduler import scheduler, shutdown_campers
|
| 27 |
-
|
| 28 |
-
# β‘ FINAL FIX: Import the scraper to initialize its aiohttp session
|
| 29 |
from bot.scraper import scraper
|
| 30 |
|
| 31 |
async def handle_ping(request):
|
|
|
|
| 32 |
return web.json_response({"error": "Forbidden", "status": 403}, status=403)
|
| 33 |
|
| 34 |
async def keep_alive_server():
|
|
@@ -49,7 +48,23 @@ async def safe_boot_sequence():
|
|
| 49 |
await db.init_db()
|
| 50 |
await db.load_settings(state)
|
| 51 |
|
| 52 |
-
# β‘
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
await scraper.init_session()
|
| 54 |
|
| 55 |
# Load Target Queues
|
|
@@ -61,6 +76,25 @@ async def safe_boot_sequence():
|
|
| 61 |
for u in custom_snipes:
|
| 62 |
if u not in temp_s: temp_s.append(u)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if state["db_filter_on"]:
|
| 65 |
before = len(temp_c)
|
| 66 |
temp_c = [u for u in temp_c if u not in state["unavailable_set"]]
|
|
@@ -81,7 +115,7 @@ async def safe_boot_sequence():
|
|
| 81 |
# Initialize UI Bot
|
| 82 |
await bot.start()
|
| 83 |
|
| 84 |
-
#
|
| 85 |
import bot.handlers as ui_handlers
|
| 86 |
|
| 87 |
# Initialize Snipe Fleet
|
|
@@ -97,11 +131,12 @@ async def safe_boot_sequence():
|
|
| 97 |
async def main():
|
| 98 |
try:
|
| 99 |
boot_success = await safe_boot_sequence()
|
| 100 |
-
if not boot_success:
|
|
|
|
| 101 |
|
| 102 |
from bot.scheduler import account_status_job, schedule_next, watchdog_cycle, reset_hourly_counters, flush_stats_to_db
|
| 103 |
|
| 104 |
-
#
|
| 105 |
scheduler.add_job(account_status_job, "interval", minutes=1)
|
| 106 |
scheduler.add_job(watchdog_cycle, "interval", seconds=3)
|
| 107 |
scheduler.add_job(reset_hourly_counters, "cron", minute=0)
|
|
@@ -115,7 +150,10 @@ async def main():
|
|
| 115 |
from bot.handlers import sync_fleet
|
| 116 |
await sync_fleet()
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
await asyncio.gather(
|
| 120 |
keep_alive_server(),
|
| 121 |
idle()
|
|
@@ -126,11 +164,14 @@ async def main():
|
|
| 126 |
finally:
|
| 127 |
log("π Initiating Graceful Shutdown...")
|
| 128 |
shutdown_campers()
|
| 129 |
-
for acc in state["account_pool"]:
|
| 130 |
-
if getattr(acc.client, "is_connected", False): await acc.client.stop()
|
| 131 |
-
if getattr(bot, "is_connected", False): await bot.stop()
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
await scraper.close_session()
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# β‘ Import 'idle' correctly to keep the bot listening
|
| 12 |
from pyrogram import idle
|
| 13 |
|
| 14 |
# Strict Environment Validation Framework
|
|
|
|
| 24 |
from bot.state import state
|
| 25 |
from bot.client import bot, init_clients
|
| 26 |
from bot.scheduler import scheduler, shutdown_campers
|
|
|
|
|
|
|
| 27 |
from bot.scraper import scraper
|
| 28 |
|
| 29 |
async def handle_ping(request):
|
| 30 |
+
"""OPSEC Decoy server to satisfy PaaS port requirements."""
|
| 31 |
return web.json_response({"error": "Forbidden", "status": 403}, status=403)
|
| 32 |
|
| 33 |
async def keep_alive_server():
|
|
|
|
| 48 |
await db.init_db()
|
| 49 |
await db.load_settings(state)
|
| 50 |
|
| 51 |
+
# β‘ Load Admins from Environment and Database
|
| 52 |
+
if "admin_ids" not in state:
|
| 53 |
+
state["admin_ids"] = set()
|
| 54 |
+
|
| 55 |
+
env_admins = os.environ.get("ADMIN_IDS", "")
|
| 56 |
+
if env_admins:
|
| 57 |
+
for x in env_admins.split(","):
|
| 58 |
+
if x.strip().lstrip('-').isdigit():
|
| 59 |
+
state["admin_ids"].add(int(x.strip()))
|
| 60 |
+
|
| 61 |
+
db_admins = await db.get_admins()
|
| 62 |
+
for adm in db_admins:
|
| 63 |
+
state["admin_ids"].add(adm)
|
| 64 |
+
|
| 65 |
+
log(f"π Authorization loaded for {len(state['admin_ids'])} admins.")
|
| 66 |
+
|
| 67 |
+
# Boot up the Scraper's HTTP pool
|
| 68 |
await scraper.init_session()
|
| 69 |
|
| 70 |
# Load Target Queues
|
|
|
|
| 76 |
for u in custom_snipes:
|
| 77 |
if u not in temp_s: temp_s.append(u)
|
| 78 |
|
| 79 |
+
# β‘ Restore Watchdog and Fragment Lists from MongoDB
|
| 80 |
+
if "watch_submitters" not in state:
|
| 81 |
+
state["watch_submitters"] = {}
|
| 82 |
+
if "watchdog_queue" not in state:
|
| 83 |
+
state["watchdog_queue"] = set()
|
| 84 |
+
if "fragcheck_queue" not in state:
|
| 85 |
+
state["fragcheck_queue"] = set()
|
| 86 |
+
|
| 87 |
+
watchdog_data = await db.get_watchdog()
|
| 88 |
+
for u, user_id in watchdog_data.items():
|
| 89 |
+
state["watchdog_queue"].add(u)
|
| 90 |
+
state["watch_submitters"][u] = user_id
|
| 91 |
+
|
| 92 |
+
frag_data = await db.get_fragcheck()
|
| 93 |
+
for u in frag_data:
|
| 94 |
+
state["fragcheck_queue"].add(u)
|
| 95 |
+
|
| 96 |
+
log(f"π₯ Restored {len(state['watchdog_queue'])} Watchdog targets & {len(state['fragcheck_queue'])} Fragment targets from Database.")
|
| 97 |
+
|
| 98 |
if state["db_filter_on"]:
|
| 99 |
before = len(temp_c)
|
| 100 |
temp_c = [u for u in temp_c if u not in state["unavailable_set"]]
|
|
|
|
| 115 |
# Initialize UI Bot
|
| 116 |
await bot.start()
|
| 117 |
|
| 118 |
+
# Alias handlers to link your commands and buttons to the active bot
|
| 119 |
import bot.handlers as ui_handlers
|
| 120 |
|
| 121 |
# Initialize Snipe Fleet
|
|
|
|
| 131 |
async def main():
|
| 132 |
try:
|
| 133 |
boot_success = await safe_boot_sequence()
|
| 134 |
+
if not boot_success:
|
| 135 |
+
return
|
| 136 |
|
| 137 |
from bot.scheduler import account_status_job, schedule_next, watchdog_cycle, reset_hourly_counters, flush_stats_to_db
|
| 138 |
|
| 139 |
+
# Start Background Engines
|
| 140 |
scheduler.add_job(account_status_job, "interval", minutes=1)
|
| 141 |
scheduler.add_job(watchdog_cycle, "interval", seconds=3)
|
| 142 |
scheduler.add_job(reset_hourly_counters, "cron", minute=0)
|
|
|
|
| 150 |
from bot.handlers import sync_fleet
|
| 151 |
await sync_fleet()
|
| 152 |
|
| 153 |
+
from bot.alerts import alert_admin
|
| 154 |
+
await alert_admin("π’ <b>Enterprise System Online</b>\nAll modules restored and listening.")
|
| 155 |
+
|
| 156 |
+
# β‘ ENTERPRISE FIX: Run both concurrently so the bot actively listens to buttons
|
| 157 |
await asyncio.gather(
|
| 158 |
keep_alive_server(),
|
| 159 |
idle()
|
|
|
|
| 164 |
finally:
|
| 165 |
log("π Initiating Graceful Shutdown...")
|
| 166 |
shutdown_campers()
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
for acc in state["account_pool"]:
|
| 169 |
+
if getattr(acc.client, "is_connected", False):
|
| 170 |
+
await acc.client.stop()
|
| 171 |
+
|
| 172 |
+
if getattr(bot, "is_connected", False):
|
| 173 |
+
await bot.stop()
|
| 174 |
+
|
| 175 |
await scraper.close_session()
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|