Integrating Pygame with a Discord Bot

import discord
import random
import pygame
import time

bot = discord.Client()
white_color = (255, 255, 255)
timer = pygame.time.Clock()
green_color = (0, 255, 0)
red_color = (255, 0, 0)
black_color = (0, 0, 0)

current_song = 0

@bot.event
async def on_message(msg):
    if msg.author == bot.user:
        return
    if msg.content.startswith(''):
        while True:
            if current_song == 1:
                await bot.send_message(msg.channel, ';;play https://www.youtube.com/watch?v=cUbFzEMQ2Fs')
            elif current_song == 2:
                await bot.send_message(msg.channel, ';;play https://www.youtube.com/watch?v=YlomIQF2zbI')
            else:
                await bot.send_message(msg.channel, "Hello!")
                pygame.quit()


def create_window():
    pygame.init()
    display = pygame.display.set_mode((500, 500))

    def draw_button(x, y, width, height, active_color, inactive_color, song_choice):
        mouse_pos = pygame.mouse.get_pos()
        mouse_click = pygame.mouse.get_pressed()
        if x + width > mouse_pos[0] > x and y + height > mouse_pos[1] > y:
            pygame.draw.rect(display, active_color, (x, y, width, height))
            if mouse_click[0] == 1 and song_choice != 0:
                pass  # Do something when the button is pressed
        else:
            pygame.draw.rect(display, inactive_color, (x, y, width, height))

    while True:
        events = pygame.event.get()
        display.fill(white_color)
        draw_button(50, 50, 50, 50, red_color, green_color, 1)
        draw_button(50, 120, 50, 50, red_color, green_color, 2)

        pygame.display.update()
        timer.tick(60)

@bot.event
async def on_ready():
    print('Bot is online as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
    create_window()

bot.run('your_token_here')

I’m currently facing some difficulties and would appreciate any guidance on how to implement functionality so that the bot responds when I click a button on the Pygame interface. Thank you for any assistance!

You can consider using asyncio.run_coroutine_threadsafe to interact between Pygame and Discord. When a button is clicked, trigger an asyncio task that waits for messages or performs actions in the Discord bot context. This approach allows Pygame’s loop to communicate changes efficiently, although it can be tricky with thread safety and async nature.

If you’re looking to synchronize Pygame events like button presses with the Discord bot’s functionalities, one way I’ve found effective is using threading. Since Pygame runs in a loop, you can launch a separate thread for handling Discord bot events while keeping Pygame’s event loop active. This way, the Pygame interface can update the shared state or send signals that the Discord bot checks.

When you detect a button press in your draw_button logic, change a global variable to a specific value which reflects the button action. This global state needs to be periodically polled by asynchronous tasks running in the Discord bot’s scope. However, keep in mind Python’s GIL as it might affect performance so test this method thoroughly in your setup to see if it meets your performance requirements.