@spectacles/gateway
v0.12.1
Published
The gateway to Discord.
Downloads
19
Readme
Spectacles Gateway
Spawns shards and manages a bot's lifetime on the Discord WebSocket gateway.
Getting started
const { Cluster } = require('@spectacles/gateway');
const cluster = new Cluster('a token');
cluster.spawn();
You've just spawned the recommended number of shards. If you want to use a custom or fixed shard count:
const { Cluster, Gateway } = require('@spectacles/gateway');
const gateway = Gateway.from('token', 30);
const cluster = new Cluster(gateway);
This will spawn 30 shards for the given token. Providing shard count isn't necessary after the first call.
Events
The shard emits events in the form [event name], [data]
. The cluster emits events in the form [event name], [data], [shard]
. When using a cluster, events will only be emitted on shards that have event listeners. Available events:
open
- WebSocket openedclose
- WebSocket closures (follows the CloseEvent API)error
- proxied from the underlying WebSocket connectionsend
- before data is sent over the connection (emitted as unencoded packet, or buffer)receive
- data that is received from the connection (decoded prior to emission)connect
- explicit connections to the WebSocket (fired initially and upon any reconnections)disconnect
- explicit disconnections from the WebSocket (i.e. when the client requests a connection closure)[Discord gateway event]
- OP 0 data, keyed byt
(onlyd
is emitted)
For details about Discord Gateway events, check out their documentation.
shard.on('MESSAGE_CREATE', message => {
// message = https://discordapp.com/developers/docs/resources/channel#message-object-message-structure
});
Properties
Cluster
gateway: Gateway
- the gateway session to use with the clustertoken: string
- your tokenshards: Map<number, Shard>
- a map of your shard connections, keyed by shard idconstructor(token: string | Gateway)
spawn(shards: number | number[])
- spawnshards
number of shards (if number), or spawn the specified shard IDs (if array)
Shard
- static readonly
ZLIB_SUFFIX: UInt8Array
- the zlib suffix gateway: Gateway
- the gateway session to use with this shardclient: Client
- the client of this shardshard: number
- this shard idversion: 6
- the gateway version to use (locked at 6)constructor(token: string | Gateway, shard: number)
- readonly
seq: number
- the current sequence - readonly
session?: string
- the current session identifier - readonly
ws: WebSocket
- the raw websocket connect(): Promise<void>
- connect to the gatewaydisconnect(code?: number): Promise<void>
- disconnectreconnect(code?: number): Promise<void>
- reconnectidentify(pk?: Partial<Identify>): Promise<void>
- identifyresume(): void
- resume the sessionheartbeat(): void
- send a heartbeatreceive(data: WebSocket.Data): void
- handle packets receivedsend(opOrPK: number | buffer | Payload | string, d?: any): void
- send data to the gatewaysend(pk: Buffer)
- just send a buffersend(pk: Payload)
- send a pre-formatted payload objectsend(op: number | string, d: any)
- sendd
to the gateway: ifop
is a number, send as that op; ifop
is a string, send as op 0 withop
ast
Gateway
Represents connection information for a token.
- static
tokens: Map<string, Gateway>
- map of tokens to instantiated gateway instances; used to ensure singletons per token - static
fetch(tokenOrGateway: string | Gateway): Gateway
- fetches the gateway for a given token constructor(token: string, shardCount?: number)
token: string
- the token of this gatewayshards: number
- total shard count of this token; recommended count is set if no value is provided- readonly
url: string
- the gateway URL to connect to - readonly
sessionStartLimit: null | { total: number, remaining: number, resetAfter: Date }
- information about the session start ratelimits identify(shard: Shard, packet: Partial<Identify>): Promise<void>
- identify with the given shard; attempts to resume if a session is available on the shardfetch(force = false): Promise<this>
- fetch gateway information; automatically called when connecting