This repository has been archived by the owner on Mar 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
493 lines (423 loc) · 9.92 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Generated by dts-bundle v0.7.3
// Dependencies for this module:
//
// ../veza
// ../cluster
import type {
Channel,
Client,
Client as DiscordClient,
ClientOptions,
EventDispatcher,
Guild,
Store,
User,
} from "@kyudiscord/neo";
import type { Client as VezaClient, ClientSocket, SendOptions, Server } from "veza";
import type { Worker } from "cluster";
declare module "@kyudiscord/neo" {
interface Client {
shard: ShardUtil<this>;
}
}
export class ShardUtil<C extends Client> {
readonly client: C;
/**
* The ID of this shard.
*/
readonly id: number;
/**
* The IPC for this shard.
*/
readonly ipc: ClusterIPC<C>;
/**
* The IPC socket of this shard.
*/
ipcSocket: number | string;
/**
* @param client
* @param ipcSocket
*/
constructor(client: C, ipcSocket: number | string);
/**
* The total shard count..
*/
get shardCount(): number;
/**
* The total cluster count.
*/
get clusterCount(): number;
/**
* Sends a message to the cluster.
* @param data The data to send.
* @param options The send options.
*/
send<T = unknown>(data: Dictionary | any, options?: SendOptions): Promise<T>;
/**
* Broadcasts a script to run on all shards.
* @param script The script to broadcast.
*/
broadcastEval<T = unknown>(script: EvalScript): Promise<T[]>;
/**
* Evaluates a script on the master process.
* @param script The script to evaluate.
*/
masterEval<T = unknown>(script: EvalScript): Promise<T>;
/**
* Fetch a client value on all clusters.
* @param property The property to fetch.
*/
fetchClientValues<T = unknown>(property: string): Promise<T[]>;
/**
* Fetches a guild.
* @param id The id of the guild to fetch.
*/
fetchGuild<G extends Guild = Guild>(id: string): Promise<Readonly<G>>;
/**
* Fetches a channel.
* @param id The id of the channel to fetch.
*/
fetchChannel<C extends Channel = Channel>(id: string): Promise<Readonly<C>>;
/**
* Fetches a user.
* @param id The id of the user to fetch.
*/
fetchUser<U extends User = User>(id: string): Promise<Readonly<U>>;
/**
* Restarts all clusters.
*/
restartAll(): Promise<void>;
/**
* Restarts a specific cluster.
* @param clusterId The cluster to restart.
*/
restart(clusterId?: number): Promise<void>;
}
export abstract class BaseCluster<C extends Client> {
/**
* The id of this cluster.
*/
readonly id: number;
/**
* The client instance.
*/
readonly client: C;
/**
* The sharding manager.
*/
manager: ShardingManager;
/**
* @param manager
*/
constructor(manager: ShardingManager);
/**
* Initializes the base cluster.
*/
init(): Promise<void>;
/**
* Launches this cluster.
*/
protected abstract launch(): void | Promise<void>;
}
export class Cluster extends EventDispatcher {
/**
* The sharding manager.
*/
readonly manager: ShardingManager;
/**
* The id of this cluster.
*/
readonly id: number;
/**
* The cluster worker.
*/
worker?: Worker;
/**
* The shards of this cluster.
*/
shards: number[];
/**
* Whether this cluster is ready or not.
*/
ready: boolean;
/**
* @param manager
* @param options
*/
constructor(manager: ShardingManager, options: ClusterOptions);
/**
* Evaluates a script.
* @param script The script to evaluate.
*/
eval<T = unknown>(script: EvalScript): Promise<T>;
/**
* Fetches a property of the client.
* @param property The property.
*/
fetchClientValue<T = unknown>(property: string): Promise<T>;
/**
* Kills the cluster worker.
*/
kill(): void;
/**
* Respawns the worker.
* @param delay The delay.
*/
respawn(delay?: number): Promise<void>;
/**
* Sends data to the cluster ipc.
* @param data The data to send.
*/
send<T = unknown>(data: Dictionary): Promise<T>;
/**
* Spawns the cluster worker.
*/
spawn(): Promise<void>;
}
export interface ClusterOptions {
id: number;
shards: number[];
}
export class ClusterIPC<C extends DiscordClient> extends EventDispatcher {
readonly discord: C;
/**
* The veza
*/
readonly client: VezaClient;
/**
* The cluster id.
*/
id: number;
/**
* The socket to use.
*/
socket: string | number;
/**
* @param discord The discord client.
* @param id The cluster id.
* @param socket The socket.
*/
constructor(discord: C, id: number, socket: string | number);
/**
* Returns the client socket.
*/
get server(): ClientSocket;
/**
* Initializes the client socket.
*/
init(): Promise<void>;
/**
* Broadcasts a script to all clusters.
* @param script The script to broadcast.
*/
broadcast<T>(script: EvalScript): Promise<T[]>;
/**
* Evaluates a script on the master process.
* @param script The script to evaluate.
*/
masterEval<T>(script: EvalScript): Promise<T>;
}
export type EvalScript = string | ((this: Client, ...args: unknown[]) => unknown | Promise<unknown[]>);
export interface IPCRequest {
op: IPCEvent;
d: string;
}
export interface IPCResult {
success: boolean;
d: unknown;
}
export interface IPCError {
name: string;
message: string;
stack: string;
}
export class MasterIPC extends EventDispatcher {
/**
* The sharding manager.
*/
readonly manager: ShardingManager;
/**
* The IPC Server.
*/
readonly server: Server;
/**
* @param manager
*/
constructor(manager: ShardingManager);
/**
* Broadcasts a script to every cluster.
* @param script The script to broadcast.
*/
broadcast<T = unknown>(script: EvalScript): Promise<T[]>;
}
export class ShardingManager extends EventDispatcher {
/**
* The path to the main file.
*/
readonly path: string;
/**
* Cluster store.
*/
readonly clusters: Store<number, Cluster>;
/**
* The master IPC.
*/
readonly ipc: MasterIPC;
/**
* The number of guilds per shard.
*/
guildsPerShard: number;
/**
* The client to use.
*/
createClient: CreateClient;
/**
* The IPC socket.
*/
ipcSocket: number | string;
/**
* The shards to spawn.
*/
shardCount: number | "auto";
/**
* The clusters to spawn.
*/
clusterCount: number;
/**
* Whether to retry.
*/
retry: boolean;
/**
* The arguments to pass to the clusters.
*/
nodeArgs: string[];
/**
* Whether or not to respawn workers if they exit.
*/
respawn: boolean;
/**
* The timeout.
*/
timeout: number;
/**
* Amount of retries to use for respawning.
*/
retries: number;
/**
* Env Variables to Pass.
*/
env: Dictionary;
/**
* @param path
* @param options
*/
constructor(path: string, options: SharderOptions);
/**
* The provided token.
*/
get token(): string;
/**
* Spawns all clusters and shards.
*/
spawn(): Promise<void>;
/**
* Fetch a client property from all shards.
* @param property
*/
fetchClientValues<T>(property: string): Promise<Readonly<T>[]>;
/**
* Evaluate a script in 'this' context.
* @param script The script to eval.
*/
eval<T = unknown>(script: string): Promise<T>;
/**
* Restarts all clusters.
*/
respawnAll(): Promise<void>;
/**
* Respawns a specific cluster.
* @param clusterId The cluster to respawn.
*/
restart(clusterId: number): Promise<void>;
}
export type CreateClient = (token: string, options: ClientOptions) => Client;
export interface ShardingManager {
on(event: SharderEvent.DEBUG, listener: (message: string) => void): this;
on(event: SharderEvent.MESSAGE, listener: (message: unknown) => void): this;
on(event: SharderEvent.READY | SharderEvent.SPAWN, listener: (cluster: Cluster) => void): this;
on(event: SharderEvent.SHARD_READY | SharderEvent.SHARD_RECONNECT, listener: (shardId: number) => void): this;
on(event: SharderEvent.SHARD_RESUME, listener: (shardId: number, replayed: number) => void): this;
on(event: SharderEvent.SHARD_DISCONNECT, listener: (shardId: number) => void): this;
once(event: SharderEvent.DEBUG, listener: (message: string) => void): this;
once(event: SharderEvent.MESSAGE, listener: (message: unknown) => void): this;
once(event: SharderEvent.READY | SharderEvent.SPAWN, listener: (cluster: Cluster) => void): this;
once(event: SharderEvent.SHARD_READY | SharderEvent.SHARD_RECONNECT, listener: (shardId: number) => void): this;
once(event: SharderEvent.SHARD_RESUME, listener: (shardId: number, replayed: number) => void): this;
once(event: SharderEvent.SHARD_DISCONNECT, listener: (shardId: number) => void): this;
}
export interface SharderOptions {
token: string;
shardCount?: number | "auto";
clusterCount?: number;
createClient?: CreateClient;
guildsPreShard?: number;
respawn?: boolean;
ipcSocket?: string | number;
timeout?: number;
retry?: boolean;
retries?: number;
nodeArgs?: string[];
env?: Dictionary;
}
export interface SessionObject {
url: string;
shards: number;
session_start_limit: {
total: number;
remaining: number;
reset_after: number;
};
}
/**
* Makes an error from a plain object.
* @param obj The error object.
*/
export function makeError(obj: ErrorObject): Error;
export interface ErrorObject {
stack: string;
message: string;
name: string;
}
/**
* Chunks an array.
* @param entries The entries to chunk.
* @param chunks How many chunks to create.
*/
export function chunk<T>(entries: T[], chunks: number): Array<T[]>;
export enum IPCEvent {
EVAL = 0,
MESSAGE = 1,
BROADCAST = 2,
READY = 3,
SHARD_READY = 4,
SHARD_RECONNECT = 5,
SHARD_RESUME = 6,
SHARD_DISCONNECT = 7,
MASTER_EVAL = 8,
RESTART_ALL = 9,
RESTART = 10,
FETCH_USER = 11,
FETCH_CHANNEL = 12,
FETCH_GUILD = 13
}
export enum SharderEvent {
DEBUG = "debug",
MESSAGE = "message",
READY = "ready",
SPAWN = "spawn",
SHARD_READY = "shardReady",
SHARD_RECONNECT = "shardReconnect",
SHARD_RESUME = "shardResume",
SHARD_DISCONNECT = "shardDisconnect",
ERROR = "error"
}