sticker-captcha-bot/plugins/chat_member_update.py

136 lines
4.4 KiB
Python
Raw Permalink Normal View History

2024-11-11 08:57:10 +00:00
import asyncio
2022-07-05 08:29:02 +00:00
import contextlib
2024-11-11 08:57:10 +00:00
from time import time
from typing import Dict
2022-07-05 08:29:02 +00:00
2023-09-11 12:48:52 +00:00
from cashews import cache
2022-07-05 08:29:02 +00:00
from pyrogram import filters
2023-09-11 12:48:52 +00:00
from pyrogram.enums import ChatMemberStatus
from pyrogram.types import ChatMemberUpdated
2022-07-05 08:29:02 +00:00
2023-09-11 12:48:52 +00:00
from pyromod.utils.errors import TimeoutConversationError
2024-01-22 13:06:47 +00:00
from sticker.languages import MSG_PUBLIC, ADMIN_MSG, MSG, VERIFY_TIME
2024-11-11 08:57:10 +00:00
from sticker.scheduler import add_ban_chat_member_job, add_delete_message_id_job
from sticker.functions.service_message import ServiceMessage
2024-08-07 15:33:57 +00:00
from sticker.single_utils import Client, Message
from sticker import bot, log, LogAction
2024-11-11 08:57:10 +00:00
lock_map_lock = asyncio.Lock()
lock_map: Dict[int, asyncio.Lock] = {}
async def get_lock(chat_id: int):
async with lock_map_lock:
lock = lock_map.get(chat_id)
if not lock:
lock = asyncio.Lock()
lock_map[chat_id] = lock
return lock
async def send_message(client: "Client", chat, user):
n_time = time()
lock = await get_lock(chat.id)
async with lock:
if time() - n_time > 30:
# 认为此任务已过期
return
try:
key = f"msg:{chat.id}:{user.id}"
msg: "Message" = await client.send_message(
chat.id, MSG % (user.mention, user.mention)
)
await msg.delay_delete(VERIFY_TIME + 5)
await cache.set(key, msg.id, expire=VERIFY_TIME + 5)
except Exception:
return
await log(chat, user, LogAction.REQUEST)
2024-08-07 15:33:57 +00:00
async def start_verify(client: "Client", chat, user):
key = f"wait:{chat.id}:{user.id}"
2024-11-11 08:57:10 +00:00
key2 = f"msg:{chat.id}:{user.id}"
2024-08-07 15:33:57 +00:00
await cache.set(key, "True", expire=VERIFY_TIME + 5)
2024-11-11 08:57:10 +00:00
client.loop.create_task(send_message(client, chat, user))
2024-08-07 15:33:57 +00:00
try:
msg_: "Message" = await client.listen(
2024-11-11 08:57:10 +00:00
chat.id,
filters=filters.user(user.id) & ~filters.service,
timeout=VERIFY_TIME,
2024-08-07 15:33:57 +00:00
)
2024-11-11 08:57:10 +00:00
msg = await cache.get(key2)
if msg:
2024-11-17 13:14:22 +00:00
add_delete_message_id_job(chat.id, msg, 1)
2024-08-07 15:33:57 +00:00
await msg_.delay_delete(1)
if not msg_.sticker:
add_ban_chat_member_job(chat.id, user.id)
await log(chat, user, LogAction.FAIL_ERROR)
await ServiceMessage.try_delete(user.id, chat.id)
else:
await cache.delete(key)
await log(chat, user, LogAction.ACCEPT)
except TimeoutConversationError:
2024-11-11 08:57:10 +00:00
msg = await cache.get(key2)
if msg:
2024-11-17 13:14:22 +00:00
add_delete_message_id_job(chat.id, msg, 1)
2024-08-07 15:33:57 +00:00
add_ban_chat_member_job(chat.id, user.id)
await log(chat, user, LogAction.FAIL_TIMEOUT)
await ServiceMessage.try_delete(user.id, chat.id)
2022-07-05 08:29:02 +00:00
2023-09-11 12:48:52 +00:00
@bot.on_chat_member_updated()
async def invite(client: Client, chat_member_updated: ChatMemberUpdated):
chat = chat_member_updated.chat
member = chat_member_updated.new_chat_member
old_member = chat_member_updated.old_chat_member
if not member:
return
if not member.user:
return
user = member.user
old_user = old_member.user if old_member else None
2024-08-07 15:33:57 +00:00
if user.is_self:
if member.status not in {
ChatMemberStatus.ADMINISTRATOR,
ChatMemberStatus.MEMBER,
}:
return
await log(chat, chat_member_updated.from_user, LogAction.NEW_GROUP)
if chat.username:
with contextlib.suppress(Exception):
await client.send_message(chat.id, MSG_PUBLIC)
return
2023-09-11 13:05:50 +00:00
if user.is_verified or user.is_bot or user.is_deleted or user.is_support:
2022-07-05 08:29:02 +00:00
return
2023-09-11 12:48:52 +00:00
if member.status not in {ChatMemberStatus.MEMBER}:
return
2023-09-11 13:05:50 +00:00
if (
old_user
and old_user.id == user.id
2023-10-31 05:17:44 +00:00
and old_member.status
2023-09-11 13:05:50 +00:00
in {
ChatMemberStatus.ADMINISTRATOR,
ChatMemberStatus.OWNER,
ChatMemberStatus.MEMBER,
ChatMemberStatus.RESTRICTED,
}
):
return
from_user = chat_member_updated.from_user
if from_user and from_user.id == user.id:
from_user = None
2023-09-23 03:37:40 +00:00
if from_user and from_user.is_self:
2023-09-13 14:34:14 +00:00
return
2023-09-11 13:05:50 +00:00
if (
user
and from_user
and (await bot.get_chat_member(chat.id, from_user.id)).status
in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}
):
2022-07-06 15:20:06 +00:00
with contextlib.suppress(Exception):
2024-08-07 15:33:57 +00:00
msg: "Message" = await client.send_message(chat.id, ADMIN_MSG)
await msg.delay_delete()
return
await start_verify(client, chat, user)