bakit 2.2.4 → 3.0.0

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.js CHANGED
@@ -1,364 +1,522 @@
1
- import { attachEventBus } from '@bakit/utils';
2
- export { Collection, attachEventBus, capitalize, createEventBus, createQueue, isPlainObject, isPromiseLike, promisify, sleep } from '@bakit/utils';
3
- import { ChannelType, GatewayDispatchEvents } from 'discord-api-types/v10';
4
- export { DEFAULT_GATEWAY_MANAGER_OPTIONS, DEFAULT_WORKER_PATH, createGatewayManager, createWorker } from '@bakit/gateway';
5
- export { createREST } from '@bakit/rest';
6
- export { RPCError, createIPCClient, createIPCServer, createService, createTransportClient, createTransportServer, deserializeRPCError, getIPCPath, serializeRPCError } from '@bakit/service';
1
+ import { ShardingManager } from '@bakit/gateway';
2
+ export { Cluster, Shard, ShardState, ShardStrategy, ShardingManager } from '@bakit/gateway';
3
+ import { REST } from '@bakit/rest';
4
+ export { DEFAULT_REST_OPTIONS, DiscordHTTPError, REST, RESTAdapter, RESTMethod, createREST, createRESTAdapter } from '@bakit/rest';
5
+ export { Service, ServiceDriver, ServiceProcess, ServiceState, TransportClient, TransportServer, createService, createTransportClient, createTransportServer } from '@bakit/service';
6
+ import { getSnowflakeDate } from '@bakit/utils';
7
+ export { capitalize, instanceToObject, isCommonJS, isESM, isPlainObject, isPromiseLike, promisify, sleep } from '@bakit/utils';
8
+ import EventEmitter from 'events';
9
+ import { Collection } from '@discordjs/collection';
10
+ import { Routes, GatewayDispatchEvents, ChannelType } from 'discord-api-types/v10';
11
+ import * as v10 from 'discord-api-types/v10';
12
+ export { v10 as DiscordAPIV10 };
13
+ export { GatewayIntentBits as Intent } from 'discord-api-types/v10';
14
+ import { createMixin, applyMixins } from 'tiny-mixin';
7
15
 
8
- // src/lib/client/index.ts
16
+ // src/modules/gateway.ts
9
17
 
