Бот для капчи в чатах
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from aiogram import types
 
import db
from cfg import OWNER_ID, dp, logger
from util import now_ts
 
 
@dp.message.middleware()
async def track_msg(handler, event: types.Message, data: dict):
    # Фиксируем чаты и пользователей, чтобы панель и /aperm могли их находить.
    chat = event.chat
    ts = now_ts()
    if chat and chat.type in ("group", "supergroup", "channel"):
        try:
            await db.upsert_chat(chat.id, chat.title, chat.type, ts)
        except Exception as e:
            logger.warning("upsert_chat failed chat=%s: %s", chat.id, e)
    u = event.from_user
    if u is not None and not u.is_bot:
        try:
            await db.upsert_user(u.id, u.username, u.first_name, u.last_name, ts)
        except Exception as e:
            logger.warning("upsert_user failed user=%s: %s", u.id, e)
    return await handler(event, data)
 
 
@dp.my_chat_member()
async def on_my_member(event: types.ChatMemberUpdated) -> None:
    # Обновление статуса пребывания бота в чате.
    chat = event.chat
    status = event.new_chat_member.status
    try:
        if status in ("left", "kicked"):
            await db.set_chat_active(chat.id, 0)
        else:
            await db.upsert_chat(chat.id, chat.title, chat.type, now_ts())
    except Exception as e:
        logger.warning("my_chat_member failed chat=%s: %s", chat.id, e)