iShotaBot/scheduler.py

58 lines
1.6 KiB
Python
Raw Normal View History

2022-10-02 15:14:06 +00:00
import contextlib
import datetime
2023-06-26 04:02:44 +00:00
from pathlib import Path
2022-10-02 15:14:06 +00:00
import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pyrogram.types import Message
scheduler = AsyncIOScheduler(timezone="Asia/ShangHai")
async def delete_message(message: Message) -> bool:
with contextlib.suppress(Exception):
await message.delete()
return True
return False
2023-06-26 04:02:44 +00:00
async def delete_file(path: str):
path = Path(path)
if path.exists():
path.unlink(missing_ok=True)
2022-10-02 15:14:06 +00:00
def add_delete_message_job(message: Message, delete_seconds: int = 60):
scheduler.add_job(
2023-01-12 13:19:54 +00:00
delete_message,
"date",
2022-10-02 15:14:06 +00:00
id=f"{message.chat.id}|{message.id}|delete_message",
name=f"{message.chat.id}|{message.id}|delete_message",
args=[message],
2023-01-12 13:19:54 +00:00
run_date=datetime.datetime.now(pytz.timezone("Asia/Shanghai"))
2023-07-16 09:49:21 +00:00
+ datetime.timedelta(seconds=delete_seconds),
2023-06-26 04:02:44 +00:00
replace_existing=True,
)
def add_delete_file_job(path: str, delete_seconds: int = 3600):
scheduler.add_job(
delete_file,
"date",
id=f"{hash(path)}|delete_file",
name=f"{hash(path)}|delete_file",
args=[path],
run_date=datetime.datetime.now(pytz.timezone("Asia/Shanghai"))
2023-07-16 09:49:21 +00:00
+ datetime.timedelta(seconds=delete_seconds),
2023-01-12 13:19:54 +00:00
replace_existing=True,
)
2023-05-31 14:10:42 +00:00
2023-07-16 09:49:21 +00:00
async def reply_message(
msg: Message, text: str, delete_origin: bool = True, *args, **kwargs
):
2023-05-31 14:10:42 +00:00
reply_msg = await msg.reply(text, *args, **kwargs)
add_delete_message_job(reply_msg)
if delete_origin:
add_delete_message_job(msg)