From 6829f551f55aa806db0b4e15882484b48e61a477 Mon Sep 17 00:00:00 2001 From: diamante0018 Date: Sat, 29 Aug 2026 11:51:30 +0200 Subject: [PATCH] fix: try what Claude suggested --- bot/events.py | 4 ++++ bot/events_handlers/voice_events.py | 34 ++++++++++++++--------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/bot/events.py b/bot/events.py index ce72cbe..69ee01e 100644 --- a/bot/events.py +++ b/bot/events.py @@ -40,4 +40,8 @@ async def setup(bot): async def on_message_edit(before, after): await handle_message_edit(before, after, bot) + @bot.event + async def on_voice_state_update(member, before, after): + await handle_voice_state_update(member, before, after, bot) + logger.info("Events extension loaded!") diff --git a/bot/events_handlers/voice_events.py b/bot/events_handlers/voice_events.py index 48e9759..732018e 100644 --- a/bot/events_handlers/voice_events.py +++ b/bot/events_handlers/voice_events.py @@ -5,30 +5,30 @@ import discord from bot.log import logger MP3_PATH = "sounds/hello.mp3" +_locks: dict[int, asyncio.Lock] = {} async def handle_voice_state_update(member, before, after, bot): - # Ignore bot users if member.bot: return - # Check if the member joined a new voice channel if after.channel and (before.channel != after.channel): voice_channel = after.channel + guild_id = voice_channel.guild.id + lock = _locks.setdefault(guild_id, asyncio.Lock()) - try: - # Join the voice channel - vc = await voice_channel.connect() + if lock.locked(): + return # a connect for this guild is already in flight - # Play the MP3 file - vc.play(discord.FFmpegPCMAudio(MP3_PATH)) - - # Wait for playback to finish - while vc.is_playing(): - await asyncio.sleep(1) - - # Disconnect - await vc.disconnect() - - except Exception as e: - logger.error("Error while connecting to a voice channel: %s", e) + async with lock: + # bail if we're already connected to this guild's voice + if voice_channel.guild.voice_client is not None: + return + try: + vc = await voice_channel.connect() + vc.play(discord.FFmpegPCMAudio(MP3_PATH)) + while vc.is_playing(): + await asyncio.sleep(1) + await vc.disconnect() + except Exception as e: + logger.error("Error while connecting to a voice channel: %s", e)