[Python] Discord voice WebSocket 4006 "Session is no longer valid" error with Nextcord...

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    I’m developing a Discord bot in Python using Nextcord, with classic prefix commands. I want the bot to join a voice channel when a user sends a command.

    However, when running the voice join command, the connection repeatedly fails with the following error in the logs:

    nextcord.errors.ConnectionClosed: Shard ID None WebSocket closed with 4006


    This error happens multiple times during reconnect attempts.

    Environment and tests

    • I get this error both on Linux and Mac.
    • I tested the bot using my 4G connection to exclude potential issues with my home network/firewall.
    • Despite that, the error still occurs.
    • I suspect that something related to the voice connection is not properly cleaned up or closed, which leads to the session becoming invalid and the connection failing.
    • Previously, the code was working correctly.
    • Sometimes it still works, for example if I haven't restarted the bot for a long time, but this is very rare.
    What I did (full code)


    import logging
    import nextcord
    from nextcord.ext import commands

    logging.basicConfig(
    format="%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)d)",
    level=logging.INFO
    )

    TOKEN=""

    intents = nextcord.Intents.default()
    intents.message_content = True
    intents.voice_states = True

    bot = commands.Bot(command_prefix="!", intents=intents)

    @bot.command()
    async def join(ctx):
    channel = ctx.author.voice.channel if ctx.author.voice else None
    if not channel:
    await ctx.send("You must be in a voice channel.")
    return

    voice_client = nextcord.utils.get(bot.voice_clients, guild=ctx.guild)

    if voice_client and voice_client.is_connected():
    await voice_client.move_to(channel)
    else:
    try:
    await channel.connect()
    except nextcord.errors.ConnectionClosed as e:
    print(f"Voice connection error: {e.code} - {e.reason}")

    bot.run(TOKEN)

    Logs when running


    2025-08-13 01:28:44,038 [INFO] logging in using static token (client.py:685)
    2025-08-13 01:28:44,043 [INFO] Allowed 1 rate limits to be garbage collected. (http.py:712)
    2025-08-13 01:28:44,495 [INFO] Shard ID None has sent the IDENTIFY payload. (gateway.py:435)
    2025-08-13 01:28:44,685 [INFO] Shard ID None has connected to Gateway: [...] (gateway.py:526)
    2025-08-13 01:28:58,632 [INFO] Connecting to voice... (voice_client.py:346)
    2025-08-13 01:28:58,633 [INFO] Starting voice handshake... (connection attempt 1) (voice_client.py:328)
    2025-08-13 01:28:58,932 [INFO] Voice handshake complete. Endpoint found c-cdg15-a5a2903c.discord.media (voice_client.py:332)
    2025-08-13 01:28:59,044 [ERROR] Failed to connect to voice... Retrying... (voice_client.py:374)
    Traceback (most recent call last):
    ...
    nextcord.errors.ConnectionClosed: Shard ID None WebSocket closed with 4006
    2025-08-13 01:29:00,051 [INFO] The voice handshake is being terminated for Channel ID ... (voice_client.py:321)
    ... (repeated attempts with the same 4006 error)


    Thanks a lot for your help!

    Continue reading...

Compartilhe esta Página