Opcodes and Close Codes

Gateway

All Gateway events in Discord are tagged with an opcode that denotes the payload type. Your connection to the Gateway may also sometimes close. When it does, you will receive a close code that tells you what happened.

Gateway Opcodes
CodeNameActionDescription
0DispatchReceiveAn event was dispatched
1HeartbeatSend/ReceiveKeep the WebSocket connection alive
2IdentifySendStart a new session during the initial handshake
3Presence UpdateSendUpdate the client's presence
4Voice State UpdateSendJoin/leave or move between voice channels and calls
5Voice Server PingSendPing the Discord voice servers
6ResumeSendResume a previous session that was disconnected
7ReconnectReceiveYou should attempt to reconnect and resume immediately
8Request Guild MembersSendRequest information about guild members
9Invalid SessionReceiveThe session has been invalidated. You should reconnect and identify/resume accordingly
10HelloReceiveSent immediately after connecting, contains the heartbeat_interval to use
11Heartbeat ACKReceiveAcknowledge a received heartbeat
12Guild SyncSendRequest all members and presences for guilds
13Call ConnectSendRequest a private channels's pre-existing call data
14Guild SubscriptionsSendUpdate subscriptions for a guild
15Lobby ConnectSendJoin a lobby
16Lobby DisconnectSendLeave a lobby
17Lobby Voice StatesSendUpdate the client's voice state in a lobby
18Stream CreateSendCreate a stream for the client
19Stream DeleteSendEnd a client stream
20Stream WatchSendWatch a user's stream
21Stream PingSendPing a stream's voice server
22Stream Set PausedSendPause or resume a client stream
23LFG SubscriptionsSendUpdate subscriptions for an LFG lobby
24Request Guild Application CommandsSendRequest guild application commands
25Embedded Activity CreateSendLaunch an embedded activity in a voice channel or call
26Embedded Activity DeleteSendStop an embedded activity
27Embedded Activity UpdateSendUpdate an embedded activity
28Request Forum UnreadsSendRequest thread-only channel unread counts
29Remote CommandSendSend a a message to another Gateway session
30Request Deleted Entity IDsSendRequest deleted entity IDs not matching a given hash for a guild
31Request Soundboard SoundsSendRequest soundboard sounds for guilds
32Speed Test CreateSendCreate or update an RTC speed test
33Speed Test DeleteSendDelete an RTC speed test
34Request Last MessagesSendRequest last messages for a guild's channels
35Search Recent MembersSendRequest information about recently-joined guild members
36Request Channel StatusesSendRequest voice channel statuses for a guild
37Guild Subscriptions BulkSendUpdate subscriptions for multiple guilds
38Guild Channels ResyncSendResynchronize accessible guild channels
39Request Channel Member CountSendRequest the member and online count for a channel
40QoS HeartbeatSendKeep the WebSocket connection alive (with QoS metrics)
41Update Time Spent Session IDSendTrack the time spent in the current session
42Lobby Voice Server PingSendPing a lobby's voice server
43Request Channel InfoSendRequest extra voice channel fields for a guild
Gateway Close Event Codes
CodeDescriptionExplanation
4000Unknown errorWe're not sure what went wrong. Try reconnecting?
4001Unknown opcodeYou sent an invalid Gateway opcode or an invalid payload for an opcode. Don't do that!
4002Decode errorYou sent an invalid payload. Don't do that!
4003Not authenticatedYou sent us a payload prior to identifying, or this session has been invalidated
4004Authentication failedThe account token sent with your identify payload is incorrect
4005Already authenticatedYou sent more than one identify payload. Don't do that!
4006Session no longer validYour session is no longer valid
4007Invalid seqThe sequence sent when resuming the session was invalid. Reconnect and start a new session
4008Rate limitedWoah nelly! You're sending payloads too quickly. Slow it down! You will be disconnected on receiving this
4009Session timed outYour session timed out. Reconnect and start a new one
4010Invalid shardYou sent us an invalid shard when identifying
4011Sharding requiredThe session would have handled too many guilds—you are required to shard your connection in order to connect
4012Invalid API versionYou sent an invalid version for the Gateway
4013Invalid intent(s)You sent an invalid intent for a Gateway intent. You may have incorrectly calculated the bitwise value
4014Disallowed intent(s)You sent a disallowed intent for a Gateway intent. You may have tried to specify an intent that you have not enabled or are not approved for
4015Too many sessionsYou have more than the allowed amount of user account Gateway sessions open
4016Connection request canceledConsole device connection request was canceled. The Gateway session is no longer needed

