import asyncio from re import search from telethon import events from telethon.events import NewMessage, MessageEdited from telethon.errors import DataInvalidError from telethon.tl.custom import Message from . import kanha_bot HEXA_ID = 572621020 async def re_fetch(m): return await m.client.get_messages(m.chat_id, ids=m.id) async def watch_edits(client, chat, msg_id, timeout=20, uid=None): if uid is None: uid = str((await client.get_me()).id) # Automatically fetch UID if not passed async with client.conversation(chat, timeout=timeout) as conv: func = lambda e: e.id == msg_id and search(rf"(?i)Current turn: (.+){uid}", e.message.text) response = await conv.wait_event( MessageEdited(incoming=True, from_users=HEXA_ID, func=func) ) await asyncio.sleep(3) response = await re_fetch(response) return response async def do_click(msg, *buttons): async def _click(): try: await msg.click(*buttons) except DataInvalidError: pass async def _loop(): nonlocal msg for _ in range(3): await asyncio.sleep(2) r_msg = await re_fetch(msg) if r_msg and r_msg.buttons and r_msg.text == msg.text: await _click() msg = r_msg else: return await _click() asyncio.create_task(_loop()) async def get_ready(client, chat): async with client.conversation(chat, timeout=16) as conv: response = await conv.wait_event( NewMessage(incoming=True, from_users=HEXA_ID, func=lambda e: e.mentioned) ) if response.buttons and response.buttons[0][0].text.startswith("Ready"): await asyncio.sleep(3) await do_click(response, 0, 0) await asyncio.sleep(3) return response.id async def click_rnd_button(msg): for count1, sub_buttons in enumerate(msg.buttons): for count2, button in enumerate(sub_buttons): text = str(button.text).strip() if text and text.isdigit(): return await do_click(msg, count1, count2) async def auto_battle(client, chat, msg_id): response = await watch_edits(client, chat, msg_id, timeout=15) await asyncio.sleep(2) if not response.buttons: return True elif "team" in response.buttons[0][0].text.lower(): return await click_rnd_button(response) else: if 'missed' in response.raw_text or 'dodged' in response.raw_text: await do_click(response, 0, 0) return @kanha_bot.on(NewMessage(incoming=True, pattern="^/fakeChallenge$")) async def autohemxa(e): client = e.client await asyncio.sleep(1) msg = await e.reply("#ReadyforBattle") try: hexa_msg_id = await get_ready(client, e.chat_id) await msg.delete() while True: response = await auto_battle(client, e.chat_id, hexa_msg_id) if response is True: return except asyncio.TimeoutError: return await msg.respond("Timeout Error.. Skipping!") except Exception as exc: await msg.respond(f"**2nd ID Error** \n\nGot {exc.__class__} \n`{exc}`") @kanha_bot.on(events.NewMessage(incoming=True)) async def handle_team2(event): client = event.client chat = "HeXamonbot" if "✅ Successfully loaded Team 1!" in event.raw_text: retries = 3 delay = 5 click_attempts = 5 click_delay = 4 response = None for attempt in range(retries): async with client.conversation(chat, timeout=20) as conv: try: await conv.send_message("/myteam") response = await conv.get_response(timeout=10) if response.buttons: break else: await asyncio.sleep(delay) except asyncio.TimeoutError: if attempt < retries - 1: await asyncio.sleep(delay) else: await event.respond("⚠️ Bot did not respond after multiple attempts.") return if response is None: return final_response = None try: for _ in range(click_attempts): buttons = response.buttons team2_button = None for row in buttons: for button in row: if button.text == "Team 2": team2_button = button break if team2_button: break if team2_button is None: await event.respond("⚠️ Couldn't find 'Team 2' button.") return click_result = await team2_button.click() if hasattr(click_result, "message"): if "Loaded team 2" in click_result.message: await event.respond("✅ Successfully loaded Team 2!") break await asyncio.sleep(click_delay) final_response = await client.get_messages(chat, limit=1) except Exception: await event.respond("⚠️ Couldn't select Team 2.") return if final_response: await event.respond(final_response[0].text)