So I made a piece of code with discord.py that plays an audio file when a user joins a voice channel. The problem is that I get the warning above. I think this is because when the bot joins it sees itself joining and can't join again. I have tried to solve this with the commented line of code, but that doesn't seem to work. The full error will be below the code. I'll only paste the code needed for this aspect of my bot, because the problem lies with this piece of code and not the rest. I also get this websocket closing warning, but do'nt know if it's important.
Code:
@bot.event
async def on_voice_state_update(member: discord.Member, before, after):
vc_before = before.channel
vc_after = after.channel
path_mp3 = "Tutturuu.mp3"
path_ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
if vc_before == vc_after:
return
if vc_before is None: # and member.display_name != discord.ClientUser.name:
channel = member.voice.channel
vc = await channel.connect()
time.sleep(.5)
vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
with audioread.audio_open(path_mp3) as f:
time.sleep(f.duration)
await vc.disconnect()
elif vc_after is None:
return
else:
channel = member.voice.channel
vc = await channel.connect()
time.sleep(.5)
vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
with audioread.audio_open(path_mp3) as f:
time.sleep(f.duration)
await vc.disconnect()
Full error: Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) File "C:/Users/yorbe/OneDrive/Documenten/Folders/Gerbinbot_3000/main.py", line 109, in on_voice_state_update vc = await channel.connect() File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\abc.py", line 1115, in connect raise ClientException('Already connected to a voice channel.') discord.errors.ClientException: Already connected to a voice channel. websocket connection is closing. websocket connection is closing. websocket connection is closing.