Running Playwright browser and Discord bot in the same program

79 Views Asked by At

My project needs to interact with a headless web browser (using Playwright) by using commands given to the program through a Discord bot (using Pycord). My problem is that both Playwright and Pycord start their own event loops and I can't figure out how to have them working together. My idea is to have the web browser running constantly and I can interact with it using commands passed through the Discord bot. I know how to create the Discord bot part and Playwright part individually and they work perfectly fine but I cannot put them together into one.

My current code looks something like this:

def get_round_image(page: Page, token: str, outfile: str=None):
    ...

def guess(page: Page, token: str, lat: float, lng: float):
    ...

def get_game_info(page: Page, token: str):
    ...

def create_game(page: Page):
    ...
# ...
with sync_playwright() as p:
    ctx = p.webkit.launch_persistent_context("./userdata/", headless=True, viewport={"width": 1920, "height": 1080})
    page = ctx.new_page()

# and the discord part is fairly generic as well
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.command()
async def guess(ctx: commands.Context, *args):
    ...

bot.run(DISCORD_TOKEN)

Obviously it doesn't work but I put it here to give an idea of the structure of the code.

0

There are 0 best solutions below