File size: 2,476 Bytes
14d7d32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from telethon import events
import asyncio
from telethon.errors import DataInvalidError
from . import kanha_bot

target_chat = "@HeXamonbot"
teamkanha = "@teamkanha"

MAX_CLICKS = 30
RETRY_CLICKS = 4
BOT_DELAY = 2  # seconds


async def get_latest_message_with_button(client, chat_id):
    """
    Get the latest message from the bot that still has a button.
    """
    async for msg in client.iter_messages(chat_id, limit=1):
        if msg.buttons:
            return msg
    return None


@kanha_bot.on(events.NewMessage(chats=target_chat))
async def handle_lottery_start(event):
    """Auto-click Poke Lottery buttons until no more are found."""
    client = event.client

    # Only trigger on welcome message
    if not event.raw_text.lower().startswith("welcome to the poke lottery"):
        return

    clicks = 0
    retries = 0
    message = event
    last_message = message

    try:
        await asyncio.sleep(1)  # let bot load buttons

        while clicks < MAX_CLICKS:
            # Always refresh current message
            message = await client.get_messages(message.chat_id, ids=message.id)

            if message.buttons:
                try:
                    await message.click(0, 0)
                    clicks += 1
                    retries = 0
                except DataInvalidError:
                    raise RuntimeError("Button click failed: DataInvalidError")
                except Exception as e:
                    raise RuntimeError(f"Button click failed: {str(e)}")

                await asyncio.sleep(BOT_DELAY)
                last_message = message
                continue

            # If current message has no button → check latest message with button
            latest_with_button = await get_latest_message_with_button(client, message.chat_id)
            if latest_with_button:
                last_message = latest_with_button
                try:
                    await last_message.click(0, 0)
                    clicks += 1
                    retries = 0
                except Exception:
                    retries += 1
            else:
                retries += 1

            if retries >= RETRY_CLICKS:
                break

            await asyncio.sleep(BOT_DELAY)

    except Exception as e:
        try:
            await client.send_message(
                teamkanha,
                f"⚠️ Lottery AutoClicker Error:\n\n`{str(e)}`"
            )
        except Exception:
            pass