txlyre 1 month ago
parent
commit
40cfc24730
2 changed files with 26 additions and 8 deletions
  1. 24 7
      main.py
  2. 2 1
      requirements.txt

+ 24 - 7
main.py

@@ -6,6 +6,7 @@ import asyncio
 from datetime import datetime, timedelta
 from collections import namedtuple
 
+import aiohttp
 from dotenv import load_dotenv
 from telethon import TelegramClient, events
 from telethon.utils import get_peer_id, get_display_name
@@ -44,10 +45,20 @@ ALLOWED_GROUPS = set(
 )
 CAPTCHA_WAIT_TIME = int(os.getenv("CAPTCHA_WAIT_TIME", 80))
 MAX_CAPTCHA_ATTEMPTS = int(os.getenv("MAX_CAPTCHA_ATTEMPTS", 8))
+USE_LOLS_API = os.getenv("USE_LOLS_API", "yes") == "yes"
 
 bot = TelegramClient("captcha_bot", API_ID, API_HASH).start(bot_token=BOT_TOKEN)
 pending = {}
 
+async def lols_check(user_id):
+    try:
+        async with aiohttp.ClientSession() as session:
+            async with session.get(f"https://api.lols.bot/account?id={user_id}") as response:
+                data = await response.json()
+
+                return data["banned"] or data["scammer"]
+    except Exception:
+        return False
 
 class PendingUser:
     def __init__(self, peer_id, user_id):
@@ -68,6 +79,12 @@ class PendingUser:
         return time.time() - self.join_ts >= CAPTCHA_WAIT_TIME
 
     async def start(self, event):
+        if USE_LOLS_API:
+            if not await lols_check(self.user_id):
+                await self.kick(forever=True)
+
+                return
+
         await self.update_captcha()
 
         self.text = f"Welcome, {get_link_to_user(event.user)}! Please write a message with the **four latin letters (case insensitive)** that appear in this image to verify that you are a human. If you don't solve the captcha in **{CAPTCHA_WAIT_TIME}** seconds, you will be automatically kicked out of the group."
@@ -133,12 +150,13 @@ class PendingUser:
     async def remove(self):
         del pending[self.user_id]
 
-        try:
-            await bot.delete_messages(self.peer_id, self.message_id)
-        except Exception:
-            pass
+        if self.message_id is not None:
+            try:
+                await bot.delete_messages(self.peer_id, self.message_id)
+            except Exception:
+                pass
 
-    async def kick(self):
+    async def kick(self, forever=False):
         if self.user_id not in pending:
             return
 
@@ -150,7 +168,7 @@ class PendingUser:
                     self.peer_id,
                     self.user_id,
                     ChatBannedRights(
-                        until_date=datetime.now() + timedelta(minutes=5),
+                        until_date=None if forever else datetime.now() + timedelta(minutes=5),
                         view_messages=True,
                     ),
                 )
@@ -158,7 +176,6 @@ class PendingUser:
         except Exception:
             pass
 
-
 @bot.on(events.ChatAction)
 async def handler(event):
     if event.chat_id not in ALLOWED_GROUPS:

+ 2 - 1
requirements.txt

@@ -1,3 +1,4 @@
 python-dotenv
 telethon
-captcha
+captcha
+aiohttp