feature: public raw events (#3159)

* add a public alternative to the private raw event

while retaining raw for use in debugging privately

* only emit dispatch packets

* requested changes

TIL, that's neat

* fix padding

* requested changes

* Update WebSocketManager.js
This commit is contained in:
bdistin
2019-04-29 11:37:57 -05:00
committed by Amish Shah
parent 870528ed33
commit 9b0f4b298d
3 changed files with 11 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
const EventEmitter = require('events');
const { Error: DJSError } = require('../../errors');
const Collection = require('../../util/Collection');
const Util = require('../../util/Util');
@@ -21,9 +22,14 @@ const UNRECOVERABLE_CLOSE_CODES = [4004, 4010, 4011];
/**
* The WebSocket manager for this client.
* <info>This class forwards raw dispatch events,
* read more about it here {@link https://discordapp.com/developers/docs/topics/gateway}</info>
* @extends EventEmitter
*/
class WebSocketManager {
class WebSocketManager extends EventEmitter {
constructor(client) {
super();
/**
* The client that instantiated this WebSocketManager
* @type {Client}

View File

@@ -257,6 +257,7 @@ class WebSocketShard extends EventEmitter {
try {
packet = WebSocket.unpack(this.inflate.result);
this.manager.client.emit(Events.RAW, packet, this.id);
if (packet.op === OPCodes.DISPATCH) this.manager.emit(packet.t, packet.d, this.id);
} catch (err) {
this.manager.client.emit(Events.SHARD_ERROR, err, this.id);
return;

4
typings/index.d.ts vendored
View File

@@ -1291,7 +1291,7 @@ declare module 'discord.js' {
constructor(id: string, token: string, options?: ClientOptions);
}
export class WebSocketManager {
export class WebSocketManager extends EventEmitter {
constructor(client: Client);
private totalShards: number | string;
private shardQueue: Set<WebSocketShard>;
@@ -1306,6 +1306,8 @@ declare module 'discord.js' {
public status: Status;
public readonly ping: number;
public on(event: WSEventType, listener: (data: any, shardID: number) => void): this;
public once(event: WSEventType, listener: (data: any, shardID: number) => void): this;
private debug(message: string, shard?: WebSocketShard): void;
private connect(): Promise<void>;
private createShards(): Promise<void>;