cloudstorm 0.9.1 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -6,6 +6,9 @@ import { EventEmitter } from 'events';
6
6
 
7
7
  type IntentFlags = typeof flags;
8
8
  type IntentResolvable = number | Array<number> | keyof IntentFlags | Array<keyof IntentFlags>;
9
+ /**
10
+ * Bit flags representing Discord intents.
11
+ */
9
12
  declare const flags: {
10
13
  GUILDS: number;
11
14
  GUILD_MEMBERS: number;
@@ -27,6 +30,11 @@ declare const flags: {
27
30
  AUTO_MODERATION_CONFIGURATION: number;
28
31
  AUTO_MODERATION_EXECUTION: number;
29
32
  };
33
+ /**
34
+ * A function to resolve either bit number(s) or human readable string(s) to a bit collection number Discord can accept as client intents.
35
+ * @param bit Data representing intents that can be resolved to a bit collection number Discord can accept.
36
+ * @returns A bit collection number Discord can accept as the client intents.
37
+ */
30
38
  declare function resolve(bit?: IntentResolvable): number;
31
39
  declare const _default: {
32
40
  flags: {
@@ -93,12 +101,15 @@ declare class RatelimitBucket {
93
101
  limit: number;
94
102
  limitReset: number;
95
103
  defaultReset?: number | undefined;
104
+ /** Functions waiting to be executed. */
96
105
  fnQueue: Array<{
97
106
  fn: () => unknown;
98
107
  callback: () => unknown;
99
108
  error: Error;
100
109
  }>;
110
+ /** How many more functions can be executed before this bucket needs to wait to reset. */
101
111
  remaining: number;
112
+ /** The Timeout that when executed, will reset this bucket's remaining to the limit. Null if remaining is the limit. */
102
113
  resetTimeout: NodeJS.Timeout | null;
103
114
  /**
104
115
  * Create a new Bucket.
@@ -155,22 +166,85 @@ interface BetterWs {
155
166
  declare class BetterWs extends EventEmitter {
156
167
  address: string;
157
168
  options: IClientWSOptions;
169
+ /** The encoding to send/receive messages to/from the server with. */
158
170
  encoding: "etf" | "json";
171
+ /** If the messages sent/received are compressed with zlib. */
159
172
  compress: boolean;
173
+ /** The ratelimit bucket for how many packets this ws can send within a time frame. */
160
174
  wsBucket: RatelimitBucket;
175
+ /** The ratelimit bucket for how many presence update specific packets this ws can send within a time frame. Is still affected by the overall packet send bucket. */
161
176
  presenceBucket: RatelimitBucket;
177
+ /** The raw net.Socket retreived from upgrading the connection or null if not upgraded/closed. */
162
178
  private _socket;
179
+ /** Internal properties that need a funny way to be referenced. */
163
180
  private _internal;
181
+ /** If a request is going through to initiate a WebSocket connection and hasn't been upgraded by the server yet. */
164
182
  private _connecting;
183
+ /** Code received from frame op 8 */
184
+ private _lastCloseCode;
185
+ /** Reason received from frame op 8 */
186
+ private _lastCloseReason;
187
+ /**
188
+ * Creates a new lightweight WebSocket.
189
+ * @param address The http(s):// or ws(s):// URL cooresponding to the server to connect to.
190
+ * @param options Options specific to this WebSocket.
191
+ */
165
192
  constructor(address: string, options: IClientWSOptions);
193
+ /**
194
+ * The state this WebSocket is in. 1 is connected, 2 is connecting, 3 is closing, and 4 is closed.
195
+ */
166
196
  get status(): 1 | 2 | 3 | 4;
197
+ /**
198
+ * Initiates a WebSocket connection to the server.
199
+ */
167
200
  connect(): Promise<void>;
201
+ /**
202
+ * Disconnects from the server.
203
+ * @param code The close code.
204
+ * @param reason The reason for closing if any.
205
+ * @returns A Promise that resolves when the connection is fully closed.
206
+ */
168
207
  close(code: number, reason?: string): Promise<void>;
208
+ /**
209
+ * Sends a message to the server in the format of { op: number, d: any } (before any encoding/compression).
210
+ * @param data What to send to the server.
211
+ * @returns A Promise that resolves when the message passes the bucket queue(s) and is written to the socket's Buffer to send.
212
+ */
169
213
  sendMessage(data: GatewaySendPayload): Promise<void>;
214
+ /**
215
+ * Method to raw write messages to the server.
216
+ * @param packet Buffer containing the message to send.
217
+ * @param opcode WebSocket spec op code.
218
+ */
170
219
  private _write;
220
+ /**
221
+ * Handler for when requests from connect are upgraded to WebSockets.
222
+ * @param key The sec key from connect.
223
+ * @param req The HTTP request from connect.
224
+ * @param resolve Promise resolver from connect.
225
+ * @param reject Promise rejector from connect.
226
+ * @param res The HTTP response from the server from connect.
227
+ * @param socket The raw socket from upgrading the request from connect.
228
+ */
229
+ private _onUpgrade;
230
+ /**
231
+ * Handler for when the raw socket to the server encounters an error.
232
+ * @param error What happened.
233
+ */
171
234
  private _onError;
235
+ /**
236
+ * Handler for when the raw socket is fully closed and cleans up this WebSocket.
237
+ */
172
238
  private _onClose;
239
+ /**
240
+ * Handler for when there is data in the socket's Buffer to read.
241
+ */
173
242
  private _onReadable;
243
+ /**
244
+ * Transforms/reads raw messages from the server and emits the appropriate event.
245
+ * @param opcode WebSocket spec op code.
246
+ * @param message Buffer of data to transform/read.
247
+ */
174
248
  private _processFrame;
175
249
  }
176
250
 
@@ -208,25 +282,47 @@ declare class DiscordConnector extends EventEmitter {
208
282
  endpoint?: string;
209
283
  };
210
284
  };
285
+ /** The options used by the client */
211
286
  options: DiscordConnector["client"]["options"];
287
+ /** If this connector will attempt to automatically reconnect */
212
288
  reconnect: boolean;
289
+ /** The WebSocket this connector uses */
213
290
  betterWs: BetterWs;
291
+ /** A Timeout that, when triggered, will send an op 1 heartbeat. Is null if Discord hasn't told this connector how often to heartbeat */
214
292
  heartbeatTimeout: NodeJS.Timeout | null;
293
+ /** How often this connector should heartbeat if not 0 */
215
294
  heartbeatInterval: number;
295
+ /** The _trace as sent by the Discord READY and RESUMED payloads */
216
296
  _trace: string | null;
297
+ /** The sequence, which is the number of events received by Discord within the session if any session */
217
298
  seq: number;
299
+ /** The status of this connector */
218
300
  status: "connecting" | "identifying" | "resuming" | "ready" | "disconnected";
301
+ /** The session ID used for resuming or null if Discord hasn't sent one yet */
219
302
  sessionId: string | null;
303
+ /** The ms timestamp when this connector last received an op 11 heartbeat ack if not 0 */
220
304
  lastACKAt: number;
305
+ /** The ms timestamp when this connector last sent an op 1 heartbeat if not 0 */
221
306
  lastHeartbeatSend: number;
307
+ /** The time in milliseconds it took for Discord to send an op 11 heartbeat ack in response to an op 1 heartbeat */
222
308
  latency: number;
223
- private _closing;
309
+ /** The address the WebSocket will use to connect to the Discord gateway if not resuming */
224
310
  identifyAddress: string;
311
+ /** The address the WebSocket will use to connect to the Discord gateway if resuming */
225
312
  resumeAddress: string | null;
313
+ /** If this connector is disconnected/disconnecting currently, but will reconnect eventually */
226
314
  reconnecting: boolean;
315
+ /** If this connector is waiting to be fully closed */
316
+ private _closing;
317
+ /** If the disconnect method on this class was called and the connect method hasn't been called yet */
318
+ private _closeCalled;
319
+ /** A Timeout that, when triggered, closes the connection because op HELLO hasn't been received and may never be received */
320
+ private _openToHeartbeatTimeout;
321
+ /** A Timeout that, when triggered, sends the first heartbeat */
322
+ private _initialHeartbeatTimeout;
227
323
  static readonly default: typeof DiscordConnector;
228
324
  /**
229
- * Create a new Discord Connector.
325
+ * Creates a new Discord Connector.
230
326
  * @param id id of the shard that created this class.
231
327
  * @param client Main client instance.
232
328
  */
@@ -258,6 +354,9 @@ declare class DiscordConnector extends EventEmitter {
258
354
  * Hard reset this connector.
259
355
  */
260
356
  private reset;
357
+ /**
358
+ * Sets the this.heartbeatTimeout Interval.
359
+ */
261
360
  private setHeartBeat;
262
361
  /**
263
362
  * Clear the heart beat interval, set it to null and set the cached heartbeat_interval as 0.
@@ -360,7 +459,9 @@ declare class Shard extends EventEmitter {
360
459
  endpoint?: string;
361
460
  };
362
461
  };
462
+ /** If this shard has received the READY or RESUMED payload and isn't disconnected yet. */
363
463
  ready: boolean;
464
+ /** The connector that handles all of the Discord specific connection logic. */
364
465
  connector: DiscordConnector;
365
466
  /**
366
467
  * Create a new Shard.
@@ -414,11 +515,15 @@ declare class ShardManager {
414
515
  endpoint?: string;
415
516
  };
416
517
  };
518
+ /** The options used by the client */
417
519
  options: ShardManager["client"]["options"];
520
+ /** A Record of shards keyed by their ID */
418
521
  shards: {
419
522
  [id: number]: Shard;
420
523
  };
524
+ /** The bucket used to identify a certain number of shards within a day. */
421
525
  identifyBucket: RatelimitBucket;
526
+ /** The bucket used to identify x number of shards within 5 second intervals. Larger bots benefit from this, but doesn't change how many times per day any shards can identify. */
422
527
  concurrencyBucket: RatelimitBucket | null;
423
528
  /**
424
529
  * Create a new ShardManager.
@@ -508,17 +613,23 @@ interface Client {
508
613
  * Main class used for receiving events and interacting with the Discord gateway.
509
614
  */
510
615
  declare class Client extends EventEmitter {
616
+ /** The Discord auth token to connect with. */
511
617
  token: string;
618
+ /** User specific options filled in with defaults if not specified. */
512
619
  options: Omit<IClientOptions, "snowtransferInstance"> & {
513
620
  token: string;
514
621
  endpoint?: string;
515
622
  };
623
+ /** The manager of all of the shards used to connect to Discord. */
516
624
  shardManager: ShardManager;
625
+ /** The version string of CloudStorm. */
517
626
  version: string;
627
+ /** The SnowTransfer instance to use to make some requests to get connect info. */
518
628
  private _restClient;
519
629
  /**
520
630
  * Create a new Client to connect to the Discord gateway.
521
631
  * @param token Token received from creating a discord bot user, which will be used to connect to the gateway.
632
+ * @param options Baseline options to use. Will be filled with defaults if not specified.
522
633
  */
523
634
  constructor(token: string, options?: IClientOptions);
524
635
  /**