10
- // src/lib/structures/user.ts
11
- function createUser(client, data) {
12
- return {
13
- get client() {
14
- return client;
15
- },
16
- get id() {
17
- return data.id;
18
- },
19
- get username() {
20
- return data.username;
21
- },
22
- get discriminator() {
23
- return data.discriminator;
24
- },
25
- get globalName() {
26
- return data.global_name;
27
- },
28
- get avatar() {
29
- return data.avatar;
30
- },
31
- get system() {
32
- return data.system;
33
- },
34
- get mfaEnabled() {
35
- return data.mfa_enabled;
36
- },
37
- get banner() {
38
- return data.banner;
39
- },
40
- get accentColor() {
41
- return data.accent_color ?? void 0;
42
- },
43
- get locale() {
44
- return data.locale;
45
- },
46
- get verified() {
47
- return data.verified;
48
- },
49
- get email() {
50
- return data.email;
51
- },
52
- get flags() {
53
- return data.flags;
54
- },
55
- get premiumType() {
56
- return data.premium_type;
57
- },
58
- get publicFlags() {
59
- return data.public_flags;
60
- }
61
- };
62
- }
18
+ // src/lib/structures/BaseStructure.ts
19
+ var BaseStructure = class {
20
+ constructor(client) {
21
+ Object.defineProperty(this, "client", {
22
+ value: client,
23
+ enumerable: false,
24
+ writable: false
25
+ });
26
+ }
27
+ toJSON() {
28
+ return {};
29
+ }
30
+ };
31
+ var User = class extends BaseStructure {
32
+ constructor(client, data) {
33
+ super(client);
34
+ this.data = data;
35
+ }
36
+ get id() {
37
+ return this.data.id;
38
+ }
39
+ get username() {
40
+ return this.data.username;
41
+ }
42
+ get discriminator() {
43
+ return this.data.discriminator;
44
+ }
45
+ get globalName() {
46
+ return this.data.global_name;
47
+ }
48
+ get tag() {
49
+ return this.discriminator !== "0" && this.discriminator !== "0000" ? `${this.username}#${this.discriminator}` : this.username;
50
+ }
51
+ get displayName() {
52
+ return this.globalName ?? this.tag;
53
+ }
54
+ get avatar() {
55
+ return this.data.avatar ?? void 0;
56
+ }
57
+ get bot() {
58
+ return !!this.data.bot;
59
+ }
60
+ get system() {
61
+ return !!this.data.system;
62
+ }
63
+ get mfaEnabled() {
64
+ return this.data.mfa_enabled;
65
+ }
66
+ get locale() {
67
+ return this.data.locale;
68
+ }
69
+ get verified() {
70
+ return this.data.verified;
71
+ }
72
+ get email() {
73
+ return this.data.email;
74
+ }
75
+ get flags() {
76
+ return this.data.flags;
77
+ }
78
+ get premiumType() {
79
+ return this.data.premium_type;
80
+ }
81
+ get publicFlags() {
82
+ return this.data.public_flags;
83
+ }
84
+ get banner() {
85
+ return this.data.banner;
86
+ }
87
+ get accentColor() {
88
+ return this.data.accent_color ?? void 0;
89
+ }
90
+ get createdAt() {
91
+ return getSnowflakeDate(this.id);
92
+ }
93
+ get createdTimestamp() {
94
+ return this.createdAt.getTime();
95
+ }
96
+ get hexAccentColor() {
97
+ return this.accentColor !== void 0 ? `#${this.accentColor.toString(16).padStart(6, "0")}` : void 0;
98
+ }
99
+ get defaultAvatarURL() {
100
+ return `https://cdn.discordapp.com/embed/avatars/${this.discriminator !== "0" && this.discriminator !== "0000" ? BigInt(this.discriminator) % 5n : (BigInt(this.id) >> 22n) % 6n}.png`;
101
+ }
102
+ getAvatarURL(options) {
103
+ if (!this.avatar)
104
+ return;
105
+ let isAnimated = this.avatar.startsWith("a_"), ext = options?.extension ?? (isAnimated ? "gif" : "png"), size = options?.size ? `?size=${options.size}` : "";
106
+ return `https://cdn.discordapp.com/avatars/${this.id}/${this.avatar}.${ext}${size}`;
107
+ }
108
+ getDisplayAvatarURL(options) {
109
+ return this.getAvatarURL(options) ?? this.defaultAvatarURL;
110
+ }
111
+ equals(other) {
112
+ return typeof other == "string" ? other === this.id : other.id === this.id && other.username === this.username && other.discriminator === this.discriminator && other.bot === this.bot && other.createdTimestamp === this.createdTimestamp;
113
+ }
114
+ toJSON() {
115
+ return this.data;
116
+ }
117
+ toString() {
118
+ return `<@${this.id}>`;
119
+ }
120
+ };
121
+
122
+ // src/lib/structures/Message.ts
123
+ var Message = class extends BaseStructure {
124
+ constructor(client, data) {
125
+ super(client);
126
+ this.data = data;
127
+ }
128
+ cachedAuthor;
129
+ get partial() {
130
+ return typeof this.data.content != "string" || typeof this.data.author != "object";
131
+ }
132
+ get content() {
133
+ return this.data.content;
134
+ }
135
+ get id() {
136
+ return this.data.id;
137
+ }
138
+ get channelId() {
139
+ return this.data.channel_id;
140
+ }
141
+ get channel() {
142
+ return this.guild.channels.get(this.channelId);
143
+ }
144
+ get guildId() {
145
+ return "guild_id" in this.data ? this.data.guild_id : void 0;
146
+ }
147
+ get guild() {
148
+ return this.guildId ? this.client.guilds.get(this.guildId) : void 0;
149
+ }
150
+ get author() {
151
+ return this.cachedAuthor ??= new User(this.client, this.data.author), this.cachedAuthor;
152
+ }
153
+ get createdAt() {
154
+ return new Date(this.data.timestamp);
155
+ }
156
+ get editedAt() {
157
+ return this.data.edited_timestamp ? new Date(this.data.edited_timestamp) : void 0;
158
+ }
159
+ get createdTimestamp() {
160
+ return this.createdAt.getTime();
161
+ }
162
+ get editedTimestamp() {
163
+ return this.editedAt?.getTime();
164
+ }
165
+ get url() {
166
+ return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`;
167
+ }
168
+ async fetch() {
169
+ let message = await this.client.helper.fetchMessage(this.channelId, this.id);
170
+ return this.data = message.data, this.cachedAuthor = message.cachedAuthor, this;
171
+ }
172
+ async reply(options) {
173
+ return this.client.helper.replyMessage(this.channelId, this.id, options);
174
+ }
175
+ async sendToChannel(options) {
176
+ return this.client.helper.createMessage(this.channelId, options);
177
+ }
178
+ toJSON() {
179
+ return this.data;
180
+ }
181
+ toString() {
182
+ return this.data.content;
183
+ }
184
+ };
63
185
 
64
- // src/lib/structures/message.ts
65
- function createMessage(client, data) {
66
- let author, mentions, referencedMessage;
67
- return {
68
- get client() {
69
- return client;
70
- },
71
- get id() {
72
- return data.id;
73
- },
74
- get channelId() {
75
- return data.channel_id;
76
- },
186
+ // src/lib/structures/channel/BaseChannel.ts
187
+ var BaseChannel = class extends BaseStructure {
188
+ constructor(client, data) {
189
+ super(client);
190
+ this.data = data;
191
+ }
192
+ get id() {
193
+ return this.data.id;
194
+ }
195
+ get type() {
196
+ return this.data.type;
197
+ }
198
+ toString() {
199
+ return `<#${this.id}>`;
200
+ }
201
+ toJSON() {
202
+ return this.data;
203
+ }
204
+ isTextBased() {
205
+ return "send" in this;
206
+ }
207
+ isVoiceBased() {
208
+ return "join" in this;
209
+ }
210
+ inGuild() {
211
+ return "guild_id" in this.data;
212
+ }
213
+ _patch(data) {
214
+ this.data = { ...this.data, ...data };
215
+ }
216
+ };
217
+ var TextBasedChannelMixin = createMixin((base) => {
218
+ class TextBasedChannel extends base {
219
+ send(options) {
220
+ return this.client.helper.createMessage(this.id, options);
221
+ }
222
+ }
223
+ return TextBasedChannel;
224
+ }), VoiceBasedChannelMixin = createMixin((base) => {
225
+ class VoiceBasedChannel extends base {
226
+ }
227
+ return VoiceBasedChannel;
228
+ }), GuildChannelMixin = createMixin((base) => {
229
+ class GuildChannel extends base {
77
230
  get guildId() {
78
- if ("guild_id" in data)
79
- return data.guild_id;
80
- },
81
- get author() {
82
- return author || (author = createUser(client, data.author)), author;
83
- },
84
- get content() {
85
- return data.content;
86
- },
87
- get timestamp() {
88
- return data.timestamp;
89
- },
90
- get editedTimestamp() {
91
- return data.edited_timestamp ?? void 0;
92
- },
93
- get tts() {
94
- return data.tts;
95
- },
96
- get mentionEveryone() {
97
- return data.mention_everyone;
98
- },
99
- get mentions() {
100
- return mentions || (mentions = data.mentions.map((x) => createUser(client, x))), mentions;
101
- },
102
- get mentionRoles() {
103
- return data.mention_roles;
104
- },
105
- get nonce() {
106
- return data.nonce;
107
- },
108
- get pinned() {
109
- return data.pinned;
110
- },
111
- get webhookId() {
112
- return data.webhook_id;
113
- },
114
- get type() {
115
- return data.type;
116
- },
117
- get applicationId() {
118
- return data.application_id;
119
- },
120
- get flags() {
121
- return data.flags;
122
- },
123
- get referencedMessage() {
124
- if (data.referenced_message)
125
- return referencedMessage || (referencedMessage = createMessage(client, data.referenced_message)), referencedMessage;
126
- },
127
- get position() {
128
- return data.position;
231
+ if (!this.data.guild_id)
232
+ throw new Error("This channel is not a guild channel");
233
+ return this.data.guild_id;
129
234
  }
130
- };
131
- }
235
+ get guild() {
236
+ return this.client.guilds.get(this.guildId);
237
+ }
238
+ }
239
+ return GuildChannel;
240
+ });
241
+ var GuildTextChannel = class extends applyMixins(BaseChannel, [
242
+ GuildChannelMixin,
243
+ TextBasedChannelMixin
244
+ ]) {
245
+ };
246
+ var GuildVoiceChannel = class extends applyMixins(BaseChannel, [
247
+ GuildChannelMixin,
248
+ TextBasedChannelMixin,
249
+ VoiceBasedChannelMixin
250
+ ]) {
251
+ };
132
252
  function createChannel(client, data) {
133
253
  switch (data.type) {
134
254
  case ChannelType.GuildText:
135
- return createGuildTextChannel(client, data);
136
- case ChannelType.DM:
137
- return createDMChannel(client, data);
138
- case ChannelType.PublicThread:
139
- return createPublicThreadChannel(client, data);
140
- case ChannelType.PrivateThread:
141
- return createPrivateThreadChannel(client, data);
255
+ return new GuildTextChannel(client, data);
142
256
  case ChannelType.GuildVoice:
143
- return createVoiceChannel(client, data);
144
- case ChannelType.GuildStageVoice:
145
- return createStageChannel(client, data);
257
+ return new GuildVoiceChannel(client, data);
146
258
  default:
147
- return createBaseChannel(client, data);
259
+ return null;
148
260
  }
149
261
  }
