Update app.py
Browse files
app.py
CHANGED
|
@@ -66,69 +66,69 @@ def create_today_plan():
|
|
| 66 |
|
| 67 |
def apply_today_plan():
|
| 68 |
tz = pytz.timezone("Asia/Kolkata")
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
plans_today = list(plans.find())
|
| 72 |
-
for plan in plans_today:
|
| 73 |
acc = accounts.find_one({"user_id": plan["user_id"]})
|
| 74 |
if not acc or plan.get("created", 0) >= len(plan["times"]):
|
| 75 |
continue
|
| 76 |
|
| 77 |
for run_time in plan["times"]:
|
|
|
|
| 78 |
run_time = run_time.astimezone(tz)
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
scheduler.add_job(
|
| 81 |
auto_create_groups,
|
| 82 |
trigger='date',
|
| 83 |
run_date=run_time,
|
| 84 |
args=[userbots[plan["user_id"]], acc, plan]
|
| 85 |
)
|
|
|
|
| 86 |
def reset_and_plan():
|
| 87 |
tz = pytz.timezone("Asia/Kolkata")
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
for acc in accounts.find():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
now = datetime.now(tz).replace(second=0, microsecond=0)
|
| 98 |
-
|
| 99 |
|
| 100 |
-
minutes_left
|
| 101 |
-
|
| 102 |
-
break # Not enough time left in the day
|
| 103 |
|
| 104 |
-
random_minutes = random.randint(6, minutes_left)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
future_time = now + timedelta(minutes=random_minutes)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
if future_time
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
plans.insert_one({
|
| 113 |
-
"user_id": acc["user_id"],
|
| 114 |
-
"times": sorted(times),
|
| 115 |
-
"created": 0
|
| 116 |
-
})
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
loop = asyncio.get_event_loop()
|
| 123 |
-
loop.call_soon_threadsafe(
|
| 124 |
-
asyncio.create_task,
|
| 125 |
-
bot.send_message(
|
| 126 |
-
ADMIN_ID,
|
| 127 |
-
f"π Plans reset!\nποΈ Date: {datetime.now(tz).strftime('%d/%m/%Y')}\nπ Next Reset: 24/07/2025 12:00 AM IST"
|
| 128 |
-
)
|
| 129 |
-
)
|
| 130 |
-
except Exception as e:
|
| 131 |
-
log.warning(f"β οΈ Failed to notify admin: {e}")
|
| 132 |
|
| 133 |
@bot.on(events.NewMessage(pattern=r'^/stats$'))
|
| 134 |
async def handle_stats(event):
|
|
|
|
| 66 |
|
| 67 |
def apply_today_plan():
|
| 68 |
tz = pytz.timezone("Asia/Kolkata")
|
| 69 |
+
for plan in plans.find():
|
|
|
|
|
|
|
|
|
|
| 70 |
acc = accounts.find_one({"user_id": plan["user_id"]})
|
| 71 |
if not acc or plan.get("created", 0) >= len(plan["times"]):
|
| 72 |
continue
|
| 73 |
|
| 74 |
for run_time in plan["times"]:
|
| 75 |
+
now = datetime.now(tz)
|
| 76 |
run_time = run_time.astimezone(tz)
|
| 77 |
+
|
| 78 |
+
# Only schedule if it's at least 5 seconds in the future
|
| 79 |
+
if run_time > now + timedelta(seconds=5):
|
| 80 |
scheduler.add_job(
|
| 81 |
auto_create_groups,
|
| 82 |
trigger='date',
|
| 83 |
run_date=run_time,
|
| 84 |
args=[userbots[plan["user_id"]], acc, plan]
|
| 85 |
)
|
| 86 |
+
|
| 87 |
def reset_and_plan():
|
| 88 |
tz = pytz.timezone("Asia/Kolkata")
|
| 89 |
+
now = datetime.now(tz).replace(second=0, microsecond=0)
|
| 90 |
+
midnight = (now + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
| 91 |
+
minutes_left = int((midnight - now).total_seconds() // 60)
|
| 92 |
+
|
| 93 |
+
plans.delete_many({}) # Clear old plans
|
| 94 |
|
| 95 |
for acc in accounts.find():
|
| 96 |
+
if acc["user_id"] not in userbots:
|
| 97 |
+
continue
|
| 98 |
+
|
| 99 |
+
# Only generate times if there's enough time left in the day
|
| 100 |
+
if minutes_left < 20:
|
| 101 |
+
continue
|
| 102 |
|
| 103 |
+
times = []
|
| 104 |
+
used_minutes = set()
|
| 105 |
+
while len(times) < 3:
|
| 106 |
now = datetime.now(tz).replace(second=0, microsecond=0)
|
| 107 |
+
minutes_left = int(((now + timedelta(days=1)).replace(hour=0, minute=0) - now).total_seconds() // 60)
|
| 108 |
|
| 109 |
+
if minutes_left < 10:
|
| 110 |
+
break
|
|
|
|
| 111 |
|
| 112 |
+
random_minutes = random.randint(6, minutes_left)
|
| 113 |
+
if random_minutes in used_minutes:
|
| 114 |
+
continue
|
| 115 |
+
|
| 116 |
+
used_minutes.add(random_minutes)
|
| 117 |
future_time = now + timedelta(minutes=random_minutes)
|
| 118 |
|
| 119 |
+
# Skip if time is already past
|
| 120 |
+
if future_time <= now:
|
| 121 |
+
continue
|
| 122 |
|
| 123 |
+
times.append(future_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
plans.insert_one({
|
| 126 |
+
"user_id": acc["user_id"],
|
| 127 |
+
"times": sorted(times),
|
| 128 |
+
"created": 0
|
| 129 |
+
})
|
| 130 |
|
| 131 |
+
log.info("π All plans have been reset and regenerated.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
@bot.on(events.NewMessage(pattern=r'^/stats$'))
|
| 134 |
async def handle_stats(event):
|