Voice

The Voice Gateway has its own set of opcodes and close codes.

Voice Opcodes
CodeNameActionDescriptionFormat
0IdentifySendStart a new voice WebSocket connectionJSON
1Select ProtocolSendSelect the voice protocolJSON
2ReadyReceiveComplete the WebSocket handshakeJSON
3HeartbeatSend/ReceiveKeep the WebSocket connection aliveJSON
4Session DescriptionReceiveDescribe the sessionJSON
5SpeakingSend/ReceiveIndicate which users are speakingJSON
6Heartbeat ACKReceiveAcknowledge a received heartbeatJSON
7ResumeSendResume a previous session that was disconnectedJSON
8HelloReceiveSent immediately after connecting, contains the heartbeat_interval to useJSON
9ResumedReceiveResponse to acknowledging a successful resumeJSON
10SignalSend/ReceiveSignal WebRTC peers for P2P connectionsJSON
11ResetSend/ReceiveReset the voice connectionJSON
11Clients ConnectReceiveIndicate that clients have connected to the voice channelJSON
12VideoSend/ReceiveDescribe the video sessionJSON
13Client DisconnectReceiveIndicate that a client has disconnected from the voice channelJSON
14Session UpdateSend/ReceiveIndicate an update in session descriptionJSON
15Media Sink WantsSend/ReceiveIndicate the media streams wanted for simulcastingJSON
16Voice Backend VersionSend/ReceiveVersion information about the voice backendJSON
17Channel Options UpdateReceiveIndicate an update in voice connection propertiesJSON
18Client FlagsReceiveIndicate a client's flagsJSON
19Speed TestReceiveIndicate speed test resultsJSON
20Client PlatformReceiveIndicate the platform a client is connected onJSON
21DAVE Protocol Prepare TransitionReceiveA downgrade from the DAVE protocol is upcomingJSON
22DAVE Protocol Execute TransitionReceiveExecute a previously announced protocol transitionJSON
23DAVE Protocol Transition ReadySendAcknowledge readiness previously announced transitionJSON
24DAVE Protocol Prepare EpochReceiveA DAVE protocol version or group change is upcomingJSON
25MLS External Sender PackageReceiveCredential and public key for MLS external senderBinary
26MLS Key PackageSendMLS Key Package for pending group memberBinary
27MLS ProposalsReceiveMLS Proposals to be appended or revokedBinary
28MLS Commit WelcomeSendMLS Commit with optional MLS Welcome messagesBinary
29MLS Announce Commit TransitionReceiveMLS Commit to be processed for upcoming transitionJSON
30MLS WelcomeReceiveMLS Welcome to group for upcoming transitionBinary
31MLS Invalid Commit WelcomeSendFlag invalid commit or welcome, request re-addJSON
Voice Close Event Codes
CodeDescriptionExplanation
4001Unknown opcodeYou sent an invalid opcode
4002Failed to decode payloadYou sent a invalid payload in your identifying to the Gateway
4003Not authenticatedYou sent a payload before identifying with the Gateway
4004Authentication failedThe token you sent in your identify payload is incorrect
4005Already authenticatedYou sent more than one identify payload. Stahp
4006Session no longer validYour session is no longer valid
4009Session timeoutYour session has timed out
4011Server not foundWe can't find the server you're trying to connect to
4012Unknown protocolWe didn't recognize the protocol you sent
4014DisconnectedDisconnect individual client (you were kicked, the main Gateway session was dropped, etc.). Should not reconnect
4015Voice server crashedThe server crashed. Our bad! Try resuming
4016Unknown encryption modeWe didn't recognize your encryption
4017E2EE requiredThis channel requires a client supporting E2EE via the DAVE Protocol
4020Bad requestYou sent a malformed request
4021Rate limitedRate limit exceeded. Should not reconnect
4022DisconnectedDisconnect all clients (channel deleted, voice server changed, call ended, etc.). Should not reconnect