detritus

Socketclass

A connection to Discord's gateway. Handles identifying, heartbeats, resuming and compression, and emits every dispatch as a raw packet.

On this page
import { Gateway } from 'detritus-client-socket';

Gateway.Socket;
EventEmitterEventSpewerSocket
35 properties45 methodssrc/gateway.ts:102

Constructor

Properties

_heartbeat
{ ack: boolean; interval: Interval; intervalTime: number | null; lastAck: number | null; lastSent: number | null; timeout: Timeout }
_helloTimeout
Timeout
_lastSocket
BaseSocket | null
Default null
_packetImmediate
Immediate | null
Default null
_packetQueue
any[]
Default []
_reconnectTimeout
Timeout
_resumeUrlSocket
BaseSocket | null
Default null
autoReconnect
boolean
bucket
Bucket
capabilities
number
Default 0
compress
decompressor
Decompressor | null
disabledEvents
string[]
discordTrace
any[]
Default []
encoding
guildSubscriptions
boolean
identifyProperties
intents
number
Default GATEWAY_INTENTS_ALL_UNPRIVILEGED
killed
boolean
Default false
largeThreshold
number
mediaGateways
presence
Default null
reconnectDelay
number
reconnectMax
number
reconnects
number
Default 0
resumeGatewayUrl
string | null
Default null
resuming
boolean
Default false
sequence
number
Default 0
sessionId
string | null
Default null
shardCount
number
shardId
number
socket
BaseSocket | null
Default null
state
readonly
Default SocketStates.CLOSED
token
string
url
URL | null
Default null
userId
string | null
Default null
voiceReconnectDelay
number
voiceReconnectMax
number

Accessors

closed
boolean
get
closing
boolean
get
connected
boolean
get
connecting
boolean
get

Methods

decode()

src/gateway.ts:572
decode(data: any, uncompressed?: boolean): any
data
any
uncompressed?
boolean
Default false

disconnect()

src/gateway.ts:601
disconnect(code?: number, reason?: string): void

Closes the socket. 1000 (the default) drops the session and doesn't reconnect, internal codes (SocketInternalCloseCodes) keep the session and reconnect to resume

code?
number
Default SocketCloseCodes.NORMAL
reason?
string

emitPackets()

src/gateway.ts:703
emitPackets(): void

Emits the queued raw packets in order, one immediate for every packet handled in the same tick

guildStreamCreate()

src/gateway.ts:1037
guildStreamCreate(
  guildId: string,
  options: { channelId: string; preferredRegion?: string },
  callback?: Function,
): void
guildId
string
options
{ channelId: string; preferredRegion?: string }
options.channelId
string
options.preferredRegion?
string
callback?
Function

handle()

src/gateway.ts:622
handle(data: any, uncompressed?: boolean): void
data
any
uncompressed?
boolean
Default false

heartbeat()

src/gateway.ts:961
heartbeat(fromInterval?: boolean, requested?: boolean): void
fromInterval?
boolean
Default false
requested?
boolean
Default false

lobbyConnect()

src/gateway.ts:1053
lobbyConnect(lobbyId: string, lobbySecret: string, callback?: Function): void

lobbyDisconnect()

src/gateway.ts:1064
lobbyDisconnect(lobbyId: string, callback?: Function): void

lobbyVoiceStatesUpdate()

src/gateway.ts:1073
lobbyVoiceStatesUpdate(
  voiceStates: { lobbyId: string; selfDeaf: boolean; selfMute: boolean }[],
  callback?: Function,
): void

on()

