Перейти к содержимому

Opcodes and close codes

Every Gateway payload has an opcode, and the Gateway ends a connection with a close code.

OpNameDescription
0DispatchServer to client. An event with its name in t and a sequence in s
1HeartbeatBoth ways. The client sends its last sequence or null in d. Respond to a server heartbeat at once
2IdentifyClient to server. Starts a session with an Identify object
3Presence UpdateClient to server. A Presence Update object
4Voice State UpdateClient to server. A Voice State Update object
5Voice Server PingReserved. The Gateway closes a client that sends it
6ResumeClient to server. Restores a session with a Resume object
7ReconnectServer to client, with no d, before a close with 4000. Reconnect and resume, or Identify again when the socket had no session yet
8Request Guild MembersClient to server. A Request Guild Members object
9Invalid SessionServer to client, always with d: false. Start a new session with Identify
10HelloServer to client, with heartbeat_interval, as soon as the WebSocket opens
11Heartbeat ACKServer to client, with no d, for each accepted heartbeat
12Gateway ErrorServer to client. A refused voice join or update
13Voice SignalClient to server. A Voice Signal object for one other voice connection
14Lazy RequestClient to server. A Lazy Request object
15Request Guild CountsClient to server. A Request Guild Counts object
16Request Channel Member CountsClient to server. A Request Channel Member Counts object
28Request Forum UnreadsClient to server. A Request Forum Unreads object

Ignore a server opcode that isn’t in this table.

Opcode 12 tells the session that sent a Voice State Update that Squad refused it. It has no s or t, and the connection stays open.

FieldTypeDescription
codestringThe refusal code
messagestringA readable description for logs
{
"op": 12,
"d": {
"code": "VOICE_P2P_CONSENT_REQUIRED",
"message": "This call is peer-to-peer and requires agreement to join"
}
}

A refused voice join or update gets one of these codes. Show the user why it failed.

CodeMessageCause
VOICE_PERMISSION_DENIEDMissing voice permissionsThe member lacks VIEW_CHANNEL or CONNECT
VOICE_MEMBER_TIMED_OUTVoice member is timed outThe member has an active timeout
VOICE_CHANNEL_FULLVoice channel is fullThe channel is at its user_limit, at 25 users while anyone has a camera on, or the peer-to-peer call is full
VOICE_CONNECTION_LIMIT_REACHEDVoice connection limit reachedThe user has voice_connection_limit connections in the channel
VOICE_CAMERA_USER_LIMITToo many users in channel to enable camera25 members already have a camera on
VOICE_UNCLAIMED_ACCOUNTClaim your account to join voiceAn unclaimed account joins a one-on-one call or a voice channel in a guild it doesn’t own
VOICE_ACCOUNT_VERIFICATION_REQUIREDConfirm an email address to join voiceThe instance’s interaction_tier is enforced and the account isn’t verified
VOICE_E2EE_REQUIREDThis voice channel is end-to-end encrypted and requires a client with E2EE supportVoice encryption is on, everyone in the channel is e2ee_capable, and the session (not a bot) didn’t identify with it
VOICE_P2P_CONSENT_REQUIREDThis call is peer-to-peer and requires agreement to joinThe call is peer-to-peer and the join didn’t send p2p: true. Ask the user, then join again with p2p: true, as in peer-to-peer voice
VOICE_P2P_UNAVAILABLEPeer-to-peer voice is unavailable for this joinThe Gateway declined peer-to-peer for this join
VOICE_TOKEN_FAILEDFailed to obtain voice tokenSquad couldn’t issue a voice token. Try the join again later

Squad drops other invalid updates with no Gateway Error, such as an unknown or foreign connection_id, a viewer_stream_keys entry that doesn’t match the update, or a channel or member that doesn’t exist.

CodeNameMeaning
4000Unknown errorOpcode 7 came just before it, session creation failed, or the session in Resume did not respond
4001Unknown opcodeAn undefined or server-only opcode, a payload other than Identify or Resume with no d, or opcode 28 without thread access or an object d
4002Decode errorThe payload size, compression, encoding or command fields are invalid
4003Not authenticatedA command arrived before Identify or Resume, or no Identify or Resume arrived within one heartbeat interval of Hello
4004Authentication failedThe token is invalid, or it doesn’t own the session in Resume
4005Already authenticatedIdentify or Resume arrived while a session was attached, or with no d
4007Invalid sequenceHeartbeat d isn’t an integer or null, or Resume seq is out of range
4008Rate limitedA connection, payload, source IP or session budget was exceeded
4009Session timeoutNo heartbeat was accepted within the heartbeat timeout
4010Invalid shardThe Identify shard isn’t a valid [shard_id, shard_count] pair1
4011Sharding requiredMore than 2,500 guilds belong to one bot session2
4012Invalid API versionThe v connection parameter is missing or isn’t 1

1 shard_count is 1 to 16,384 and shard_id is a non-negative integer below it

2 The Gateway counts guilds after the shard filter. Raise shard_count until each shard has at most 2,500.

If the connection had a session, you can Resume it for 60,000 ms after a network failure or any close except 1000 and 1001. A 1000 or 1001 close from the client ends the session right away. Close with another code, such as 4000, when you plan to resume. When a Resume closes with 4004 or 4007, the session stays and you can try again. Resume only replays events still in the replay buffer.

After 4004, 4010 or 4012, change the token, shard pair or version before you reconnect.

Heartbeat closes with 4007 only after a session is attached.

Resume closes with 4007 when seq is negative, above the last sequence the session dispatched or below the last acknowledged sequence. A seq inside those bounds but older than the oldest event in the replay buffer gets opcode 9 with d: false, and the session stays available.

Every close with a 4000-range code has a reason. Branch on the code and log the reason. The reasons below say more than their code. Other codes use the name of the close code as the reason.

ReasonCodeCause
Failed to start session4000Session creation failed for an unclassified reason
Session unavailable4000The session did not respond during Resume
Session drain requested; reconnect to continue4000Opcode 7 came just before the close
Compression failed: zstd-stream4002Hello could not be compressed on a zstd connection
Payload too large4002A message is over 16,384 bytes, on the wire or after decompression
Decompression failed4002A compressed message could not be decompressed
Decode failed4002The payload isn’t valid JSON or isn’t an object
Invalid payload4002The decoded object has no op
Invalid identify payload4002Identify is missing token or properties, or has an invalid properties, ignored_events or flags, such as more than 256 ignored_events or a negative flags
Invalid resume payload4002Resume is missing token, session_id or an integer seq, or a field has the wrong type
Invalid presence payload4002Presence Update is not an object, has no status, or has a non-empty string status outside the set
Invalid voice payload4002Voice State Update d is not an object
Invalid token4004The Identify token is not a string of 1 to 256 bytes with at least one visible character, the backend rejects it, or the Resume token doesn’t own the session
Too many connections4008The source IP has 256 concurrent connections
Rate limited4008The WebSocket, session or source IP payload budget was exceeded, or more than 64 commands queued before Ready or Resumed
Too many sessions4008The user already has 100 live sessions. Bots have no limit
Identify rate limited4008The source IP sent more than 300 Identify and Resume commands in a 60-second window
Resume rate limited4008The same per-IP budget, hit by a Resume
Heartbeat timeout4009No heartbeat was accepted within the heartbeat timeout

The Gateway drops some over-budget commands, such as Presence Update and Voice Signal, without closing the connection.