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

时间:2020-11-06 13:43:07

标签: python discord.py

因此,我用discord.py编写了一段代码,当用户加入语音通道时播放音频文件。问题是我收到上面的警告。我认为这是因为当漫游器加入时,它会看到自己加入并且无法再次加入。我试图用注释的代码行来解决这个问题,但这似乎行不通。完整的错误将在代码下方。我只会粘贴机器人这一方面所需的代码,因为问题出在这部分代码,而不是其余部分。我也收到此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()

完整错误: 忽略on_voice_state_update中的异常 追溯(最近一次通话): _run_event中的第333行“ C:\ Users \ yorbe \ OneDrive \ Documenten \ Folders \ Gerbinbot_3000 \ venv \ lib \ site-packages \ discord \ client.py” 等待科罗(* args,** kwargs) 在on_voice_state_update中,文件“ C:/Users/yorbe/OneDrive/Documenten/Folders/Gerbinbot_3000/main.py”第109行 vc =等待channel.connect() 1115行中的文件“ C:\ Users \ yorbe \ OneDrive \ Documenten \ Folders \ Gerbinbot_3000 \ venv \ lib \ site-packages \ discord \ abc.py” 引发ClientException(“已连接到语音通道。”) discord.errors.ClientException:已连接到语音通道。 websocket连接正在关闭。 websocket连接正在关闭。 websocket连接正在关闭。

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了解决方法。如果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()