fix: try what Claude suggested

This commit is contained in:
2026-08-29 11:51:30 +02:00
parent 4c37d2eed3
commit 6829f551f5
2 changed files with 21 additions and 17 deletions
+4
View File
@@ -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!")
+17 -17
View File
@@ -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)