Python discord.errors.ClientException:已连接到语音频道

Python discord.errors.ClientException:已连接到语音频道,python,discord.py,Python,Discord.py,因此,我用discord.py编写了一段代码,当用户加入语音频道时,它会播放音频文件。问题是我得到了上面的警告。我认为这是因为当机器人加入时,它看到自己加入了,不能再加入了。我试图用注释过的代码行来解决这个问题,但这似乎不起作用。完整错误将位于代码下方。我将只粘贴我的bot的这方面所需的代码,因为问题在于这段代码,而不是其余部分。我也收到这个websocket关闭警告,但不知道它是否重要 代码: @bot.event async def on_voice_state_update(member:

因此,我用discord.py编写了一段代码,当用户加入语音频道时,它会播放音频文件。问题是我得到了上面的警告。我认为这是因为当机器人加入时,它看到自己加入了,不能再加入了。我试图用注释过的代码行来解决这个问题,但这似乎不起作用。完整错误将位于代码下方。我将只粘贴我的bot的这方面所需的代码,因为问题在于这段代码,而不是其余部分。我也收到这个websocket关闭警告,但不知道它是否重要

代码:

@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()
完全错误: 正在忽略语音状态更新中的异常 回溯(最近一次呼叫最后一次): 文件“C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot\u 3000\venv\lib\site packages\discord\client.py”,第333行,在运行事件中 等待coro(*args,**kwargs) 文件“C:/Users/yorbe/OneDrive/Documenten/Folders/Gerbinbot\u 3000/main.py”,第109行,处于语音更新状态 vc=等待通道。连接() 文件“C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot\u 3000\venv\lib\site packages\discord\abc.py”,第1115行,在connect中 引发ClientException('已连接到语音频道') discord.errors.ClientException:已连接到语音频道。 websocket连接正在关闭。 websocket连接正在关闭。
websocket连接正在关闭。

好的,我自己找到了解决方法。如果vc_before==vc_after,只需在前面添加此代码(请参见页眉之间)

@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"
    -----------------
    for role in member.roles:
        if role.name == "Name of the role that your bot made when it joined the server, 
usually its own name":
            return
    -----------------
    if vc_before == vc_after:
        return
    if vc_before is None:
        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()