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
2023-08-10 13:05:40 +02:00

109 lines
3.2 KiB
Python

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
from classes.pyrouser import PyroUser
from modules.database import col_schedule
from modules.kicker import kick_unstarted
logger = logging.getLogger(__name__)
@PyroClient.on_message(filters.new_chat_members & ~filters.me)
async def handler_user_join(app: PyroClient, message: Message):
if (
app.config["whitelist"]["enabled"]
and message.chat.id not in app.config["whitelist"]["groups"]
):
logger.info(
"User %s has joined the group %s, but it's not whitelisted, ignoring.",
message.from_user.id,
message.chat.id,
)
return
logger.info(
"User %s has joined the group %s", message.from_user.id, message.chat.id
)
await message.delete()
# If user has already failed the test and joined once more
with contextlib.suppress(KeyError):
user = await app.find_user(message.from_user, message.chat.id)
if user.failed is True:
logger.info(
"User %s has previously failed the captcha, kicking and banning him",
user.id,
)
banned = await app.ban_chat_member(message.chat.id, user.id)
if isinstance(banned, Message):
await banned.delete()
return
await app.restrict_chat_member(
chat_id=message.chat.id,
user_id=message.from_user.id,
permissions=ChatPermissions(can_send_messages=False),
)
user = PyroUser.create_if_not_exists(message.from_user.id, message.chat.id)
if user.mistakes > 0 or user.score > 0:
user.set_score(0)
user.set_mistakes(0)
await asyncio.sleep(2)
verification_request = await app.send_message(
chat_id=message.chat.id,
text=app._(
"welcome",
"messages",
).format(mention=message.from_user.mention),
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
app._(
"verify",
"buttons",
),
callback_data=f"verify;{message.from_user.id}",
)
],
[
InlineKeyboardButton(
app._(
"ban",
"buttons",
),
callback_data=f"ban;{message.from_user.id}",
)
],
],
),
)
if app.scheduler is not None:
job = app.scheduler.add_job(
kick_unstarted,
"date",
[app, user.id, verification_request.chat.id, verification_request.id],
run_date=datetime.now() + timedelta(seconds=app.config["timeouts"]["join"]),
)
col_schedule.insert_one(
{"user": message.from_user.id, "group": message.chat.id, "job_id": job.id}
)