150
- function createBaseChannel(client, data) {
151
- return {
152
- get client() {
153
- return client;
154
- },
155
- get id() {
156
- return data.id;
157
- },
158
- get type() {
159
- return data.type;
160
- },
161
- isTextBased() {
162
- return data.type === ChannelType.GuildText || data.type === ChannelType.DM || data.type === ChannelType.PublicThread || data.type === ChannelType.PrivateThread;
163
- },
164
- isVoiceBased() {
165
- return data.type === ChannelType.GuildVoice || data.type === ChannelType.GuildStageVoice;
166
- },
167
- isGuildBased() {
168
- return data.type === ChannelType.GuildText || data.type === ChannelType.GuildVoice || data.type === ChannelType.GuildStageVoice || data.type === ChannelType.PublicThread || data.type === ChannelType.PrivateThread;
169
- },
170
- isThreadBased() {
171
- return data.type === ChannelType.PublicThread || data.type === ChannelType.PrivateThread;
172
- },
173
- isDm() {
174
- return data.type === ChannelType.DM;
175
- },
176
- isGuildText() {
177
- return data.type === ChannelType.GuildText;
178
- },
179
- isVoice() {
180
- return data.type === ChannelType.GuildVoice;
181
- },
182
- isStage() {
183
- return data.type === ChannelType.GuildStageVoice;
184
- },
185
- isPublicThread() {
186
- return data.type === ChannelType.PublicThread;
187
- },
188
- isPrivateThread() {
189
- return data.type === ChannelType.PrivateThread;
190
- }
191
- };
192
- }
193
- function createGuildTextChannel(client, data) {
194
- return {
195
- ...createBaseChannel(client, data),
196
- ...createGuildBasedChannelSingleton(data),
197
- ...createTextBasedChannelSingleton(client, data)
198
- };
199
- }
200
- function createDMChannel(client, data) {
201
- let recipients;
202
- return {
203
- ...createBaseChannel(client, data),
204
- ...createTextBasedChannelSingleton(client, data),
205
- get recipients() {
206
- if (data.recipients)
207
- return recipients || (recipients = data.recipients.map((x) => createUser(client, x))), recipients;
208
- }
209
- };
210
- }
211
- function createPublicThreadChannel(client, data) {
212
- return {
213
- ...createBaseChannel(client, data),
214
- ...createThreadBasedChannelSingleton(client, data)
215
- };
216
- }
217
- function createPrivateThreadChannel(client, data) {
218
- return {
219
- ...createBaseChannel(client, data),
220
- ...createThreadBasedChannelSingleton(client, data)
221
- };
222
- }
223
- function createVoiceChannel(client, data) {
224
- return {
225
- ...createBaseChannel(client, data),
226
- ...createVoiceBasedChannelSingleton(),
227
- ...createGuildBasedChannelSingleton(data),
228
- ...createTextBasedChannelSingleton(client, data)
229
- };
230
- }
231
- function createStageChannel(client, data) {
232
- return {
233
- ...createBaseChannel(client, data),
234
- ...createVoiceBasedChannelSingleton(),
235
- ...createGuildBasedChannelSingleton(data),
236
- ...createTextBasedChannelSingleton(client, data)
237
- };
238
- }
239
- function createTextBasedChannelSingleton(client, data) {
240
- return {
241
- get lastMessageId() {
242
- return data.last_message_id ?? void 0;
243
- },
244
- async send(options) {
245
- return client.helpers.channel.createMessage(data.id, options);
246
- }
247
- };
248
- }
249
- function createVoiceBasedChannelSingleton(_client, _data) {
250
- return {
251
- async join() {
252
- }
253
- };
254
- }
255
- function createGuildBasedChannelSingleton(data) {
256
- return {
257
- get name() {
258
- return data.name;
259
- },
260
- get guildId() {
261
- return data.guild_id;
262
- },
263
- get parentId() {
264
- return data.parent_id ?? void 0;
265
- }
266
- };
267
- }
268
- function createGuildTextBasedChannelSingleton(client, data) {
269
- return {
270
- ...createGuildBasedChannelSingleton(data),
271
- ...createTextBasedChannelSingleton(client, data)
272
- };
273
- }
274
- function createThreadBasedChannelSingleton(client, data) {
275
- return {
276
- ...createGuildTextBasedChannelSingleton(client, data),
277
- get messageCount() {
278
- return data.message_count;
279
- },
280
- get memberCount() {
281
- return data.member_count;
282
- },
283
- get ownerId() {
284
- return data.owner_id;
285
- },
286
- get totalMessageSent() {
287
- return data.total_message_sent;
288
- },
289
- get appliedTags() {
290
- return data.applied_tags;
291
- }
292
- };
293
- }
294
262
 
