-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Client.ts
450 lines (397 loc) · 11.7 KB
/
Client.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
import { EventEmitter } from "node:events";
import type { Collection } from "@discordjs/collection";
import { RestManager, WebSocketManager } from "@guildedjs/api";
import type { SkeletonWSPayload } from "@guildedjs/api";
import type TypedEmitter from "typed-emitter";
import type { CacheStructure } from "../cache";
import { ClientGatewayHandler } from "../gateway/ClientGatewayHandler";
import { GlobalCalendarManager } from "../managers/global/CalendarManager";
import { GlobalCategoryManager } from "../managers/global/CategoryManager";
import { GlobalChannelManager } from "../managers/global/ChannelManager";
import { GlobalDocManager } from "../managers/global/DocManager";
import { GlobalForumTopicManager } from "../managers/global/ForumManager";
import { GlobalGroupManager } from "../managers/global/GroupManager";
import { GlobalGuildBanManager } from "../managers/global/GuildBanManager";
import { GlobalListItemManager } from "../managers/global/ListManager";
import { GlobalMemberManager } from "../managers/global/MemberManager";
import { GlobalMessageManager } from "../managers/global/MessageManager";
import { GlobalReactionManager } from "../managers/global/ReactionManager";
import { GlobalRoleManager } from "../managers/global/RoleManager";
import { GlobalServerManager } from "../managers/global/ServerManager";
import { GlobalSubscriptionManager } from "../managers/global/SubscriptionManager";
import { GlobalUserManager } from "../managers/global/UserManager";
import { GlobalWebhookManager } from "../managers/global/WebhookManager";
import type { ClientEvents } from "../typings";
import type { Server } from "./Server";
import { ClientUser } from "./User";
/**
* The main class for interacting with the api.
*
* @template ClientEvents The custom events for the client.
*/
export class Client extends (EventEmitter as unknown as new () => TypedEmitter<ClientEvents>) {
/**
* The time in milliseconds since the Client connected.
*/
readyTimestamp: number | null = null;
/**
* The manager for the bot to make requests to the REST api.
*/
rest = new RestManager({
...this.options.rest,
token: this.options.token,
});
/**
* The websocket connection.
*/
ws = new WebSocketManager({
...this.options.ws,
token: this.options.token,
});
/**
* The gateway events will be processed by this manager.
*/
gatewayHandler = new ClientGatewayHandler(this);
/**
* A manager for channels, used to manage and interact with channels.
*/
channels = new GlobalChannelManager(this);
/**
* A manager for docs, used to manage and interact with docs.
*/
docs = new GlobalDocManager(this);
/**
* A manager for forum topics, used to manage and interact with forum topics.
*/
topics = new GlobalForumTopicManager(this);
/**
* A manager for groups, used to manage and interact with groups.
*/
groups = new GlobalGroupManager(this);
/**
* A manager for list items, used to manage and interact with list items.
*/
lists = new GlobalListItemManager(this);
/**
* A manager for members, used to manage and interact with members.
*/
members = new GlobalMemberManager(this);
/**
* A manager for messages, used to manage and interact with messages.
*/
messages = new GlobalMessageManager(this);
/**
* A manager for roles, used to manage and interact with roles.
*/
roles = new GlobalRoleManager(this);
/**
* A manager for users, used to manage and interact with users.
*/
users = new GlobalUserManager(this);
/**
* A manager for guild bans, used to manage and interact with bans.
*/
bans = new GlobalGuildBanManager(this);
/**
* A manager for webhooks, used to manage and interact with webhooks.
*/
webhooks = new GlobalWebhookManager(this);
/**
* A manager for servers, used to manage and interact with servers.
*/
servers = new GlobalServerManager(this);
/**
* A manager for reactions, used to manage and interact with reactions.
*/
reactions = new GlobalReactionManager(this);
/**
* A manager for calendars, used to manage and interact with calendars.
*/
calendars = new GlobalCalendarManager(this);
/**
* A manager for categories, used to manage and interact with categories.
*/
categories = new GlobalCategoryManager(this);
/**
* A manager for server subscriptions, used to manage and interact with server subscriptions.
*/
subscriptions = new GlobalSubscriptionManager(this);
/**
* The user belonging to this bot.
*/
user: ClientUser | null = null;
/**
* @param options The options for the client.
* @throws {Error} Must provide options in client constructor in the form of an object.
* @throws {Error} No token provided.
*/
constructor(public options: ClientOptions) {
// eslint-disable-next-line constructor-super
super();
if (typeof options !== "object") throw new Error("Must provide options in client constructor in the form of an object.");
if (typeof options?.token === "undefined") throw new Error("No token provided.");
}
/**
* The amount of time the bot has been online in milliseconds.
*/
get uptime(): number {
return this.readyTimestamp ? Date.now() - this.readyTimestamp : 0;
}
/**
* The bot's token.
*/
get token(): string {
return this.options.token;
}
/**
* Connects the bot to the api.
*
* @param [opts] The options for connecting to the api.
* @param [opts.fresh] Whether this should create a new WebSocketManager instance.
* @example
* let client = new Guilded.Client({ token: process.env.GUILDED_TOKEN });
* client.on('ready', () => console.log('Logged in!'));
* client.login();
*/
login(opts?: {
fresh?: boolean;
}): void {
if (opts?.fresh)
this.ws = new WebSocketManager({
token: this.options.token,
});
this.ws.emitter
.on("error", (reason, err) => this.emit("error", `[WS] ${reason}`, err))
.on("ready", (user) => {
this.user = new ClientUser(this, user);
this.readyTimestamp = Date.now();
this.emit("ready");
})
.on("gatewayEvent", (event, data) => this.gatewayHandler.handleWSMessage(event, data))
.on("debug", (data) => this.emit("debug", data))
.on("exit", () => this.emit("exit"));
this.ws.connect();
}
/**
* Disconnects the bot.
*
* @throws {Error} There is no active connection to disconnect.
*/
disconnect(): void {
if (!this.ws.isAlive) throw new Error("There is no active connection to disconnect.");
this.ws.emitter.removeAllListeners();
this.ws.destroy();
this.emit("exit");
}
/**
* Fetches the logged in client's servers.
*
* @returns The logged in client's servers.
*/
fetchServers(): Promise<Collection<string, Server>> {
return this.users.fetchServers(this.user?.id!);
}
/**
* Set current logged in client's status
*
* @param options The options for setting the status.
* @param options.content The content of the status.
* @param options.emoteId The id of the emote to use for the status.
* @param options.expiresAt The time the status should expire.
*/
async setStatus(options: {
content?: string;
emoteId: number;
expiresAt?: Date | number | string;
}): Promise<void> {
let resolvedDate: Date | undefined;
if (options.expiresAt instanceof Date) {
resolvedDate = options.expiresAt;
} else if (typeof options.expiresAt === "string") {
resolvedDate = new Date(options.expiresAt);
} else if (typeof options.expiresAt === "number") {
resolvedDate = new Date(Date.now() + options.expiresAt);
} else {
resolvedDate = options.expiresAt;
}
await this.rest.router.userStatus.userStatusCreate({
userId: this.user!.id,
requestBody: {
content: options.content,
emoteId: options.emoteId,
expiresAt: resolvedDate?.toISOString() ?? undefined,
},
});
}
/**
* Clear current logged in client's status
*/
async clearStatus(): Promise<void> {
await this.rest.router.userStatus.userStatusDelete({
userId: this.user!.id,
});
}
}
/**
* Options for constructing the client.
*/
export type ClientOptions = {
/**
* The bot's token.
*
* @remarks The token is required to authenticate with the Guilded API.
*/
token: string;
/**
* The RestManager options.
*/
rest?: {
/**
* The version of the API to be used for making requests. By default, this will use the latest version that the library supports.
*
* @default 1
* @remarks You can set the API version to 1 if you want to use the stable API.
*/
version?: 1;
/**
* The base url of the API you want to send requests to. By default, this will send it to guilded's rest API.
* This is meant for big bot developers who want to use a proxy rest system.
*
* @remarks If you want to use a custom API url, you can set this property to your custom url.
*/
proxyURL?: string;
/**
* The headers that will be supplied in each request that the client, or more precisely, RestManager, will send.
*
* @remarks It's generally only recommended to supply headers that may be required for Guilded experiments, as vital headers will be supplied by Guilded.js.
*/
headers?: Record<string, string>;
};
/**
* Options that will be given directly to WebSocket.
*/
ws?: {
/**
* The headers that will be supplied when WebSocket is being initialized and its request is made.
*
* @remarks It's generally only recommended to supply headers that may be required for Guilded experiments, as vital headers will be supplied by Guilded.js.
*/
headers?: Record<string, string>;
};
/**
* Gateway handling options
*/
gateway?: {
/**
* A boolean returning function that dictates whether an event is discarded
*/
discardEvent?(event: string, data: SkeletonWSPayload): boolean;
};
/**
* The cache options for the client.
*/
cache?: {
/**
* The function to create a new instance of CacheStructure.
*
* @remarks You can use this to provide your own implementation of CacheStructure, which will be used to cache data in the client in a future update.
*/
structureBuilder?<K, V>(): CacheStructure<K, V>;
/**
* Whether to fetch the author of a message when it is created and cache it.
*
* @default false
*/
fetchMessageAuthorOnCreate?: boolean;
/**
* Whether to remove a member from the cache when they leave a server.
*
* @default true
*/
removeMemberOnLeave?: boolean;
/**
* Whether to remove a member's ban from the cache when they are unbanned.
*
* @default true
*/
removeMemberBanOnUnban?: boolean;
/**
* Whether to remove a channel from the cache when it is deleted.
*
* @default true
*/
removeChannelOnDelete?: boolean;
/**
* Whether to remove a calendar from the cache when it is deleted.
*
* @default true
*/
removeCalendarsOnDelete?: boolean;
/**
* Whether to remove a calendar RSVP from the cache when it is deleted.
*
* @default true
*/
removeCalendarRsvpOnDelete?: boolean;
/**
* Whether to cache member bans.
*
* @default true
*/
cacheMemberBans?: boolean;
/**
* Whether to cache webhooks.
*
* @default true
*/
cacheWebhooks?: boolean;
/**
* Whether to cache channels.
*
* @default true
*/
cacheChannels?: boolean;
/**
* Whether to cache servers.
*
* @default true
*/
cacheServers?: boolean;
/**
* Whether to cache messages.
*
* @default true
*/
cacheMessages?: boolean;
/**
* Whether to cache forum topics.
*
* @default true
*/
cacheForumTopics?: boolean;
/**
* Whether to cache message reactions.
*
* @default true
*/
cacheMessageReactions?: boolean;
/**
* Whether to cache calendars.
*
* @default true
*/
cacheCalendars?: boolean;
/**
* Whether to cache calendar RSVPs.
*
* @default true
*/
cacheCalendarsRsvps?: boolean;
/**
* Whether to cache member social links.
*
* @default true
*/
cacheSocialLinks?: boolean;
};
};