From aec2d3b546c1d0cd5c3b99833cbae8d4aa6f794d Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 16 May 2024 21:11:12 +0800 Subject: [PATCH] feat: auto delete bot start message --- src/plugins/start.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/plugins/start.py b/src/plugins/start.py index 21ae42a..3f613b2 100644 --- a/src/plugins/start.py +++ b/src/plugins/start.py @@ -1,8 +1,12 @@ +import contextlib +from datetime import datetime, timedelta + from pyrogram import filters -from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from pyrogram.enums import ChatType +from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message from src.bot import bot - +from src.services.scheduler import scheduler HELP_MSG = "此 BOT 将会自动回复可以转换成 Telegram 预览的 URL 链接,可以提供更直观、方便的浏览体验。" TEST_URL = "https://m.miyoushe.com/ys?channel=xiaomi/#/article/51867765" @@ -21,10 +25,26 @@ def get_test_button() -> InlineKeyboardMarkup: ) +async def delete_message(message: Message): + with contextlib.suppress(Exception): + await message.delete() + + +def add_delete_message_task(message: Message): + scheduler.add_job( + delete_message, + "date", + run_date=datetime.now() + timedelta(seconds=60), + args=(message,), + ) + + @bot.on_message(filters=filters.command("start")) -async def start(_, message): - await message.reply_text( +async def start(_, message: "Message"): + reply = await message.reply_text( HELP_MSG, quote=True, reply_markup=get_test_button(), ) + if message.chat and message.chat.type in [ChatType.GROUP, ChatType.SUPERGROUP]: + add_delete_message_task(reply)