sticker-captcha-bot/sticker/scheduler.py

105 lines
3.1 KiB
Python
Raw Normal View History

2022-07-02 11:44:57 +00:00
import contextlib
import datetime
2024-11-17 13:14:22 +00:00
from typing import Union, List
2022-07-02 11:44:57 +00:00
import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler
2022-07-06 15:20:06 +00:00
from pyrogram.types import ChatJoinRequest
2024-08-07 15:33:57 +00:00
from sticker.languages import VERIFY_TIME
2022-07-02 11:44:57 +00:00
from sticker.single_utils 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
2024-11-17 13:14:22 +00:00
async def delete_message_id(chat_id: int, message_id: Union[int, List[int]]) -> bool:
2024-11-11 08:57:10 +00:00
with contextlib.suppress(Exception):
from sticker.bot import bot
await bot.delete_messages(chat_id, message_id)
return True
return False
2022-07-06 15:20:06 +00:00
async def decline_request(chat_join_request: ChatJoinRequest):
with contextlib.suppress(Exception):
await chat_join_request.decline()
return True
return False
2024-08-07 15:33:57 +00:00
async def ban_chat_member(chat_id: int, user_id: int):
from sticker import bot
with contextlib.suppress(Exception):
await bot.ban_chat_member(
chat_id, user_id, datetime.datetime.now() + datetime.timedelta(minutes=5)
)
return True
return False
2024-11-17 13:14:22 +00:00
def add_delete_message_id_job(
chat_id: int, message_id: Union[int, List[int]], delete_seconds: int = 60
):
2024-11-11 08:57:10 +00:00
scheduler.add_job(
delete_message_id,
"date",
id=f"{chat_id}|{message_id}|delete_message",
name=f"{chat_id}|{message_id}|delete_message",
args=[chat_id, message_id],
run_date=datetime.datetime.now(pytz.timezone("Asia/Shanghai"))
+ datetime.timedelta(seconds=delete_seconds),
replace_existing=True,
)
2022-07-02 11:44:57 +00:00
def add_delete_message_job(message: Message, delete_seconds: int = 60):
scheduler.add_job(
2023-09-11 13:05:50 +00:00
delete_message,
"date",
2022-07-02 11:44:57 +00:00
id=f"{message.chat.id}|{message.id}|delete_message",
name=f"{message.chat.id}|{message.id}|delete_message",
args=[message],
2023-09-11 13:05:50 +00:00
run_date=datetime.datetime.now(pytz.timezone("Asia/Shanghai"))
+ datetime.timedelta(seconds=delete_seconds),
replace_existing=True,
)
2022-07-06 15:20:06 +00:00
def add_decline_request_job(chat_join_request: ChatJoinRequest):
scheduler.add_job(
2023-09-11 13:05:50 +00:00
decline_request,
"date",
2022-07-06 15:20:06 +00:00
id=f"{chat_join_request.chat.id}|{chat_join_request.from_user.id}|decline_request",
name=f"{chat_join_request.chat.id}|{chat_join_request.from_user.id}|decline_request",
args=[chat_join_request],
2023-09-11 13:05:50 +00:00
run_date=datetime.datetime.now(pytz.timezone("Asia/Shanghai"))
2024-08-07 15:33:57 +00:00
+ datetime.timedelta(seconds=VERIFY_TIME),
replace_existing=True,
)
def add_ban_chat_member_job(chat_id: int, user_id: int):
scheduler.add_job(
ban_chat_member,
id=f"{chat_id}|{user_id}|ban_chat_member",
name=f"{chat_id}|{user_id}|ban_chat_member",
args=[chat_id, user_id],
2023-09-11 13:05:50 +00:00
replace_existing=True,
)
2022-07-06 15:20:06 +00:00
def rem_decline_request_job(chat_join_request: ChatJoinRequest):
2023-09-11 13:05:50 +00:00
if job := scheduler.get_job(
f"{chat_join_request.chat.id}|{chat_join_request.from_user.id}|decline_request"
):
2022-07-06 15:20:06 +00:00
job.remove()