mirror of
https://github.com/alterware/aw-bot.git
synced 2026-09-21 12:37:09 +00:00
fix: try what Claude suggested
This commit is contained in:
@@ -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!")
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user