From 5279cef2c88472d7a4dc64a389f9cb385762e02b Mon Sep 17 00:00:00 2001 From: "knmuhammed2@gmail.com" Date: Sun, 17 Nov 2024 01:56:15 +0530 Subject: [PATCH] Add 'APscheduler' for none asynchronous auto delete (better than asyncio.sleep()) --- helper_func.py | 20 +++++++++++++++++--- plugins/start.py | 6 +++--- requirements.txt | 2 ++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/helper_func.py b/helper_func.py index a61f9047b..a1979747a 100644 --- a/helper_func.py +++ b/helper_func.py @@ -3,11 +3,23 @@ import base64 import re import asyncio +import logging from pyrogram import filters from pyrogram.enums import ChatMemberStatus from config import FORCE_SUB_CHANNEL, ADMINS, AUTO_DELETE_TIME, AUTO_DEL_SUCCESS_MSG from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant from pyrogram.errors import FloodWait +from datetime import datetime, timedelta +from apscheduler.schedulers.background import BackgroundScheduler + +# Disable apscheduler logging +apscheduler_logger = logging.getLogger('apscheduler') +apscheduler_logger.addHandler(logging.NullHandler()) +apscheduler_logger.propagate = False + +# Initialize background scheduler +scheduler = BackgroundScheduler() + async def is_subscribed(filter, client, update): if not FORCE_SUB_CHANNEL: @@ -106,14 +118,16 @@ def get_readable_time(seconds: int) -> str: return up_time async def delete_file(message, client, process): - await asyncio.sleep(AUTO_DELETE_TIME) for msg in message: try: - await client.delete_messages(chat_id=msg.chat.id, message_ids=[msg.id]) + scheduler.add_job(msg.delete, 'date', run_date=datetime.now() + timedelta(seconds=AUTO_DELETE_TIME)) except Exception as e: - await asyncio.sleep(e.x) + await asyncio.sleep(e.value) await process.edit_text(AUTO_DEL_SUCCESS_MSG) subscribed = filters.create(is_subscribed) + +# start scheduler +scheduler.start() diff --git a/plugins/start.py b/plugins/start.py index 2c72abf5e..4b6459844 100644 --- a/plugins/start.py +++ b/plugins/start.py @@ -70,7 +70,7 @@ async def start_command(client: Client, message: Message): else: reply_markup = None - if AUTO_DELETE_TIME: + if AUTO_DELETE_TIME and AUTO_DELETE_TIME > 0: track_msgs = [] try: @@ -78,7 +78,7 @@ async def start_command(client: Client, message: Message): track_msgs.append(copied_msg_for_deletion) except FloodWait as e: - await asyncio.sleep(e.x) + await asyncio.sleep(e.value) 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) track_msgs.append(copied_msg_for_deletion) @@ -94,7 +94,7 @@ async def start_command(client: Client, message: Message): await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT) await asyncio.sleep(0.5) except FloodWait as e: - await asyncio.sleep(e.x) + await asyncio.sleep(e.value) await msg.copy(chat_id=message.from_user.id, caption=caption, parse_mode=ParseMode.HTML, reply_markup=reply_markup, protect_content=PROTECT_CONTENT) except: pass diff --git a/requirements.txt b/requirements.txt index 863b82a65..0edfcbb81 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,5 @@ pymongo dnspython # --- For-Web-Response ------- # aiohttp +# --- For-None-Asynchronous-Auto-Delete-----# +apscheduler