Discord bot integration with Pygame window controls

I’m trying to create a Discord bot that responds when I interact with buttons in a Pygame window. The idea is to have a GUI with clickable buttons that trigger the bot to send messages or play music in Discord channels.

import discord
import pygame
import asyncio

bot = discord.Client()
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

current_track = 0

@bot.event
async def on_message(msg):
    if msg.author == bot.user:
        return
    
    if msg.content.startswith('!play'):
        if current_track == 1:
            await msg.channel.send(';;play https://example1.com')
        elif current_track == 2:
            await msg.channel.send(';;play https://example2.com')
        else:
            await msg.channel.send('No track selected')

def create_gui():
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Bot Controller')
    
    def draw_button(pos_x, pos_y, width, height, hover_color, default_color, track_id):
        mouse_pos = pygame.mouse.get_pos()
        mouse_click = pygame.mouse.get_pressed()
        
        if pos_x < mouse_pos[0] < pos_x + width and pos_y < mouse_pos[1] < pos_y + height:
            pygame.draw.rect(screen, hover_color, (pos_x, pos_y, width, height))
            if mouse_click[0] and track_id > 0:
                global current_track
                current_track = track_id
        else:
            pygame.draw.rect(screen, default_color, (pos_x, pos_y, width, height))
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        screen.fill(WHITE)
        draw_button(100, 80, 60, 60, YELLOW, BLUE, 1)
        draw_button(200, 80, 60, 60, YELLOW, BLUE, 2)
        
        pygame.display.flip()
    
    pygame.quit()

@bot.event
async def on_ready():
    print(f'Bot connected: {bot.user}')
    create_gui()

bot.run('your_token_here')

I’m having trouble getting the bot to respond when I click the Pygame buttons. The GUI shows up fine but the Discord integration isn’t working as expected. Any ideas on how to properly connect the button clicks to Discord bot actions?

Your bot’s broken because you’re mixing pygame’s synchronous operations with Discord’s async framework. When on_ready calls create_gui(), that infinite while loop freezes the entire Discord client.

Don’t try forcing pygame into async - run them as separate processes instead. Use IPC like named pipes or sockets to connect them. I did this for my streaming setup where a pygame control panel sends commands to a separate Discord bot.

Or go simple: have pygame write button states to a JSON file that your Discord bot checks periodically. Both components stay independent without threading mess or async headaches.

Your GUI is blocking everything else. Use asyncio instead of threads - make your pygame loop async and add await asyncio.sleep(0) in the while loop so other coroutines can run. Or ditch the blocking approach and handle pygame events non-blocking.

Your create_gui() function is blocking the Discord bot’s event loop. When you call it in on_ready, it hits that while loop and never returns - so the bot can’t process Discord messages anymore. I hit the same issue building a control panel for my music bot. You need to separate the GUI from the bot’s main thread. Run the Pygame window in its own thread using threading.Thread, and use queue.Queue to pass button clicks between the GUI thread and Discord bot. That way the bot keeps handling Discord events while your GUI runs separately. Also throw in pygame.time.Clock() to control your GUI’s frame rate instead of letting it run wild.