295
- // src/lib/client/helpers/channel.ts
296
- function createChannelHelpers(client) {
297
- return {
298
- async getById(id) {
299
- try {
300
- let data = await client.rest.get(`/channels/${id}`);
301
- return createChannel(client, data);
302
- } catch {
303
- return;
263
+ // src/lib/structures/Guild.ts
264
+ var Guild = class extends BaseStructure {
265
+ constructor(client, data) {
266
+ super(client);
267
+ this.data = data;
268
+ }
269
+ cachedChannels;
270
+ get id() {
271
+ return this.data.id;
272
+ }
273
+ get name() {
274
+ return this.data.name;
275
+ }
276
+ get icon() {
277
+ return this.data.icon;
278
+ }
279
+ get banner() {
280
+ return this.data.banner;
281
+ }
282
+ get owner() {
283
+ return this.data.owner_id;
284
+ }
285
+ get mfaLevel() {
286
+ return this.data.mfa_level;
287
+ }
288
+ get verificationLevel() {
289
+ return this.data.verification_level;
290
+ }
291
+ get channels() {
292
+ if (!this.cachedChannels && (this.cachedChannels = new Collection(), "channels" in this.data))
293
+ for (let data of this.data.channels) {
294
+ let channel = createChannel(this.client, data);
295
+ channel && this.cachedChannels.set(channel.id, channel);
304
296
  }
305
- },
306
- async createMessage(channelId, options) {
307
- typeof options == "string" && (options = { content: options });
308
- let message = await client.rest.post(
309
- `/channels/${channelId}/messages`,
310
- {
311
- content: options.content,
312
- flags: options.flags
313
- }
314
- );
315
- return createMessage(client, message);
316
- },
317
- async deleteMessage(channelId, messageId) {
318
- await client.rest.delete(`/channels/${channelId}/messages/${messageId}`);
319
- }
320
- };
321
- }
322
-
323
- // src/lib/client/helpers/user.ts
324
- function createUserHelpers(client) {
325
- return {
326
- async getById(id) {
327
- try {
328
- let data = await client.rest.get(`/users/${id}`);
329
- return createUser(client, data);
330
- } catch {
331
- return;
297
+ return this.cachedChannels;
298
+ }
299
+ _patch(data) {
300
+ this.data = { ...this.data, ...data };
301
+ }
302
+ // public get roles() {
303
+ // return this.data.roles;
304
+ // }
305
+ };
306
+ var ClientHelper = class _ClientHelper {
307
+ constructor(client) {
308
+ Object.defineProperty(this, "client", {
309
+ value: client,
310
+ enumerable: false,
311
+ writable: false
312
+ });
313
+ }
314
+ async fetchMessage(channelId, messageId) {
315
+ let data = await this.client.rest.get(Routes.channelMessage(channelId, messageId));
316
+ return new Message(this.client, data);
317
+ }
318
+ async deleteMessage(channelId, messageId) {
319
+ return this.client.rest.delete(Routes.channelMessage(channelId, messageId));
320
+ }
321
+ async createMessage(channelId, options) {
322
+ return this.client.rest.post(Routes.channelMessages(channelId), {
323
+ body: _ClientHelper.toAPICreateMessagePayload(options)
324
+ });
325
+ }
326
+ async replyMessage(channelId, messageId, options) {
327
+ return this.createMessage(channelId, {
328
+ ...options,
329
+ messageReference: {
330
+ channel_id: channelId,
331
+ message_id: messageId
332
332
  }
333
- },
334
- async getCurrent() {
335
- let data = await client.rest.get("/users/@me");
336
- return createUser(client, data);
337
- }
338
- };
339
- }
333
+ });
334
+ }
335
+ async fetchGuild(guildId) {
336
+ let data = await this.client.rest.get(Routes.guild(guildId));
337
+ return new Guild(this.client, data);
338
+ }
339
+ static toAPICreateMessagePayload(options) {
340
+ return {
341
+ content: options.content,
342
+ tts: options.tts,
343
+ embeds: options.embeds,
344
+ allowed_mentions: options.allowedMentions,
345
+ message_reference: options.messageReference,
346
+ flags: options.flags
347
+ };
348
+ }
349
+ };
340
350
 
341
- // src/lib/client/helpers/index.ts
342
- function createHelpers(client) {
343
- return {
344
- user: createUserHelpers(client),
345
- channel: createChannelHelpers(client)
346
- };
347
- }
351
+ // src/lib/client/Partial.ts
352
+ var Partial = /* @__PURE__ */ ((Partial2) => (Partial2[Partial2.Message = 0] = "Message", Partial2[Partial2.User = 1] = "User", Partial2))(Partial || {});
353
+ var IntentsBitField = class _IntentsBitField {
354
+ value;
355
+ constructor(input) {
356
+ this.value = _IntentsBitField.resolve(input);
357
+ }
358
+ static resolve(input) {
359
+ return Array.isArray(input) ? input.reduce((acc, intent) => acc | BigInt(intent), 0n) : BigInt(input);
360
+ }
361
+ has(intent) {
362
+ return (this.value & BigInt(intent)) !== 0n;
363
+ }
364
+ toBigInt() {
365
+ return this.value;
366
+ }
367
+ toNumber() {
368
+ return Number(this.value);
369
+ }
370
+ toJSON() {
371
+ return this.value.toString();
372
+ }
373
+ toString() {
374
+ return this.value.toString();
375
+ }
376
+ };
348
377
 
349
- // src/lib/client/index.ts
350
- function createClient(rest) {
351
- let self = attachEventBus({
352
- rest,
353
- helpers: void 0
354
- });
355
- return Object.assign(self, { helpers: createHelpers(self) }), self.on("raw", (payload) => {
356
- switch (payload.t) {
357
- case GatewayDispatchEvents.MessageCreate:
358
- self.emit("messageCreate", createMessage(self, payload.d));
359
- return;
378
+ // src/lib/client/Client.ts
379
+ var Client = class extends EventEmitter {
380
+ options;
381
+ shards;
382
+ rest;
383
+ helper;
384
+ guilds = new Collection();
385
+ #user;
386
+ #ready = false;
387
+ constructor(options, rest) {
388
+ super();
389
+ let intents = options.intents instanceof IntentsBitField ? options.intents : new IntentsBitField(options.intents), sharding = {
390
+ shardsPerCluster: options.sharding?.shardsPerCluster ?? 1,
391
+ totalShards: options.sharding?.totalShards ?? 1
392
+ };
393
+ this.options = {
394
+ token: options.token,
395
+ intents,
396
+ sharding,
397
+ partials: new Set(options.partials ?? [])
398
+ }, this.rest = rest ?? new REST({ token: options.token }), this.shards = new ShardingManager(
399
+ {
400
+ ...this.options.sharding,
401
+ token: this.options.token,
402
+ intents: this.options.intents.toBigInt()
403
+ },
404
+ rest
405
+ ), this.helper = new ClientHelper(this), this.#setupShardsListeners();
406
+ }
407
+ get user() {
408
+ return this.#user;
409
+ }
410
+ async login() {
411
+ if (this.#user || this.shards.ready || this.shards.clusters.size > 0)
412
+ throw new Error("Cannot login twice");
413
+ return await this.shards.spawn(), new Promise((resolve, reject) => {
414
+ let cleanup = () => {
415
+ this.removeListener("ready", onReady), this.removeListener("error", onError);
416
+ }, onReady = () => {
417
+ cleanup(), resolve();
418
+ }, onError = (error) => {
419
+ cleanup(), reject(error);
420
+ };
421
+ this.once("ready", onReady), this.once("error", onError);
422
+ });
423
+ }
424
+ #setupShardsListeners() {
425
+ this.shards.on("dispatch", (_, shardId, payload) => this.#handleDispatch(shardId, payload)), this.shards.on("raw", (_, shardId, payload) => this.emit("raw", shardId, payload)), this.shards.once("ready", () => this.#tryEmitReady());
426
+ }
427
+ async #handleDispatch(shardId, payload) {
428
+ switch (this.emit("dispatch", shardId, payload), payload.t) {
429
+ case GatewayDispatchEvents.Ready: {
430
+ this.#user ??= new User(this, payload.d.user), this.#tryEmitReady();
431
+ break;
432
+ }
433
+ case GatewayDispatchEvents.MessageCreate: {
434
+ let message = new Message(this, payload.d);
435
+ if (message.partial && !this.options.partials.has(0 /* Message */))
436
+ return;
437
+ this.emit("messageCreate", message);
438
+ break;
439
+ }
440
+ case GatewayDispatchEvents.MessageUpdate: {
441
+ let message = new Message(this, payload.d);
442
+ if (message.partial && !this.options.partials.has(0 /* Message */))
443
+ return;
444
+ this.emit("messageUpdate", message);
445
+ break;
446
+ }
447
+ case GatewayDispatchEvents.MessageDelete: {
448
+ this.emit("messageDelete", {
449
+ id: payload.d.id,
450
+ channelId: payload.d.channel_id,
451
+ guildId: payload.d.guild_id
452
+ });
453
+ break;
454
+ }
455
+ case GatewayDispatchEvents.UserUpdate: {
456
+ payload.d.id === this.#user?.id && (this.#user = new User(this, payload.d)), this.emit("userUpdate", new User(this, payload.d));
457
+ break;
458
+ }
459
+ case GatewayDispatchEvents.GuildCreate: {
460
+ let guild = this.guilds.get(payload.d.id);
461
+ guild ? guild._patch(payload.d) : guild = new Guild(this, payload.d), this.guilds.set(payload.d.id, guild), payload.d.unavailable ? this.emit("guildCreate", guild) : this.emit("guildAvailable", guild);
462
+ break;
463
+ }
464
+ case GatewayDispatchEvents.GuildDelete: {
465
+ let guild = this.guilds.get(payload.d.id);
466
+ if (!guild)
467
+ return;
468
+ this.guilds.delete(payload.d.id), this.emit("guildDelete", guild);
469
+ break;
470
+ }
471
+ case GatewayDispatchEvents.GuildUpdate: {
472
+ let guild = this.guilds.get(payload.d.id);
473
+ guild ? guild._patch(payload.d) : (guild = new Guild(this, payload.d), this.guilds.set(payload.d.id, guild)), this.emit("guildUpdate", guild);
474
+ break;
475
+ }
476
+ case GatewayDispatchEvents.ChannelCreate: {
477
+ let channel = createChannel(this, payload.d);
478
+ if (!channel)
479
+ return;
480
+ channel.inGuild() && channel.guild.channels.set(channel.id, channel), this.emit("channelCreate", channel);
481
+ break;
482
+ }
483
+ case GatewayDispatchEvents.ChannelUpdate: {
484
+ let guild = this.guilds.get(payload.d.guild_id);
485
+ if (!guild)
486
+ return;
487
+ let channel = guild.channels.get(payload.d.id);
488
+ if (channel)
489
+ channel._patch(payload.d);
490
+ else {
491
+ if (channel = createChannel(this, payload.d), !channel)
492
+ return;
493
+ guild.channels.set(payload.d.id, channel);
494
+ }
495
+ this.emit("channelUpdate", channel);
496
+ break;
497
+ }
498
+ case GatewayDispatchEvents.ChannelDelete: {
499
+ let guild = this.guilds.get(payload.d.guild_id);
500
+ if (!guild)
501
+ return;
502
+ let channel = guild.channels.get(payload.d.id);
503
+ if (!channel)
504
+ return;
505
+ guild.channels.delete(payload.d.id), this.emit("channelDelete", channel);
506
+ break;
507
+ }
360
508
  }
361
- }), self;
509
+ }
510
+ /**
511
+ * We use this method to emit ready event only once since we have 2 ways to emit it.
512
+ * @private
513
+ */
514
+ #tryEmitReady() {
515
+ this.#ready || !this.#user || !this.shards.ready || (this.#ready = true, this.emit("ready", this.#user));
516
+ }
517
+ };
518
+ function createClient(options) {
519
+ return new Client(options);
362
520
  }
363
521
 
364
- export { createBaseChannel, createChannel, createClient, createDMChannel, createGuildBasedChannelSingleton, createGuildTextBasedChannelSingleton, createGuildTextChannel, createMessage, createPrivateThreadChannel, createPublicThreadChannel, createStageChannel, createTextBasedChannelSingleton, createThreadBasedChannelSingleton, createUser, createVoiceBasedChannelSingleton, createVoiceChannel };
522
+ export { BaseChannel, Client, ClientHelper, Guild, GuildTextChannel, GuildVoiceChannel, IntentsBitField, Message, Partial, User, createClient };