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
| Code | Name | Action | Description |
|---|---|---|---|
| 0 | Dispatch | Receive | An event was dispatched |
| 1 | Heartbeat | Send/Receive | Keep the WebSocket connection alive |
| 2 | Identify | Send | Start a new session during the initial handshake |
| 3 | Presence Update | Send | Update the client's presence |
| 4 | Voice State Update | Send | Join/leave or move between voice channels and calls |
| 5 | Voice Server Ping | Send | Ping the Discord voice servers |
| 6 | Resume | Send | Resume a previous session that was disconnected |
| 7 | Reconnect | Receive | You should attempt to reconnect and resume immediately |
| 8 | Request Guild Members | Send | Request information about guild members |
| 9 | Invalid Session | Receive | The session has been invalidated. You should reconnect and identify/resume accordingly |
| 10 | Hello | Receive | Sent immediately after connecting, contains the heartbeat_interval to use |
| 11 | Heartbeat ACK | Receive | Acknowledge a received heartbeat |
| 13 | Call Connect | Send | Request a private channels's pre-existing call data |
| 14 | Guild Subscriptions | Send | Update subscriptions for a guild |
| 17 | Lobby Voice States | Send | Update the client's voice state in a lobby |
| 18 | Stream Create | Send | Create a stream for the client |
| 19 | Stream Delete | Send | End a client stream |
| 20 | Stream Watch | Send | Watch a user's stream |
| 21 | Stream Ping | Send | Ping a stream's voice server |
| 22 | Stream Set Paused | Send | Pause or resume a client stream |
| 28 | Request Forum Unreads | Send | Request thread-only channel unread counts |
| 29 | Remote Command | Send | Send a a message to another Gateway session |
| 30 | Request Deleted Entity IDs | Send | Request deleted entity IDs not matching a given hash for a guild |
| 31 | Request Soundboard Sounds | Send | Request soundboard sounds for guilds |
| 32 | Speed Test Create | Send | Create or update an RTC speed test |
| 33 | Speed Test Delete | Send | Delete an RTC speed test |
| 34 | Request Last Messages | Send | Request last messages for a guild's channels |
| 35 | Search Recent Members | Send | Request information about recently-joined guild members |
| 36 | Request Channel Statuses | Send | Request voice channel statuses for a guild |
| 37 | Guild Subscriptions Bulk | Send | Update subscriptions for multiple guilds |
| 38 | Guild Channels Resync | Send | Resynchronize accessible guild channels |
| 39 | Request Channel Member Count | Send | Request the member and online count for a channel |
| 40 | QoS Heartbeat | Send | Keep the WebSocket connection alive (with QoS metrics) |
| 41 | Update Time Spent Session ID | Send | Track the time spent in the current session |
| 42 | Lobby Voice Server Ping | Send | Ping a lobby's voice server |
| 43 | Request Channel Info | Send | Request extra voice channel fields for a guild |
Gateway Close Event Codes
| Code | Description | Explanation |
|---|---|---|
| 4000 | Unknown error | We're not sure what went wrong. Try reconnecting? |
| 4001 | Unknown opcode | You sent an invalid Gateway opcode or an invalid payload for an opcode. Don't do that! |
| 4002 | Decode error | You sent an invalid payload. Don't do that! |
| 4003 | Not authenticated | You sent us a payload prior to identifying, or this session has been invalidated |
| 4004 | Authentication failed | The account token sent with your identify payload is incorrect |
| 4005 | Already authenticated | You sent more than one identify payload. Don't do that! |
| 4007 | Invalid seq | The sequence sent when resuming the session was invalid. Reconnect and start a new session |
| 4008 | Rate limited | Woah nelly! You're sending payloads too quickly. Slow it down! You will be disconnected on receiving this |
| 4009 | Session timed out | Your session timed out. Reconnect and start a new one |
| 4010 | Invalid shard | You sent us an invalid shard when identifying |
| 4011 | Sharding required | The session would have handled too many guilds—you are required to shard your connection in order to connect |
| 4012 | Invalid API version | You sent an invalid version for the Gateway |
| 4013 | Invalid intent(s) | You sent an invalid intent for a Gateway intent. You may have incorrectly calculated the bitwise value |
| 4014 | Disallowed 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 |
| 4015 | Too many sessions | You have more than the allowed amount of user account Gateway sessions open |
| 4016 | Connection request canceled | Console 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
| Code | Name | Action | Description | Format |
|---|---|---|---|---|
| 0 | Identify | Send | Start a new voice WebSocket connection | JSON |
| 1 | Select Protocol | Send | Select the voice protocol | JSON |
| 2 | Ready | Receive | Complete the WebSocket handshake | JSON |
| 3 | Heartbeat | Send/Receive | Keep the WebSocket connection alive | JSON |
| 4 | Session Description | Receive | Describe the session | JSON |
| 5 | Speaking | Send/Receive | Indicate which users are speaking | JSON |
| 6 | Heartbeat ACK | Receive | Acknowledge a received heartbeat | JSON |
| 7 | Resume | Send | Resume a previous session that was disconnected | JSON |
| 8 | Hello | Receive | Sent immediately after connecting, contains the heartbeat_interval to use | JSON |
| 9 | Resumed | Receive | Response to acknowledging a successful resume | JSON |
| JSON | ||||
| JSON | ||||
| 11 | Clients Connect | Receive | Indicate that clients have connected to the voice channel | JSON |
| 12 | Video | Send/Receive | Describe the video session | JSON |
| 13 | Client Disconnect | Receive | Indicate that a client has disconnected from the voice channel | JSON |
| 14 | Session Update | Send/Receive | Indicate an update in session description | JSON |
| 15 | Media Sink Wants | Send/Receive | Indicate the media streams wanted for simulcasting | JSON |
| 16 | Voice Backend Version | Send/Receive | Version information about the voice backend | JSON |
| JSON | ||||
| 18 | Client Flags | Receive | Indicate a client's flags | JSON |
| 19 | Speed Test | Receive | Indicate speed test results | JSON |
| 20 | Client Platform | Receive | Indicate the platform a client is connected on | JSON |
| 21 | DAVE Protocol Prepare Transition | Receive | A downgrade from the DAVE protocol is upcoming | JSON |
| 22 | DAVE Protocol Execute Transition | Receive | Execute a previously announced protocol transition | JSON |
| 23 | DAVE Protocol Transition Ready | Send | Acknowledge readiness previously announced transition | JSON |
| 24 | DAVE Protocol Prepare Epoch | Receive | A DAVE protocol version or group change is upcoming | JSON |
| 25 | MLS External Sender Package | Receive | Credential and public key for MLS external sender | Binary |
| 26 | MLS Key Package | Send | MLS Key Package for pending group member | Binary |
| 27 | MLS Proposals | Receive | MLS Proposals to be appended or revoked | Binary |
| 28 | MLS Commit Welcome | Send | MLS Commit with optional MLS Welcome messages | Binary |
| 29 | MLS Announce Commit Transition | Receive | MLS Commit to be processed for upcoming transition | JSON |
| 30 | MLS Welcome | Receive | MLS Welcome to group for upcoming transition | Binary |
| 31 | MLS Invalid Commit Welcome | Send | Flag invalid commit or welcome, request re-add | JSON |
Voice Close Event Codes
| Code | Description | Explanation |
|---|---|---|
| 4001 | Unknown opcode | You sent an invalid opcode |
| 4002 | Failed to decode payload | You sent a invalid payload in your identifying to the Gateway |
| 4003 | Not authenticated | You sent a payload before identifying with the Gateway |
| 4004 | Authentication failed | The token you sent in your identify payload is incorrect |
| 4005 | Already authenticated | You sent more than one identify payload. Stahp |
| 4006 | Session no longer valid | Your session is no longer valid |
| 4009 | Session timeout | Your session has timed out |
| 4011 | Server not found | We can't find the server you're trying to connect to |
| 4012 | Unknown protocol | We didn't recognize the protocol you sent |
| 4014 | Disconnected | Disconnect individual client (you were kicked, the main Gateway session was dropped, etc.). Should not reconnect |
| 4015 | Voice server crashed | The server crashed. Our bad! Try resuming |
| 4016 | Unknown encryption mode | We didn't recognize your encryption |
| 4017 | E2EE required | This channel requires a client supporting E2EE via the DAVE Protocol |
| 4020 | Bad request | You sent a malformed request |
| 4021 | Rate limited | Rate limit exceeded. Should not reconnect |
| 4022 | Disconnected | Disconnect all clients (channel deleted, voice server changed, call ended, etc.). Should not reconnect |