This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
EmojiCaptchaBot/plugins/handlers/user_join.py

110 lines
3.3 KiB
Python
Raw Normal View History

2023-08-10 14:05:40 +03:00
import asyncio
import contextlib
import logging
from datetime import datetime, timedelta
from pyrogram import filters
from pyrogram.types import (
ChatPermissions,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
)
from classes.pyroclient import PyroClient
2023-08-11 16:04:21 +03:00
from classes.pyrogroup import PyroGroup
2023-08-10 14:05:40 +03:00
from classes.pyrouser import PyroUser
from modules.database import col_schedule
from modules.kicker import kick_unstarted
logger = logging.getLogger(__name__)
2023-08-11 16:04:21 +03:00
@PyroClient.on_message(
2023-08-11 22:33:08 +03:00
filters.new_chat_members & filters.group & ~filters.me & ~filters.bot # type: ignore
2023-08-11 16:04:21 +03:00
)
2023-08-10 14:05:40 +03:00
async def handler_user_join(app: PyroClient, message: Message):
2023-08-11 16:04:21 +03:00
group = await PyroGroup.create_if_not_exists(message.chat.id, None, True)
locale = group.select_locale(app, message.from_user)
2023-08-10 14:05:40 +03:00
if (
app.config["whitelist"]["enabled"]
2023-08-11 16:04:21 +03:00
and group.id not in app.config["whitelist"]["groups"]
2023-08-10 14:05:40 +03:00
):
logger.info(
"User %s has joined the group %s, but it's not whitelisted, ignoring.",
message.from_user.id,
2023-08-11 16:04:21 +03:00
group.id,
2023-08-10 14:05:40 +03:00
)
return
2023-08-11 16:04:21 +03:00
logger.info("User %s has joined the group %s", message.from_user.id, group.id)
2023-08-10 14:05:40 +03:00
await message.delete()
# If user has already failed the test and joined once more
with contextlib.suppress(KeyError):
2023-08-11 16:04:21 +03:00
user = await app.find_user(message.from_user, group.id)
2023-08-10 14:05:40 +03:00
if user.failed is True:
logger.info(
"User %s has previously failed the captcha, kicking and banning him",
user.id,
)
2023-08-14 13:11:53 +03:00
if group.ban_failed:
banned = await app.ban_chat_member(group.id, user.id)
2023-08-10 14:05:40 +03:00
2023-08-14 13:11:53 +03:00
if isinstance(banned, Message):
await banned.delete()
2023-08-10 14:05:40 +03:00
return
await app.restrict_chat_member(
2023-08-11 16:04:21 +03:00
chat_id=group.id,
2023-08-10 14:05:40 +03:00
user_id=message.from_user.id,
permissions=ChatPermissions(can_send_messages=False),
)
2023-08-11 16:04:21 +03:00
user = await PyroUser.create_if_not_exists(message.from_user.id, group.id)
2023-08-10 14:05:40 +03:00
if user.mistakes > 0 or user.score > 0:
2023-08-11 16:04:21 +03:00
await user.set_score(0)
await user.set_mistakes(0)
2023-08-10 14:05:40 +03:00
2023-08-12 23:36:26 +03:00
if len(user.selected) > 0:
await user.set_selected([])
2023-08-10 14:05:40 +03:00
await asyncio.sleep(2)
verification_request = await app.send_message(
2023-08-11 16:04:21 +03:00
chat_id=group.id,
text=app._("welcome", "messages", locale=locale).format(
mention=message.from_user.mention
),
2023-08-10 14:05:40 +03:00
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
2023-08-11 16:04:21 +03:00
app._("verify", "buttons", locale=locale),
2023-08-12 23:36:26 +03:00
callback_data=f"verify:{user.id}",
2023-08-10 14:05:40 +03:00
)
],
[
InlineKeyboardButton(
2023-08-11 16:04:21 +03:00
app._("ban", "buttons", locale=locale),
2023-08-12 23:36:26 +03:00
callback_data=f"ban:{user.id}",
2023-08-10 14:05:40 +03:00
)
],
],
),
)
if app.scheduler is not None:
job = app.scheduler.add_job(
kick_unstarted,
"date",
[app, user.id, verification_request.chat.id, verification_request.id],
2023-08-14 13:11:53 +03:00
run_date=datetime.now() + timedelta(seconds=group.timeout_join),
2023-08-10 14:05:40 +03:00
)
2023-08-11 16:04:21 +03:00
await col_schedule.insert_one(
{"user": user.id, "group": group.id, "job_id": job.id}
2023-08-10 14:05:40 +03:00
)