skip to Main Content

I’m trying to make a bot that can read all messages sent in a group, and play the game. Something close to Twitch Plays Pokemon but for every game.

Right now I have this code, the bot works fine in Telegram but once it get the window focused and should move it doesn’t…

import logging
import pyautogui  # Importa la biblioteca pyautogui
import pygetwindow as gw
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

# Configurar el registro
logging.basicConfig(level=logging.INFO)

TOKEN = "TOKEN"

MOVEMENTS = {
    "up": "w",
    "Up": "W",
    "down": "s",
    "left": "a",
    "right": "d"
}

def focus_pokemon_window(window_title):
    try:
        window = gw.getWindowsWithTitle(window_title)[0]
        window.restore()
        window.activate()
    except IndexError:
        print(f"No se encontró ninguna ventana con el título '{window_title}'.")

def move_character(direction):
    focus_pokemon_window("Pokemon - FireRed Version (USA) [Gameboy Advance] - BizHawk")
    pyautogui.hotkey(MOVEMENTS[direction]) 


def start(update: Update, context: CallbackContext):
    pass

def handle_text(update: Update, context: CallbackContext):
    bot_username = context.bot.username
    text = update.message.text.lower()

    if f"@{bot_username.lower()}" not in text:
        return

    # Eliminar la mención del bot del texto
    text = text.replace(f"@{bot_username.lower()}", "").strip()

    if text in MOVEMENTS:
        move_character(text)
    else:
        update.message.reply_text("Comando desconocido. Por favor, envía una dirección válida (up, down, left, right).")

def main():
    updater = Updater(TOKEN)
    dispatcher = updater.dispatcher

    # Agregar manejadores de comandos y mensajes
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_text))

    # Iniciar el bot
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

the game will be streamed by OBS privately.

I tried all kind of solutions but seems that when windows pop-ups it doesn’t make any movement.

2

Answers


  1. Chosen as BEST ANSWER

    The main problem for my code was the fact of using a old import.

    import pyautogui
    

    Instead I used pydirectinput.

    import pydirectinput
    

    After I just replaced all the functions.


  2. In my experience, anything that involves pulling up a specific window by name using something like pygetwindow or pywin32 doesn’t really work consistently, sometimes works and sometimes doesn’t. I blame Microsoft for changing the Windows API constantly without much documentation. You could try using Pyautogui to get the window. To click an image in Pyautogui, just use pyautogui.click(x = image_name). Make sure that your screencap is accurate, if you’re on Windows hit the Windows button, then type "snip". Drag over the part you want to click then click save. If you can’t crop the image small enough, download GIMP and crop it with that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search