soxogvv commited on
Commit
0796c19
Β·
verified Β·
1 Parent(s): bd896c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -66,69 +66,69 @@ def create_today_plan():
66
 
67
  def apply_today_plan():
68
  tz = pytz.timezone("Asia/Kolkata")
69
- now = datetime.now(tz)
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
- if run_time > now:
 
 
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
- plans.delete_many({}) # Clear existing plans
89
- end_of_day = datetime.now(tz).replace(hour=23, minute=59, second=0, microsecond=0)
 
 
 
90
 
91
  for acc in accounts.find():
92
- limit = random.randint(3, 5)
93
- times = set()
94
- attempts = 0
 
 
 
95
 
96
- while len(times) < limit and attempts < 10:
 
 
97
  now = datetime.now(tz).replace(second=0, microsecond=0)
98
- attempts += 1
99
 
100
- minutes_left = int((end_of_day - now).total_seconds() // 60)
101
- if minutes_left <= 10:
102
- break # Not enough time left in the day
103
 
104
- random_minutes = random.randint(6, minutes_left) # βœ… At least 6 minutes in future
 
 
 
 
105
  future_time = now + timedelta(minutes=random_minutes)
106
 
107
- # Only add if it's at least 5 minutes in the future
108
- if future_time > now + timedelta(minutes=5):
109
- times.add(future_time.replace(second=0, microsecond=0))
110
 
111
- if times:
112
- plans.insert_one({
113
- "user_id": acc["user_id"],
114
- "times": sorted(times),
115
- "created": 0
116
- })
117
 
118
- log.info("βœ… Today's plans reset with future-safe times.")
119
- apply_today_plan()
 
 
 
120
 
121
- try:
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):