Skip to content

Commit 5279cef

Browse files
committed
Add 'APscheduler' for none asynchronous auto delete (better than asyncio.sleep())
1 parent b7a4400 commit 5279cef

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

helper_func.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
import base64
44
import re
55
import asyncio
6+
import logging
67
from pyrogram import filters
78
from pyrogram.enums import ChatMemberStatus
89
from config import FORCE_SUB_CHANNEL, ADMINS, AUTO_DELETE_TIME, AUTO_DEL_SUCCESS_MSG
910
from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
1011
from pyrogram.errors import FloodWait
12+
from datetime import datetime, timedelta
13+
from apscheduler.schedulers.background import BackgroundScheduler
14+
15+
# Disable apscheduler logging
16+
apscheduler_logger = logging.getLogger('apscheduler')
17+
apscheduler_logger.addHandler(logging.NullHandler())
18+
apscheduler_logger.propagate = False
19+
20+
# Initialize background scheduler
21+
scheduler = BackgroundScheduler()
22+
1123

1224
async def is_subscribed(filter, client, update):
1325
if not FORCE_SUB_CHANNEL:
@@ -106,14 +118,16 @@ def get_readable_time(seconds: int) -> str:
106118
return up_time
107119

108120
async def delete_file(message, client, process):
109-
await asyncio.sleep(AUTO_DELETE_TIME)
110121
for msg in message:
111122
try:
112-
await client.delete_messages(chat_id=msg.chat.id, message_ids=[msg.id])
123+
scheduler.add_job(msg.delete, 'date', run_date=datetime.now() + timedelta(seconds=AUTO_DELETE_TIME))
113124
except Exception as e:
114-
await asyncio.sleep(e.x)
125+
await asyncio.sleep(e.value)
115126

116127
await process.edit_text(AUTO_DEL_SUCCESS_MSG)
117128

118129

119130
subscribed = filters.create(is_subscribed)
131+
132+
# start scheduler
133+
scheduler.start()

plugins/start.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ async def start_command(client: Client, message: Message):
7070
else:
7171
reply_markup = None
7272

73-
if AUTO_DELETE_TIME:
73+
if AUTO_DELETE_TIME and AUTO_DELETE_TIME > 0:
7474
track_msgs = []
7575

7676
try:
7777
copied_msg_for_deletion = await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT)
7878
track_msgs.append(copied_msg_for_deletion)
7979

8080
except FloodWait as e:
81-
await asyncio.sleep(e.x)
81+
await asyncio.sleep(e.value)
8282
copied_msg_for_deletion = await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT)
8383
track_msgs.append(copied_msg_for_deletion)
8484

@@ -94,7 +94,7 @@ async def start_command(client: Client, message: Message):
9494
await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT)
9595
await asyncio.sleep(0.5)
9696
except FloodWait as e:
97-
await asyncio.sleep(e.x)
97+
await asyncio.sleep(e.value)
9898
await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT)
9999
except:
100100
pass

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ pymongo
77
dnspython
88
# --- For-Web-Response ------- #
99
aiohttp
10+
# --- For-None-Asynchronous-Auto-Delete-----#
11+
apscheduler

0 commit comments

Comments
 (0)