src/gateway.ts:1335
on(event: string | symbol, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a

Since v0.1.101

event
string | symbol
listener
(...args: any[]) => void

The callback function

on(
  event: CLOSE | "close",
  listener: (payload: { code: number; reason: string }) => any,
): this
on(event: KILLED | "killed", listener: (payload: { error?: Error }) => any): this
on(event: OPEN | "open", listener: (target: BaseSocket) => any): this
on(event: PACKET | "packet", listener: (packet: Packet) => any): this
on(event: READY | "ready", listener: () => any): this
on(event: SOCKET | "socket", listener: (socket: BaseSocket) => any): this
on(
  event: STATE | "state",
  listener: (__namedParameters: { state: SocketStates }) => any,
): this
on(event: WARN | "warn", listener: (error: Error) => any): this
Overrides EventSpewer.on

onClose()

src/gateway.ts:814
onClose(target: BaseSocket, code?: number, reason?: string): void

onIdentifyCheck()

optional src/gateway.ts:158
onIdentifyCheck(): boolean | Promise<boolean>

onMessage()

src/gateway.ts:857
onMessage(target: BaseSocket, data: any, isBinary?: boolean): void

requestApplicationCommands()

src/gateway.ts:1138
requestApplicationCommands(
  guildId: string,
  options: { applicationId?: string; applications: boolean; limit?: number; nonce: string; offset?: number; query?: string },
): void
guildId
string
options
{ applicationId?: string; applications: boolean; limit?: number; nonce: string; offset?: number; query?: string }
options.applicationId?
string
options.applications
boolean
options.limit?
number
options.nonce
string
options.offset?
number
options.query?
string

requestChannelInfo()

src/gateway.ts:1091
requestChannelInfo(guildId: string, fields?: string[], callback?: Function): void

requestGuildMembers()

src/gateway.ts:1102
requestGuildMembers(
  guildIds: string | string[],
  options: { limit: number; nonce?: string; presences?: boolean; query?: string; userIds?: string[] },
  callback?: Function,
): void
guildIds
string | string[]
options
{ limit: number; nonce?: string; presences?: boolean; query?: string; userIds?: string[] }
options.limit
number
options.nonce?
string
options.presences?
boolean
options.query?
string
options.userIds?
string[]
callback?
Function

requestSoundboardSounds()

src/gateway.ts:1129
requestSoundboardSounds(guildIds: string | string[], callback?: Function): void

send()

src/gateway.ts:895
send(op: number, d: any, callback?: Function, priority?: boolean): void
op
number
d
any
callback?
Function
priority?
boolean
Default false

streamDelete()

src/gateway.ts:1167
streamDelete(streamKey: string, callback?: Function): void

streamSetPaused()

src/gateway.ts:1185
streamSetPaused(streamKey: string, paused: boolean, callback?: Function): void

updateGuildSubscriptions()

src/gateway.ts:1212
updateGuildSubscriptions(
  guildId: string,
  options?: { activities?: boolean; channels?: { [channelId: string]: […, …][] }; members?: string[]; typing?: boolean },
  callback?: Function,
): void
guildId
string
options?
{ activities?: boolean; channels?: { [channelId: string]: [number, number][] }; members?: string[]; typing?: boolean }
options.activities?
boolean
options.channels?
{ [channelId: string]: [number, number][] }
options.members?
string[]
options.typing?
boolean
callback?
Function

voiceConnect()

async src/gateway.ts:1256
voiceConnect(
  guildId?: string | null,
  channelId?: string | null,
  options?: { forceMode?: string; receive?: boolean; selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean; timeout?: number; video?: boolean },
): Promise<Socket | null>
guildId?
string | null
channelId?
string | null
options?
{ forceMode?: string; receive?: boolean; selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean; timeout?: number; video?: boolean }
options.forceMode?
string
options.receive?
boolean
options.selfDeaf?
boolean
options.selfMute?
boolean
options.selfVideo?
boolean
options.timeout?
number
options.video?
boolean

voiceStateUpdate()

src/gateway.ts:1235
voiceStateUpdate(
  guildId?: string | null,
  channelId?: string | null,
  options?: { preferredRegion?: string; selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean },
  callback?: Function,
): void
guildId?
string | null
Default null
channelId?
string | null
Default null
options?
{ preferredRegion?: string; selfDeaf?: boolean; selfMute?: boolean; selfVideo?: boolean }
options.preferredRegion?
string
options.selfDeaf?
boolean
options.selfMute?
boolean
options.selfVideo?
boolean
callback?
Function