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?