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
32 properties44 methodssrc/gateway.ts:91

Constructor

Properties

_heartbeat
{ ack: boolean; interval: Interval; intervalTime: number | null; lastAck: number | null; lastSent: number | null }
_lastSocket
BaseSocket | null
Default null
_packetImmediate
Immediate | null
Default null
_packetQueue
any[]
Default []
_reconnectTimeout
Timeout
autoReconnect
boolean
bucket
Bucket
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

Accessors

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

Methods

callConnect()

src/gateway.ts:890
callConnect(channelId: string, callback?: Function): void

decode()

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

disconnect()

src/gateway.ts:510
disconnect(code?: number, reason?: string): void
code?
number
Default SocketCloseCodes.NORMAL
reason?
string

emitPackets()

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

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

guildStreamCreate()

src/gateway.ts:899
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:531
handle(data: any, uncompressed?: boolean): void
data
any
uncompressed?
boolean
Default false

heartbeat()

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

lobbyConnect()

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

lobbyDisconnect()

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

lobbyVoiceStatesUpdate()

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

on()

src/gateway.ts:1193
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:718
onClose(target: BaseSocket, code?: number, reason?: string): void

onIdentifyCheck()

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

onMessage()

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

requestApplicationCommands()

src/gateway.ts:1000
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:953
requestChannelInfo(guildId: string, fields?: string[], callback?: Function): void

requestGuildMembers()

src/gateway.ts:964
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:991
requestSoundboardSounds(guildIds: string | string[], callback?: Function): void

send()

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

streamDelete()

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

streamSetPaused()

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

updateGuildSubscriptions()

src/gateway.ts:1074
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:1118
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:1097
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