discordjs/discord.js · error · RangeError

Unsupported encryption method: ${encryptionMode}

Error message

Unsupported encryption method: ${encryptionMode}

What it means

encryptOpusPacket throws this RangeError when the voice gateway has selected an encryption mode (from the `modes` list in the voice server update) that the library's switch statement does not recognize. By design this branch should be unreachable because the mode is negotiated against the supported list, so it indicates a mismatch between what the gateway advertised and what this library version supports. It almost always means the library is outdated or the gateway is behaving unexpectedly.

Source

Thrown at packages/voice/src/networking/Networking.ts:810

				encrypted = Buffer.concat([cipher.update(packet), cipher.final(), cipher.getAuthTag()]);

				return [encrypted, noncePadding];
			}

			case 'aead_xchacha20_poly1305_rtpsize': {
				encrypted = secretbox.methods.crypto_aead_xchacha20poly1305_ietf_encrypt(
					packet,
					additionalData,
					connectionData.nonceBuffer,
					secretKey,
				);

				return [encrypted, noncePadding];
			}

			default: {
				// This should never happen. Our encryption mode is chosen from a list given to us by the gateway and checked with the ones we support.
				throw new RangeError(`Unsupported encryption method: ${encryptionMode}`);
			}
		}
	}
}

View on GitHub (pinned to a81ed8a306)

Solutions

  1. Update @discordjs/voice (and discord.js if applicable) to the latest version so the switch supports the encryption mode your gateway negotiated.
  2. Log the negotiated encryptionMode from the voice server update to identify which unsupported mode was selected.
  3. If you intentionally control the modes list (e.g. in a mock), restrict it to supported values: aead_xchacha20_poly1305_rtpsize, xsalsa20_poly1305_lite, xsalsa20_poly1305_suffix, xsalsa20_poly1305.
  4. Report the issue to the library maintainers if the gateway advertises a legitimate mode the latest version rejects.

Example fix

// before
const connection = joinVoiceChannel({ ... }); // library v0.13 negotiating aead_xchacha20_poly1305_rtpsize fails
// after
// npm i @discordjs/voice@latest
// (upgrade ensures the new mode is handled in encryptOpusPacket's switch)
Defensive patterns

Strategy: try-catch

Validate before calling

// After receiving the voice server update, before playing:
const SUPPORTED = ['aead_xchacha20_poly1305_rtpsize', 'xsalsa20_poly1305_lite', 'xsalsa20_poly1305_suffix', 'xsalsa20_poly1305'];
if (!SUPPORTED.includes(encryptionMode)) throw new Error(`Library does not support negotiated mode: ${encryptionMode} — update @discordjs/voice`);

Type guard

function isSupportedEncryptionMode(m: string): m is 'aead_xchacha20_poly1305_rtpsize' | 'xsalsa20_poly1305_lite' | 'xsalsa20_poly1305_suffix' | 'xsalsa20_poly1305' {
  return ['aead_xchacha20_poly1305_rtpsize', 'xsalsa20_poly1305_lite', 'xsalsa20_poly1305_suffix', 'xsalsa20_poly1305'].includes(m);
}

Try / catch

try {
  connection.playResource(resource);
} catch (e) {
  if (e instanceof RangeError && e.message.startsWith('Unsupported encryption method')) {
    connection.destroy(); // renegotiate / upgrade library before retrying
  } else throw e;
}

Prevention

When it happens

Trigger: Calling VoiceConnection.playOpus/createAudioPacket while the negotiated encryptionMode is a string not handled by the switch in encryptOpusPacket (e.g. a new mode like aead_xchacha20_poly1305_rtpsize supported by the gateway but not by an old library version, or a corrupted mode value from the gateway).

Common situations: Running an outdated @discordjs/voice version after Discord introduced a new encryption mode; a malicious/spoofed gateway returning a bogus mode; manual mocking of gateway signals in tests with an unknown mode string.

Related errors


AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30). Data as JSON: /api/errors/e32abc9d2b5cf84d. Report an issue: GitHub.