bakit 3.0.1 → 3.1.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.cjs CHANGED
@@ -1 +1,1135 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`@bakit/gateway`),l=require(`@bakit/rest`),u=require(`@bakit/service`),d=require(`@bakit/utils`),f=require(`node:events`);f=s(f);let p=require(`@discordjs/collection`),m=require(`discord-api-types/v10`);m=s(m);let h=require(`tiny-mixin`);var g=class{constructor(e){Object.defineProperty(this,`client`,{value:e,enumerable:!1,writable:!1})}toJSON(){return{}}},_=class extends g{constructor(e,t){super(e),this.data=t}get id(){return this.data.id}get username(){return this.data.username}get discriminator(){return this.data.discriminator}get globalName(){return this.data.global_name}get tag(){return this.discriminator!==`0`&&this.discriminator!==`0000`?`${this.username}#${this.discriminator}`:this.username}get displayName(){return this.globalName??this.tag}get avatar(){return this.data.avatar??void 0}get bot(){return!!this.data.bot}get system(){return!!this.data.system}get mfaEnabled(){return this.data.mfa_enabled}get locale(){return this.data.locale}get verified(){return this.data.verified}get email(){return this.data.email}get flags(){return this.data.flags}get premiumType(){return this.data.premium_type}get publicFlags(){return this.data.public_flags}get banner(){return this.data.banner}get accentColor(){return this.data.accent_color??void 0}get createdAt(){return(0,d.getSnowflakeDate)(this.id)}get createdTimestamp(){return this.createdAt.getTime()}get hexAccentColor(){return this.accentColor===void 0?void 0:`#${this.accentColor.toString(16).padStart(6,`0`)}`}get defaultAvatarURL(){return`https://cdn.discordapp.com/embed/avatars/${this.discriminator!==`0`&&this.discriminator!==`0000`?BigInt(this.discriminator)%5n:(BigInt(this.id)>>22n)%6n}.png`}getAvatarURL(e){if(!this.avatar)return;let t=this.avatar.startsWith(`a_`),n=e?.extension??(t?`gif`:`png`),r=e?.size?`?size=${e.size}`:``;return`https://cdn.discordapp.com/avatars/${this.id}/${this.avatar}.${n}${r}`}getDisplayAvatarURL(e){return this.getAvatarURL(e)??this.defaultAvatarURL}equals(e){return typeof e==`string`?e===this.id:e.id===this.id&&e.username===this.username&&e.discriminator===this.discriminator&&e.bot===this.bot&&e.createdTimestamp===this.createdTimestamp}toJSON(){return this.data}toString(){return`<@${this.id}>`}},v=class extends g{cachedAuthor;constructor(e,t){super(e),this.data=t}get partial(){return typeof this.data.content!=`string`||typeof this.data.author!=`object`}get content(){return this.data.content}get id(){return this.data.id}get channelId(){return this.data.channel_id}get channel(){return this.guild.channels.get(this.channelId)}get guildId(){return`guild_id`in this.data?this.data.guild_id:void 0}get guild(){return this.guildId?this.client.guilds.get(this.guildId):void 0}get author(){return this.cachedAuthor??=new _(this.client,this.data.author),this.cachedAuthor}get createdAt(){return new Date(this.data.timestamp)}get editedAt(){return this.data.edited_timestamp?new Date(this.data.edited_timestamp):void 0}get createdTimestamp(){return this.createdAt.getTime()}get editedTimestamp(){return this.editedAt?.getTime()}get url(){return`https://discord.com/channels/${this.guildId??`@me`}/${this.channelId}/${this.id}`}async fetch(){let e=await this.client.helper.fetchMessage(this.channelId,this.id);return this.data=e.data,this.cachedAuthor=e.cachedAuthor,this}async reply(e){return this.client.helper.replyMessage(this.channelId,this.id,e)}async sendToChannel(e){return this.client.helper.createMessage(this.channelId,e)}toJSON(){return this.data}toString(){return this.data.content}},y=class extends g{constructor(e,t){super(e),this.data=t}get id(){return this.data.id}get type(){return this.data.type}toString(){return`<#${this.id}>`}toJSON(){return this.data}isTextBased(){return`send`in this}isVoiceBased(){return`join`in this}inGuild(){return`guild_id`in this.data}_patch(e){this.data={...this.data,...e}}};const b=(0,h.createMixin)(e=>{class t extends e{send(e){return this.client.helper.createMessage(this.id,e)}}return t}),x=(0,h.createMixin)(e=>{class t extends e{}return t}),S=(0,h.createMixin)(e=>{class t extends e{get guildId(){if(!this.data.guild_id)throw Error(`This channel is not a guild channel`);return this.data.guild_id}get guild(){return this.client.guilds.get(this.guildId)}}return t});var C=class extends (0,h.applyMixins)(y,[S,b]){},w=class extends (0,h.applyMixins)(y,[S,b,x]){};function T(e,t){switch(t.type){case m.ChannelType.GuildText:return new C(e,t);case m.ChannelType.GuildVoice:return new w(e,t);default:return null}}var E=class extends g{cachedChannels;constructor(e,t){super(e),this.data=t}get id(){return this.data.id}get name(){return this.data.name}get icon(){return this.data.icon}get banner(){return this.data.banner}get owner(){return this.data.owner_id}get mfaLevel(){return this.data.mfa_level}get verificationLevel(){return this.data.verification_level}get channels(){if(!this.cachedChannels&&(this.cachedChannels=new p.Collection,`channels`in this.data))for(let e of this.data.channels){let t=T(this.client,e);t&&this.cachedChannels.set(t.id,t)}return this.cachedChannels}_patch(e){this.data={...this.data,...e}}},D=class e{constructor(e){Object.defineProperty(this,`client`,{value:e,enumerable:!1,writable:!1})}async fetchMessage(e,t){let n=await this.client.rest.get(m.Routes.channelMessage(e,t));return new v(this.client,n)}async deleteMessage(e,t){return this.client.rest.delete(m.Routes.channelMessage(e,t))}async createMessage(t,n){return this.client.rest.post(m.Routes.channelMessages(t),{body:e.toAPICreateMessagePayload(n)})}async replyMessage(e,t,n){return this.createMessage(e,{...n,messageReference:{channel_id:e,message_id:t}})}async fetchGuild(e){let t=await this.client.rest.get(m.Routes.guild(e));return new E(this.client,t)}static toAPICreateMessagePayload(e){return{content:e.content,tts:e.tts,embeds:e.embeds,allowed_mentions:e.allowedMentions,message_reference:e.messageReference,flags:e.flags}}};let O=function(e){return e[e.Message=0]=`Message`,e[e.User=1]=`User`,e}({});var k=class e{value;constructor(t){this.value=e.resolve(t)}static resolve(e){return Array.isArray(e)?e.reduce((e,t)=>e|BigInt(t),0n):BigInt(e)}has(e){return(this.value&BigInt(e))!==0n}toBigInt(){return this.value}toNumber(){return Number(this.value)}toJSON(){return this.value.toString()}toString(){return this.value.toString()}},A=class extends f.default{options;shards;rest;helper;guilds=new p.Collection;#e;#t=!1;constructor(e,t){super();let n=e.intents instanceof k?e.intents:new k(e.intents),r={shardsPerCluster:e.sharding?.shardsPerCluster??1,totalShards:e.sharding?.totalShards??1};this.options={token:e.token,intents:n,sharding:r,partials:new Set(e.partials??[])},this.rest=t??new l.REST({token:e.token}),this.shards=new c.ShardingManager({...this.options.sharding,token:this.options.token,intents:this.options.intents.toBigInt()},t),this.helper=new D(this),this.#n()}get user(){return this.#e}async login(){if(this.#e||this.shards.ready||this.shards.clusters.size>0)throw Error(`Cannot login twice`);return await this.shards.spawn(),new Promise((e,t)=>{let n=()=>{this.removeListener(`ready`,r),this.removeListener(`error`,i)},r=()=>{n(),e()},i=e=>{n(),t(e)};this.once(`ready`,r),this.once(`error`,i)})}#n(){this.shards.on(`dispatch`,(e,t,n)=>this.#r(t,n)),this.shards.on(`raw`,(e,t,n)=>this.emit(`raw`,t,n)),this.shards.once(`ready`,()=>this.#i())}async#r(e,t){switch(this.emit(`dispatch`,e,t),t.t){case m.GatewayDispatchEvents.Ready:this.#e??=new _(this,t.d.user),this.#i();break;case m.GatewayDispatchEvents.MessageCreate:{let e=new v(this,t.d);if(e.partial&&!this.options.partials.has(O.Message))return;this.emit(`messageCreate`,e);break}case m.GatewayDispatchEvents.MessageUpdate:{let e=new v(this,t.d);if(e.partial&&!this.options.partials.has(O.Message))return;this.emit(`messageUpdate`,e);break}case m.GatewayDispatchEvents.MessageDelete:this.emit(`messageDelete`,{id:t.d.id,channelId:t.d.channel_id,guildId:t.d.guild_id});break;case m.GatewayDispatchEvents.UserUpdate:t.d.id===this.#e?.id&&(this.#e=new _(this,t.d)),this.emit(`userUpdate`,new _(this,t.d));break;case m.GatewayDispatchEvents.GuildCreate:{let e=this.guilds.get(t.d.id);e?e._patch(t.d):e=new E(this,t.d),this.guilds.set(t.d.id,e),t.d.unavailable?this.emit(`guildCreate`,e):this.emit(`guildAvailable`,e);break}case m.GatewayDispatchEvents.GuildDelete:{let e=this.guilds.get(t.d.id);if(!e)return;this.guilds.delete(t.d.id),this.emit(`guildDelete`,e);break}case m.GatewayDispatchEvents.GuildUpdate:{let e=this.guilds.get(t.d.id);e?e._patch(t.d):(e=new E(this,t.d),this.guilds.set(t.d.id,e)),this.emit(`guildUpdate`,e);break}case m.GatewayDispatchEvents.ChannelCreate:{let e=T(this,t.d);if(!e)return;e.inGuild()&&e.guild.channels.set(e.id,e),this.emit(`channelCreate`,e);break}case m.GatewayDispatchEvents.ChannelUpdate:{let e=this.guilds.get(t.d.guild_id);if(!e)return;let n=e.channels.get(t.d.id);if(n)n._patch(t.d);else{if(n=T(this,t.d),!n)return;e.channels.set(t.d.id,n)}this.emit(`channelUpdate`,n);break}case m.GatewayDispatchEvents.ChannelDelete:{let e=this.guilds.get(t.d.guild_id);if(!e)return;let n=e.channels.get(t.d.id);if(!n)return;e.channels.delete(t.d.id),this.emit(`channelDelete`,n);break}}}#i(){this.#t||!this.#e||!this.shards.ready||(this.#t=!0,this.emit(`ready`,this.#e))}};function j(e){return new A(e)}exports.BaseChannel=y,exports.Client=A,exports.ClientHelper=D,Object.defineProperty(exports,`Cluster`,{enumerable:!0,get:function(){return c.Cluster}}),Object.defineProperty(exports,`DEFAULT_REST_OPTIONS`,{enumerable:!0,get:function(){return l.DEFAULT_REST_OPTIONS}}),Object.defineProperty(exports,`DiscordAPIV10`,{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,`DiscordHTTPError`,{enumerable:!0,get:function(){return l.DiscordHTTPError}}),exports.Guild=E,exports.GuildTextChannel=C,exports.GuildVoiceChannel=w,Object.defineProperty(exports,`Intent`,{enumerable:!0,get:function(){return m.GatewayIntentBits}}),exports.IntentsBitField=k,exports.Message=v,exports.Partial=O,Object.defineProperty(exports,`REST`,{enumerable:!0,get:function(){return l.REST}}),Object.defineProperty(exports,`RESTAdapter`,{enumerable:!0,get:function(){return l.RESTAdapter}}),Object.defineProperty(exports,`RESTMethod`,{enumerable:!0,get:function(){return l.RESTMethod}}),Object.defineProperty(exports,`Service`,{enumerable:!0,get:function(){return u.Service}}),Object.defineProperty(exports,`ServiceDriver`,{enumerable:!0,get:function(){return u.ServiceDriver}}),Object.defineProperty(exports,`ServiceProcess`,{enumerable:!0,get:function(){return u.ServiceProcess}}),Object.defineProperty(exports,`ServiceState`,{enumerable:!0,get:function(){return u.ServiceState}}),Object.defineProperty(exports,`Shard`,{enumerable:!0,get:function(){return c.Shard}}),Object.defineProperty(exports,`ShardState`,{enumerable:!0,get:function(){return c.ShardState}}),Object.defineProperty(exports,`ShardStrategy`,{enumerable:!0,get:function(){return c.ShardStrategy}}),Object.defineProperty(exports,`ShardingManager`,{enumerable:!0,get:function(){return c.ShardingManager}}),Object.defineProperty(exports,`TransportClient`,{enumerable:!0,get:function(){return u.TransportClient}}),Object.defineProperty(exports,`TransportServer`,{enumerable:!0,get:function(){return u.TransportServer}}),exports.User=_,Object.defineProperty(exports,`capitalize`,{enumerable:!0,get:function(){return d.capitalize}}),exports.createClient=j,Object.defineProperty(exports,`createREST`,{enumerable:!0,get:function(){return l.createREST}}),Object.defineProperty(exports,`createRESTAdapter`,{enumerable:!0,get:function(){return l.createRESTAdapter}}),Object.defineProperty(exports,`createService`,{enumerable:!0,get:function(){return u.createService}}),Object.defineProperty(exports,`createTransportClient`,{enumerable:!0,get:function(){return u.createTransportClient}}),Object.defineProperty(exports,`createTransportServer`,{enumerable:!0,get:function(){return u.createTransportServer}}),Object.defineProperty(exports,`instanceToObject`,{enumerable:!0,get:function(){return d.instanceToObject}}),Object.defineProperty(exports,`isCommonJS`,{enumerable:!0,get:function(){return d.isCommonJS}}),Object.defineProperty(exports,`isESM`,{enumerable:!0,get:function(){return d.isESM}}),Object.defineProperty(exports,`isPlainObject`,{enumerable:!0,get:function(){return d.isPlainObject}}),Object.defineProperty(exports,`isPromiseLike`,{enumerable:!0,get:function(){return d.isPromiseLike}}),Object.defineProperty(exports,`promisify`,{enumerable:!0,get:function(){return d.promisify}}),Object.defineProperty(exports,`sleep`,{enumerable:!0,get:function(){return d.sleep}});
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let _bakit_gateway = require("@bakit/gateway");
30
+ let _bakit_rest = require("@bakit/rest");
31
+ let _bakit_service = require("@bakit/service");
32
+ let _bakit_utils = require("@bakit/utils");
33
+ let node_events = require("node:events");
34
+ node_events = __toESM(node_events);
35
+ let discord_api_types_v10 = require("discord-api-types/v10");
36
+ discord_api_types_v10 = __toESM(discord_api_types_v10);
37
+ let _discordjs_collection = require("@discordjs/collection");
38
+ let tiny_mixin = require("tiny-mixin");
39
+
40
+ //#region src/lib/structures/BaseStructure.ts
41
+ var BaseStructure = class {
42
+ constructor(client) {
43
+ Object.defineProperty(this, "client", {
44
+ value: client,
45
+ enumerable: false,
46
+ writable: false
47
+ });
48
+ }
49
+ toJSON() {
50
+ return {};
51
+ }
52
+ };
53
+
54
+ //#endregion
55
+ //#region src/lib/structures/User.ts
56
+ var User = class extends BaseStructure {
57
+ constructor(client, data) {
58
+ super(client);
59
+ this.data = data;
60
+ }
61
+ get id() {
62
+ return this.data.id;
63
+ }
64
+ get username() {
65
+ return this.data.username;
66
+ }
67
+ get discriminator() {
68
+ return this.data.discriminator;
69
+ }
70
+ get globalName() {
71
+ return this.data.global_name;
72
+ }
73
+ get tag() {
74
+ return this.discriminator !== "0" && this.discriminator !== "0000" ? `${this.username}#${this.discriminator}` : this.username;
75
+ }
76
+ get displayName() {
77
+ return this.globalName ?? this.tag;
78
+ }
79
+ get avatar() {
80
+ return this.data.avatar ?? void 0;
81
+ }
82
+ get bot() {
83
+ return !!this.data.bot;
84
+ }
85
+ get system() {
86
+ return !!this.data.system;
87
+ }
88
+ get mfaEnabled() {
89
+ return this.data.mfa_enabled;
90
+ }
91
+ get locale() {
92
+ return this.data.locale;
93
+ }
94
+ get verified() {
95
+ return this.data.verified;
96
+ }
97
+ get email() {
98
+ return this.data.email;
99
+ }
100
+ get flags() {
101
+ return this.data.flags;
102
+ }
103
+ get premiumType() {
104
+ return this.data.premium_type;
105
+ }
106
+ get publicFlags() {
107
+ return this.data.public_flags;
108
+ }
109
+ get banner() {
110
+ return this.data.banner;
111
+ }
112
+ get accentColor() {
113
+ return this.data.accent_color ?? void 0;
114
+ }
115
+ get createdAt() {
116
+ return (0, _bakit_utils.getSnowflakeDate)(this.id);
117
+ }
118
+ get createdTimestamp() {
119
+ return this.createdAt.getTime();
120
+ }
121
+ get hexAccentColor() {
122
+ return this.accentColor !== void 0 ? `#${this.accentColor.toString(16).padStart(6, "0")}` : void 0;
123
+ }
124
+ get defaultAvatarURL() {
125
+ return `https://cdn.discordapp.com/embed/avatars/${this.discriminator !== "0" && this.discriminator !== "0000" ? BigInt(this.discriminator) % 5n : (BigInt(this.id) >> 22n) % 6n}.png`;
126
+ }
127
+ getAvatarURL(options) {
128
+ if (!this.avatar) return;
129
+ const isAnimated = this.avatar.startsWith("a_");
130
+ const ext = options?.extension ?? (isAnimated ? "gif" : "png");
131
+ const size = options?.size ? `?size=${options.size}` : "";
132
+ return `https://cdn.discordapp.com/avatars/${this.id}/${this.avatar}.${ext}${size}`;
133
+ }
134
+ getDisplayAvatarURL(options) {
135
+ return this.getAvatarURL(options) ?? this.defaultAvatarURL;
136
+ }
137
+ equals(other) {
138
+ if (typeof other === "string") return other === this.id;
139
+ return other.id === this.id && other.username === this.username && other.discriminator === this.discriminator && other.bot === this.bot && other.createdTimestamp === this.createdTimestamp;
140
+ }
141
+ async createDM(force = false) {
142
+ return this.client.helper.createDM(this.id, force);
143
+ }
144
+ async send(options) {
145
+ return await (await this.createDM()).send(options);
146
+ }
147
+ _patch(data) {
148
+ this.data = {
149
+ ...this.data,
150
+ ...data
151
+ };
152
+ }
153
+ toJSON() {
154
+ return this.data;
155
+ }
156
+ toString() {
157
+ return `<@${this.id}>`;
158
+ }
159
+ };
160
+
161
+ //#endregion
162
+ //#region src/lib/structures/channel/BaseChannel.ts
163
+ var BaseChannel = class extends BaseStructure {
164
+ constructor(client, data) {
165
+ super(client);
166
+ this.data = data;
167
+ }
168
+ get id() {
169
+ return this.data.id;
170
+ }
171
+ get type() {
172
+ return this.data.type;
173
+ }
174
+ toString() {
175
+ return `<#${this.id}>`;
176
+ }
177
+ toJSON() {
178
+ return this.data;
179
+ }
180
+ isTextBased() {
181
+ return "send" in this;
182
+ }
183
+ isVoiceBased() {
184
+ return "join" in this;
185
+ }
186
+ isDM() {
187
+ return this.type === discord_api_types_v10.ChannelType.DM;
188
+ }
189
+ inGuild() {
190
+ return "guild_id" in this.data;
191
+ }
192
+ fetch() {
193
+ return this.client.helper.fetchChannel(this.id, true);
194
+ }
195
+ _patch(data) {
196
+ this.data = {
197
+ ...this.data,
198
+ ...data
199
+ };
200
+ }
201
+ };
202
+
203
+ //#endregion
204
+ //#region src/lib/mixins/ChannelMixin.ts
205
+ const TextBasedChannelMixin = (0, tiny_mixin.createMixin)((base) => {
206
+ class TextBasedChannel extends base {
207
+ send(options) {
208
+ return this.client.helper.createMessage(this.id, options);
209
+ }
210
+ }
211
+ return TextBasedChannel;
212
+ });
213
+ const VoiceBasedChannelMixin = (0, tiny_mixin.createMixin)((base) => {
214
+ class VoiceBasedChannel extends base {}
215
+ return VoiceBasedChannel;
216
+ });
217
+ const GuildChannelMixin = (0, tiny_mixin.createMixin)((base) => {
218
+ class GuildChannel extends base {
219
+ get guildId() {
220
+ if (!this.data.guild_id) throw new Error("This channel is not a guild channel");
221
+ return this.data.guild_id;
222
+ }
223
+ get guild() {
224
+ return this.client.cache.guilds.get(this.guildId);
225
+ }
226
+ }
227
+ return GuildChannel;
228
+ });
229
+
230
+ //#endregion
231
+ //#region src/lib/structures/channel/DMChannel.ts
232
+ var DMChannel = class extends (0, tiny_mixin.applyMixins)(BaseChannel, [TextBasedChannelMixin]) {
233
+ #cachedRecipients;
234
+ constructor(client, data) {
235
+ super(client, data);
236
+ }
237
+ get recipients() {
238
+ if (!this.#cachedRecipients) {
239
+ this.#cachedRecipients = new _discordjs_collection.Collection();
240
+ for (const recipient of this.data.recipients ?? []) {
241
+ let user;
242
+ if (this.client.cache.isModuleEnabled("users")) user = this.client.cache.resolveLocal(this.client.cache.users.local, recipient.id, () => new User(this.client, recipient), (user) => user._patch(recipient));
243
+ else user = new User(this.client, recipient);
244
+ this.#cachedRecipients.set(recipient.id, user);
245
+ }
246
+ }
247
+ return this.#cachedRecipients;
248
+ }
249
+ get recipientId() {
250
+ const id = (this.data.recipients ?? []).find((user) => user.id !== this.client.user.id)?.id;
251
+ if (!id) throw new Error("No recipients found");
252
+ return id;
253
+ }
254
+ get recipient() {
255
+ const user = this.recipients.get(this.recipientId);
256
+ if (!user) throw new Error("Recipient not found");
257
+ return user;
258
+ }
259
+ async fetch() {
260
+ const data = await this.client.rest.post(discord_api_types_v10.Routes.userChannels(), { body: { recipient_id: this.recipientId } });
261
+ this._patch(data);
262
+ await this.#initRecipientsCache();
263
+ return this;
264
+ }
265
+ send(options) {
266
+ return super.send(options);
267
+ }
268
+ async fetchRecipients(force = false) {
269
+ if (!force && this.data.recipients?.length) {
270
+ await this.#initRecipientsCache();
271
+ return this.#cachedRecipients;
272
+ }
273
+ await this.fetch();
274
+ return this.#cachedRecipients;
275
+ }
276
+ async #initRecipientsCache() {
277
+ this.#cachedRecipients = new _discordjs_collection.Collection();
278
+ for (const recipient of this.data.recipients ?? []) {
279
+ let user;
280
+ if (this.client.cache.isModuleEnabled("users")) user = await this.client.cache.resolve(this.client.cache.users, recipient.id, () => new User(this.client, recipient), (user) => user._patch(recipient));
281
+ else user = new User(this.client, recipient);
282
+ this.#cachedRecipients.set(recipient.id, user);
283
+ }
284
+ }
285
+ _patch(data) {
286
+ super._patch(data);
287
+ this.#cachedRecipients = void 0;
288
+ }
289
+ };
290
+
291
+ //#endregion
292
+ //#region src/lib/structures/Message.ts
293
+ var Message = class extends BaseStructure {
294
+ #cachedAuthor;
295
+ constructor(client, data) {
296
+ super(client);
297
+ this.data = data;
298
+ this.#ensureChannelCache();
299
+ }
300
+ get partial() {
301
+ return typeof this.data.content !== "string" || typeof this.data.author !== "object";
302
+ }
303
+ get content() {
304
+ return this.data.content;
305
+ }
306
+ get id() {
307
+ return this.data.id;
308
+ }
309
+ get channelId() {
310
+ return this.data.channel_id;
311
+ }
312
+ get channel() {
313
+ return this.#ensureChannelCache();
314
+ }
315
+ get guildId() {
316
+ return "guild_id" in this.data ? this.data.guild_id : void 0;
317
+ }
318
+ get guild() {
319
+ return this.guildId ? this.client.cache.guilds.get(this.guildId) : void 0;
320
+ }
321
+ /**
322
+ * The author of this message.
323
+ *
324
+ * **Note:** This property returns the cached `User` synchronously.
325
+ * It may be stale if the users cache has short lifetime or uses an asynchronous remote store.
326
+ * To ensure the author is up-to-date, use the `fetchAuthor()` method instead.
327
+ */
328
+ get author() {
329
+ let author = this.#cachedAuthor;
330
+ if (!author && this.client.cache.isModuleEnabled("users")) author = this.client.cache.resolveLocal(this.client.cache.users.local, this.data.author.id, () => new User(this.client, this.data.author), (user) => user._patch(this.data.author));
331
+ if (!author) author = new User(this.client, this.data.author);
332
+ this.#cachedAuthor = author;
333
+ return this.#cachedAuthor;
334
+ }
335
+ get createdAt() {
336
+ return new Date(this.data.timestamp);
337
+ }
338
+ get editedAt() {
339
+ return this.data.edited_timestamp ? new Date(this.data.edited_timestamp) : void 0;
340
+ }
341
+ get createdTimestamp() {
342
+ return this.createdAt.getTime();
343
+ }
344
+ get editedTimestamp() {
345
+ return this.editedAt?.getTime();
346
+ }
347
+ get url() {
348
+ return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`;
349
+ }
350
+ inGuild() {
351
+ return this.guild !== void 0;
352
+ }
353
+ async fetch() {
354
+ const message = await this.client.helper.fetchMessage(this.channelId, this.id);
355
+ this.data = message.data;
356
+ this.#cachedAuthor = await this.client.cache.resolve(this.client.cache.users, message.data.author.id, () => new User(this.client, message.data.author));
357
+ return this;
358
+ }
359
+ /**
360
+ * Fetches the author of this message from cache or Discord API.
361
+ *
362
+ * **Note:** It is recommended to use this method instead of the `author` property
363
+ * if the users cache has short lifetime or is backed by an asynchronous remote cache.
364
+ *
365
+ * @param force - Whether to bypass the cache and force a request to the API.
366
+ * @returns A promise that resolves to the `User` who authored this message.
367
+ */
368
+ async fetchAuthor(force = false) {
369
+ if (!force && this.client.cache.isModuleEnabled("users")) return this.client.cache.resolve(this.client.cache.users, this.data.author.id, () => new User(this.client, this.data.author), (user) => user._patch(this.data.author));
370
+ return this.client.helper.fetchUser(this.data.author.id, force);
371
+ }
372
+ async reply(options) {
373
+ return this.client.helper.replyMessage(this.channelId, this.id, options);
374
+ }
375
+ async sendToChannel(options) {
376
+ return this.client.helper.createMessage(this.channelId, options);
377
+ }
378
+ async edit(options) {
379
+ return this.client.helper.editMessage(this.channelId, this.id, options);
380
+ }
381
+ async _patch(data) {
382
+ this.data = {
383
+ ...this.data,
384
+ ...data
385
+ };
386
+ if (data.author && this.client.cache.isModuleEnabled("users")) if (this.client.cache.isModuleEnabled("users")) this.#cachedAuthor = await this.client.cache.resolve(this.client.cache.users, data.author.id, () => new User(this.client, data.author));
387
+ else this.#cachedAuthor?._patch(data.author);
388
+ }
389
+ #ensureChannelCache() {
390
+ const { client } = this;
391
+ let channel = client.cache.channels.get(this.channelId);
392
+ if (!channel) if (this.inGuild()) channel = this.guild.channels.get(this.channelId);
393
+ else {
394
+ channel = new DMChannel(client, {
395
+ id: this.channelId,
396
+ type: discord_api_types_v10.ChannelType.DM,
397
+ name: null,
398
+ recipients: [this.data.author, this.client.user.data]
399
+ });
400
+ console.log(channel);
401
+ }
402
+ if (!channel) throw new Error(`Channel not found: ${this.channelId}`);
403
+ client.cache.channels.set(this.channelId, channel);
404
+ return channel;
405
+ }
406
+ toJSON() {
407
+ return this.data;
408
+ }
409
+ toString() {
410
+ return this.data.content;
411
+ }
412
+ };
413
+
414
+ //#endregion
415
+ //#region src/lib/structures/channel/GuildTextChannel.ts
416
+ var GuildTextChannel = class extends (0, tiny_mixin.applyMixins)(BaseChannel, [GuildChannelMixin, TextBasedChannelMixin]) {};
417
+
418
+ //#endregion
419
+ //#region src/lib/structures/channel/GuildVoiceChannel.ts
420
+ var GuildVoiceChannel = class extends (0, tiny_mixin.applyMixins)(BaseChannel, [
421
+ GuildChannelMixin,
422
+ TextBasedChannelMixin,
423
+ VoiceBasedChannelMixin
424
+ ]) {};
425
+
426
+ //#endregion
427
+ //#region src/lib/utils/channel.ts
428
+ function createChannel(client, data) {
429
+ switch (data.type) {
430
+ case discord_api_types_v10.ChannelType.GuildText: return new GuildTextChannel(client, data);
431
+ case discord_api_types_v10.ChannelType.GuildVoice: return new GuildVoiceChannel(client, data);
432
+ case discord_api_types_v10.ChannelType.DM: return new DMChannel(client, data);
433
+ default: return null;
434
+ }
435
+ }
436
+
437
+ //#endregion
438
+ //#region src/lib/structures/Guild.ts
439
+ var Guild = class extends BaseStructure {
440
+ channels = new _discordjs_collection.Collection();
441
+ constructor(client, data) {
442
+ super(client);
443
+ this.data = data;
444
+ this.#initChannels();
445
+ }
446
+ get id() {
447
+ return this.data.id;
448
+ }
449
+ get name() {
450
+ return this.data.name;
451
+ }
452
+ get icon() {
453
+ return this.data.icon;
454
+ }
455
+ get banner() {
456
+ return this.data.banner;
457
+ }
458
+ get owner() {
459
+ return this.data.owner_id;
460
+ }
461
+ get mfaLevel() {
462
+ return this.data.mfa_level;
463
+ }
464
+ get verificationLevel() {
465
+ return this.data.verification_level;
466
+ }
467
+ _patch(data) {
468
+ this.data = {
469
+ ...this.data,
470
+ ...data
471
+ };
472
+ this.#initChannels();
473
+ }
474
+ #initChannels() {
475
+ if (!("channels" in this.data)) return;
476
+ for (const id of this.channels.keys()) this.client.cache.channels.delete(id);
477
+ this.channels.clear();
478
+ for (const channelData of this.data.channels) {
479
+ const channel = createChannel(this.client, channelData);
480
+ if (channel) {
481
+ this.channels.set(channel.id, channel);
482
+ this.client.cache.channels.set(channel.id, channel);
483
+ }
484
+ }
485
+ }
486
+ };
487
+
488
+ //#endregion
489
+ //#region src/lib/client/ClientHelper.ts
490
+ var ClientHelper = class ClientHelper {
491
+ constructor(client) {
492
+ Object.defineProperty(this, "client", {
493
+ value: client,
494
+ enumerable: false,
495
+ writable: false
496
+ });
497
+ }
498
+ async fetchUser(userId, force = false) {
499
+ let user;
500
+ if (this.client.cache.isModuleEnabled("users")) user = await this.client.cache.users.get(userId);
501
+ if (!user || force) {
502
+ const data = await this.client.rest.get(discord_api_types_v10.Routes.user(userId));
503
+ if (this.client.cache.isModuleEnabled("users")) user = await this.client.cache.resolve(this.client.cache.users, userId, () => new User(this.client, data), (u) => u._patch(data));
504
+ else user = new User(this.client, data);
505
+ }
506
+ return user;
507
+ }
508
+ async createDM(userId, force = false) {
509
+ if (!force) {
510
+ const channel = this.client.cache.channels.find((c) => c.isDM() && c.recipientId === userId);
511
+ if (channel && channel.isDM()) return channel;
512
+ }
513
+ const data = await this.client.rest.post(discord_api_types_v10.Routes.userChannels(), { body: { recipient_id: userId } });
514
+ const channel = new DMChannel(this.client, data);
515
+ await channel.fetchRecipients();
516
+ this.client.cache.channels.set(channel.id, channel);
517
+ return channel;
518
+ }
519
+ async fetchMessage(channelId, messageId, force = false) {
520
+ let message;
521
+ if (this.client.cache.isModuleEnabled("messages")) message = await this.client.cache.messages.get(messageId);
522
+ if (!message || force) {
523
+ const data = await this.client.rest.get(discord_api_types_v10.Routes.channelMessage(channelId, messageId));
524
+ if (this.client.cache.isModuleEnabled("messages")) message = await this.client.cache.resolve(this.client.cache.messages, messageId, () => new Message(this.client, data), (m) => m._patch(data));
525
+ else message = new Message(this.client, data);
526
+ }
527
+ return message;
528
+ }
529
+ async deleteMessage(channelId, messageId) {
530
+ await this.client.rest.delete(discord_api_types_v10.Routes.channelMessage(channelId, messageId));
531
+ if (this.client.cache.isModuleEnabled("messages")) await this.client.cache.messages.delete(messageId);
532
+ }
533
+ async createMessage(channelId, options) {
534
+ if (typeof options === "string") options = { content: options };
535
+ const data = this.client.rest.post(discord_api_types_v10.Routes.channelMessages(channelId), { body: ClientHelper.toAPICreateMessagePayload(options) });
536
+ const message = new Message(this.client, await data);
537
+ if (this.client.cache.isModuleEnabled("messages")) await this.client.cache.messages.set(message.id, message);
538
+ return message;
539
+ }
540
+ async editMessage(channelId, messageId, options) {
541
+ if (typeof options === "string") options = { content: options };
542
+ const data = await this.client.rest.patch(discord_api_types_v10.Routes.channelMessage(channelId, messageId), { body: ClientHelper.toAPICreateMessagePayload(options) });
543
+ let message;
544
+ if (this.client.cache.isModuleEnabled("messages")) message = await this.client.cache.resolve(this.client.cache.messages, messageId, () => new Message(this.client, data), (m) => m._patch(data));
545
+ else message = new Message(this.client, data);
546
+ return message;
547
+ }
548
+ async replyMessage(channelId, messageId, options) {
549
+ return this.createMessage(channelId, {
550
+ ...options,
551
+ messageReference: {
552
+ channel_id: channelId,
553
+ message_id: messageId
554
+ }
555
+ });
556
+ }
557
+ async fetchGuild(guildId, force = false) {
558
+ let guild = this.client.cache.guilds.get(guildId);
559
+ if (!guild || force) {
560
+ const data = await this.client.rest.get(discord_api_types_v10.Routes.guild(guildId));
561
+ guild = new Guild(this.client, data);
562
+ this.client.cache.guilds.set(guildId, guild);
563
+ }
564
+ return guild;
565
+ }
566
+ async fetchChannel(channelId, force = false) {
567
+ let channel = this.client.cache.channels.get(channelId);
568
+ if (!channel || force) {
569
+ const data = await this.client.rest.get(discord_api_types_v10.Routes.channel(channelId));
570
+ channel = createChannel(this.client, data) ?? void 0;
571
+ if (!channel) throw new Error(`Channel not found: ${channelId}`);
572
+ this.client.cache.channels.set(channelId, channel);
573
+ if (channel.inGuild()) channel.guild.channels.set(channelId, channel);
574
+ }
575
+ return channel;
576
+ }
577
+ static toAPICreateMessagePayload(options) {
578
+ return {
579
+ content: options.content,
580
+ tts: options.tts,
581
+ embeds: options.embeds,
582
+ allowed_mentions: options.allowedMentions,
583
+ message_reference: options.messageReference,
584
+ flags: options.flags
585
+ };
586
+ }
587
+ };
588
+
589
+ //#endregion
590
+ //#region src/lib/client/Partial.ts
591
+ let Partial = /* @__PURE__ */ function(Partial) {
592
+ Partial[Partial["Message"] = 0] = "Message";
593
+ Partial[Partial["User"] = 1] = "User";
594
+ return Partial;
595
+ }({});
596
+
597
+ //#endregion
598
+ //#region src/lib/utils/IntentsBitField.ts
599
+ var IntentsBitField = class IntentsBitField {
600
+ value;
601
+ constructor(input) {
602
+ this.value = IntentsBitField.resolve(input);
603
+ }
604
+ static resolve(input) {
605
+ if (Array.isArray(input)) return input.reduce((acc, intent) => acc | BigInt(intent), 0n);
606
+ return BigInt(input);
607
+ }
608
+ has(intent) {
609
+ return (this.value & BigInt(intent)) !== 0n;
610
+ }
611
+ toBigInt() {
612
+ return this.value;
613
+ }
614
+ toNumber() {
615
+ return Number(this.value);
616
+ }
617
+ toJSON() {
618
+ return this.value.toString();
619
+ }
620
+ toString() {
621
+ return this.value.toString();
622
+ }
623
+ };
624
+
625
+ //#endregion
626
+ //#region src/lib/utils/cache/adapters/BaseCacheAdapter.ts
627
+ var BaseCacheAdapter = class {};
628
+
629
+ //#endregion
630
+ //#region src/lib/utils/cache/adapters/LocalCacheAdapter.ts
631
+ var LocalCacheAdapter = class extends BaseCacheAdapter {
632
+ collection = new _discordjs_collection.Collection();
633
+ #sweepTimer;
634
+ get(key) {
635
+ const entry = this.collection.get(key);
636
+ if (!entry) return;
637
+ if (entry.expiresAt && entry.expiresAt < Date.now()) {
638
+ this.collection.delete(key);
639
+ return;
640
+ }
641
+ this.#setupSweepTTL();
642
+ return entry.value;
643
+ }
644
+ set(key, value, ttl) {
645
+ const expiresAt = ttl ? Date.now() + ttl : void 0;
646
+ this.collection.set(key, {
647
+ value,
648
+ expiresAt
649
+ });
650
+ this.#setupSweepTTL();
651
+ }
652
+ delete(key) {
653
+ this.#setupSweepTTL();
654
+ this.collection.delete(key);
655
+ }
656
+ clear() {
657
+ this.collection.clear();
658
+ clearTimeout(this.#sweepTimer);
659
+ this.#sweepTimer = void 0;
660
+ }
661
+ has(key) {
662
+ this.#setupSweepTTL();
663
+ return this.get(key) !== void 0;
664
+ }
665
+ find(filter) {
666
+ for (const entry of this.collection.values()) if (filter(entry.value)) return entry.value;
667
+ }
668
+ sweep(filter) {
669
+ for (const [key, entry] of this.collection) if (filter(entry.value)) this.delete(key);
670
+ }
671
+ sweepTTL() {
672
+ for (const [key, entry] of this.collection) if (entry.expiresAt && entry.expiresAt < Date.now()) this.delete(key);
673
+ }
674
+ #setupSweepTTL() {
675
+ if (!this.collection.size) return;
676
+ if (this.#sweepTimer) return;
677
+ this.#sweepTimer = setTimeout(() => {
678
+ this.#sweepTimer = void 0;
679
+ this.sweepTTL();
680
+ this.#setupSweepTTL();
681
+ }, 1e3);
682
+ }
683
+ };
684
+
685
+ //#endregion
686
+ //#region src/lib/utils/cache/HybridCache.ts
687
+ var HybridCache = class {
688
+ local = new LocalCacheAdapter();
689
+ constructor(remote, defaultTTL) {
690
+ this.remote = remote;
691
+ this.defaultTTL = defaultTTL;
692
+ }
693
+ async get(key) {
694
+ const local = this.local.get(key);
695
+ if (local !== void 0) return local;
696
+ const remote = await this.remote?.get(key);
697
+ if (remote !== void 0) this.local.set(key, remote);
698
+ return remote;
699
+ }
700
+ async sweep(filter) {
701
+ this.local.sweep(filter);
702
+ await this.remote?.sweep(filter);
703
+ }
704
+ async set(key, value, ttl = this.defaultTTL) {
705
+ this.local.set(key, value, ttl);
706
+ await this.remote?.set(key, value, ttl);
707
+ }
708
+ async delete(key) {
709
+ this.local.delete(key);
710
+ await this.remote?.delete(key);
711
+ }
712
+ async clear() {
713
+ this.local.clear();
714
+ await this.remote?.clear();
715
+ }
716
+ async has(key) {
717
+ return await this.get(key) !== void 0;
718
+ }
719
+ };
720
+
721
+ //#endregion
722
+ //#region src/lib/client/ClientCacheManager.ts
723
+ var ClientCacheManager = class {
724
+ channels = new LocalCacheAdapter();
725
+ guilds = new LocalCacheAdapter();
726
+ #users;
727
+ #messages;
728
+ #sweepers = new _discordjs_collection.Collection();
729
+ constructor(options) {
730
+ this.options = options;
731
+ }
732
+ get users() {
733
+ let options;
734
+ if (typeof this.options.users === "object") options = this.options.users;
735
+ else options = { enabled: !!this.options.users };
736
+ if (!options.enabled) throw new Error("User cache is disabled");
737
+ if (!this.#users) this.#users = new HybridCache(options.adapter, options.ttl);
738
+ if (options.sweeper) this.#sweepers.set("users", setInterval(() => this.#users.sweep(options.sweeper.filter), options.sweeper.interval));
739
+ return this.#users;
740
+ }
741
+ get messages() {
742
+ let options;
743
+ if (typeof this.options.messages === "object") options = this.options.messages;
744
+ else options = { enabled: !!this.options.messages };
745
+ if (!options.enabled) throw new Error("Message cache is disabled");
746
+ if (!this.#messages) this.#messages = new HybridCache(options.adapter, options.ttl);
747
+ if (options.sweeper) this.#sweepers.set("messages", setInterval(() => this.#messages.sweep(options.sweeper.filter), options.sweeper.interval));
748
+ return this.#messages;
749
+ }
750
+ isModuleEnabled(module) {
751
+ return typeof this.options[module] === "object" ? this.options[module].enabled : !!this.options[module];
752
+ }
753
+ async resolve(cache, id, factory, patch) {
754
+ let value = await cache.get(id);
755
+ if (!value) {
756
+ value = factory();
757
+ await cache.set(id, value);
758
+ }
759
+ if (patch) patch(value);
760
+ return value;
761
+ }
762
+ resolveLocal(cache, id, factory, patch) {
763
+ let value = cache.get(id);
764
+ if (!value) {
765
+ value = factory();
766
+ cache.set(id, value);
767
+ }
768
+ if (patch) patch(value);
769
+ return value;
770
+ }
771
+ };
772
+
773
+ //#endregion
774
+ //#region src/lib/client/dispatches/registry.ts
775
+ const handlers = {};
776
+ function registerHandler(event, handler) {
777
+ if (handlers[event]) throw new Error(`Handler for ${event} already registered`);
778
+ handlers[event] = handler;
779
+ }
780
+
781
+ //#endregion
782
+ //#region src/lib/client/dispatches/handlers/message.ts
783
+ async function onCreateAndUpdate$1(client, payload) {
784
+ let message;
785
+ if (client.cache.isModuleEnabled("messages")) message = await client.cache.resolve(client.cache.messages, payload.d.id, () => new Message(client, payload.d), (m) => m._patch(payload.d));
786
+ else message = new Message(client, payload.d);
787
+ if (message.partial && !client.options.partials.has(Partial.Message)) return;
788
+ client.emit(payload.t === discord_api_types_v10.GatewayDispatchEvents.MessageUpdate ? "messageUpdate" : "messageCreate", message);
789
+ }
790
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.MessageCreate, onCreateAndUpdate$1);
791
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.MessageUpdate, onCreateAndUpdate$1);
792
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.MessageDelete, async (client, payload) => {
793
+ let message;
794
+ if (client.cache.isModuleEnabled("messages")) message = await client.cache.messages.get(payload.d.id);
795
+ if (!message) message = new Message(client, payload.d);
796
+ client.emit("messageDelete", message);
797
+ });
798
+
799
+ //#endregion
800
+ //#region src/lib/client/dispatches/handlers/guild.ts
801
+ async function onCreateAndUpdate(client, payload) {
802
+ const guild = await client.cache.resolve(client.cache.guilds, payload.d.id, () => new Guild(client, payload.d), (g) => g._patch(payload.d));
803
+ if (payload.t === discord_api_types_v10.GatewayDispatchEvents.GuildCreate) client.emit(payload.d.unavailable ? "guildCreate" : "guildAvailable", guild);
804
+ else client.emit("guildUpdate", guild);
805
+ }
806
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.GuildCreate, onCreateAndUpdate);
807
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.GuildUpdate, onCreateAndUpdate);
808
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.GuildDelete, async (client, payload) => {
809
+ const guild = client.cache.guilds.get(payload.d.id);
810
+ if (!guild) return;
811
+ for (const channel of guild.channels.values()) client.cache.channels.delete(channel.id);
812
+ client.cache.guilds.delete(payload.d.id);
813
+ client.emit("guildDelete", guild);
814
+ });
815
+
816
+ //#endregion
817
+ //#region src/lib/client/dispatches/handlers/channel.ts
818
+ async function onCreateOrUpdate(client, payload) {
819
+ const channel = await client.cache.resolve(client.cache.channels, payload.d.id, () => createChannel(client, payload.d), (c) => c?._patch(payload.d));
820
+ if (!channel) return;
821
+ if (channel.inGuild() && !channel.guild.channels.has(channel.id)) channel.guild.channels.set(channel.id, channel);
822
+ client.emit(payload.t === discord_api_types_v10.GatewayDispatchEvents.ChannelCreate ? "channelCreate" : "channelUpdate", channel);
823
+ }
824
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.ChannelCreate, onCreateOrUpdate);
825
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.ChannelUpdate, onCreateOrUpdate);
826
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.ChannelDelete, (client, payload) => {
827
+ const channel = client.cache.channels.get(payload.d.id);
828
+ const guild = client.cache.guilds.get(payload.d.guild_id);
829
+ if (guild) guild.channels.delete(payload.d.id);
830
+ if (!channel) return;
831
+ client.cache.channels.delete(payload.d.id);
832
+ client.emit("channelDelete", channel);
833
+ });
834
+
835
+ //#endregion
836
+ //#region src/lib/client/dispatches/handlers/user.ts
837
+ registerHandler(discord_api_types_v10.GatewayDispatchEvents.UserUpdate, async (client, payload) => {
838
+ if (payload.d.id === client.user?.id) client.user._patch(payload.d);
839
+ let user;
840
+ if (client.cache.isModuleEnabled("users")) user = await client.cache.resolve(client.cache.users, payload.d.id, () => new User(client, payload.d), (user) => user._patch(payload.d));
841
+ else user = new User(client, payload.d);
842
+ client.emit("userUpdate", user);
843
+ });
844
+
845
+ //#endregion
846
+ //#region src/lib/client/Client.ts
847
+ var Client = class extends node_events.default {
848
+ options;
849
+ shards;
850
+ rest;
851
+ helper;
852
+ cache;
853
+ #user;
854
+ #ready = false;
855
+ constructor(options, rest) {
856
+ super();
857
+ const intents = options.intents instanceof IntentsBitField ? options.intents : new IntentsBitField(options.intents);
858
+ const sharding = {
859
+ shardsPerCluster: options.sharding?.shardsPerCluster ?? 1,
860
+ totalShards: options.sharding?.totalShards ?? 1
861
+ };
862
+ this.options = {
863
+ token: options.token,
864
+ intents,
865
+ sharding,
866
+ partials: new Set(options.partials ?? []),
867
+ cache: options.cache ?? {}
868
+ };
869
+ this.rest = rest ?? new _bakit_rest.REST({ token: options.token });
870
+ this.shards = new _bakit_gateway.ShardingManager({
871
+ ...this.options.sharding,
872
+ token: this.options.token,
873
+ intents: this.options.intents.toBigInt()
874
+ }, rest);
875
+ this.cache = new ClientCacheManager(this.options.cache);
876
+ this.helper = new ClientHelper(this);
877
+ this.#setupShardsListeners();
878
+ }
879
+ get user() {
880
+ return this.#user;
881
+ }
882
+ async login() {
883
+ if (this.#user || this.shards.ready || this.shards.clusters.size > 0) throw new Error("Cannot login twice");
884
+ await this.shards.spawn();
885
+ return new Promise((resolve, reject) => {
886
+ const cleanup = () => {
887
+ this.removeListener("ready", onReady);
888
+ this.removeListener("error", onError);
889
+ };
890
+ const onReady = () => {
891
+ cleanup();
892
+ resolve();
893
+ };
894
+ const onError = (error) => {
895
+ cleanup();
896
+ reject(error);
897
+ };
898
+ this.once("ready", onReady);
899
+ this.once("error", onError);
900
+ });
901
+ }
902
+ #setupShardsListeners() {
903
+ this.shards.on("dispatch", (_, shardId, payload) => this.#handleDispatch(shardId, payload));
904
+ this.shards.on("raw", (_, shardId, payload) => this.emit("raw", shardId, payload));
905
+ this.shards.once("ready", () => this.#tryEmitReady());
906
+ }
907
+ async #handleDispatch(shardId, payload) {
908
+ this.emit("dispatch", shardId, payload);
909
+ switch (payload.t) {
910
+ case discord_api_types_v10.GatewayDispatchEvents.Ready:
911
+ this.#user ??= new User(this, payload.d.user);
912
+ this.#tryEmitReady();
913
+ break;
914
+ default: {
915
+ const handler = handlers[payload.t];
916
+ if (handler) await handler(this, payload);
917
+ break;
918
+ }
919
+ }
920
+ }
921
+ /**
922
+ * We use this method to emit ready event only once since we have 2 ways to emit it.
923
+ * @private
924
+ */
925
+ #tryEmitReady() {
926
+ if (this.#ready || !this.#user || !this.shards.ready) return;
927
+ this.#ready = true;
928
+ this.emit("ready", this.#user);
929
+ }
930
+ };
931
+ function createClient(options) {
932
+ return new Client(options);
933
+ }
934
+
935
+ //#endregion
936
+ exports.BaseCacheAdapter = BaseCacheAdapter;
937
+ exports.BaseChannel = BaseChannel;
938
+ exports.Client = Client;
939
+ exports.ClientHelper = ClientHelper;
940
+ Object.defineProperty(exports, 'Cluster', {
941
+ enumerable: true,
942
+ get: function () {
943
+ return _bakit_gateway.Cluster;
944
+ }
945
+ });
946
+ Object.defineProperty(exports, 'DEFAULT_REST_OPTIONS', {
947
+ enumerable: true,
948
+ get: function () {
949
+ return _bakit_rest.DEFAULT_REST_OPTIONS;
950
+ }
951
+ });
952
+ Object.defineProperty(exports, 'DiscordAPIV10', {
953
+ enumerable: true,
954
+ get: function () {
955
+ return discord_api_types_v10;
956
+ }
957
+ });
958
+ Object.defineProperty(exports, 'DiscordHTTPError', {
959
+ enumerable: true,
960
+ get: function () {
961
+ return _bakit_rest.DiscordHTTPError;
962
+ }
963
+ });
964
+ exports.Guild = Guild;
965
+ exports.GuildTextChannel = GuildTextChannel;
966
+ exports.GuildVoiceChannel = GuildVoiceChannel;
967
+ exports.HybridCache = HybridCache;
968
+ Object.defineProperty(exports, 'Intent', {
969
+ enumerable: true,
970
+ get: function () {
971
+ return discord_api_types_v10.GatewayIntentBits;
972
+ }
973
+ });
974
+ exports.IntentsBitField = IntentsBitField;
975
+ exports.LocalCacheAdapter = LocalCacheAdapter;
976
+ exports.Message = Message;
977
+ exports.Partial = Partial;
978
+ Object.defineProperty(exports, 'REST', {
979
+ enumerable: true,
980
+ get: function () {
981
+ return _bakit_rest.REST;
982
+ }
983
+ });
984
+ Object.defineProperty(exports, 'RESTAdapter', {
985
+ enumerable: true,
986
+ get: function () {
987
+ return _bakit_rest.RESTAdapter;
988
+ }
989
+ });
990
+ Object.defineProperty(exports, 'RESTMethod', {
991
+ enumerable: true,
992
+ get: function () {
993
+ return _bakit_rest.RESTMethod;
994
+ }
995
+ });
996
+ Object.defineProperty(exports, 'Service', {
997
+ enumerable: true,
998
+ get: function () {
999
+ return _bakit_service.Service;
1000
+ }
1001
+ });
1002
+ Object.defineProperty(exports, 'ServiceDriver', {
1003
+ enumerable: true,
1004
+ get: function () {
1005
+ return _bakit_service.ServiceDriver;
1006
+ }
1007
+ });
1008
+ Object.defineProperty(exports, 'ServiceProcess', {
1009
+ enumerable: true,
1010
+ get: function () {
1011
+ return _bakit_service.ServiceProcess;
1012
+ }
1013
+ });
1014
+ Object.defineProperty(exports, 'ServiceState', {
1015
+ enumerable: true,
1016
+ get: function () {
1017
+ return _bakit_service.ServiceState;
1018
+ }
1019
+ });
1020
+ Object.defineProperty(exports, 'Shard', {
1021
+ enumerable: true,
1022
+ get: function () {
1023
+ return _bakit_gateway.Shard;
1024
+ }
1025
+ });
1026
+ Object.defineProperty(exports, 'ShardState', {
1027
+ enumerable: true,
1028
+ get: function () {
1029
+ return _bakit_gateway.ShardState;
1030
+ }
1031
+ });
1032
+ Object.defineProperty(exports, 'ShardStrategy', {
1033
+ enumerable: true,
1034
+ get: function () {
1035
+ return _bakit_gateway.ShardStrategy;
1036
+ }
1037
+ });
1038
+ Object.defineProperty(exports, 'ShardingManager', {
1039
+ enumerable: true,
1040
+ get: function () {
1041
+ return _bakit_gateway.ShardingManager;
1042
+ }
1043
+ });
1044
+ Object.defineProperty(exports, 'TransportClient', {
1045
+ enumerable: true,
1046
+ get: function () {
1047
+ return _bakit_service.TransportClient;
1048
+ }
1049
+ });
1050
+ Object.defineProperty(exports, 'TransportServer', {
1051
+ enumerable: true,
1052
+ get: function () {
1053
+ return _bakit_service.TransportServer;
1054
+ }
1055
+ });
1056
+ exports.User = User;
1057
+ Object.defineProperty(exports, 'capitalize', {
1058
+ enumerable: true,
1059
+ get: function () {
1060
+ return _bakit_utils.capitalize;
1061
+ }
1062
+ });
1063
+ exports.createClient = createClient;
1064
+ Object.defineProperty(exports, 'createREST', {
1065
+ enumerable: true,
1066
+ get: function () {
1067
+ return _bakit_rest.createREST;
1068
+ }
1069
+ });
1070
+ Object.defineProperty(exports, 'createRESTAdapter', {
1071
+ enumerable: true,
1072
+ get: function () {
1073
+ return _bakit_rest.createRESTAdapter;
1074
+ }
1075
+ });
1076
+ Object.defineProperty(exports, 'createService', {
1077
+ enumerable: true,
1078
+ get: function () {
1079
+ return _bakit_service.createService;
1080
+ }
1081
+ });
1082
+ Object.defineProperty(exports, 'createTransportClient', {
1083
+ enumerable: true,
1084
+ get: function () {
1085
+ return _bakit_service.createTransportClient;
1086
+ }
1087
+ });
1088
+ Object.defineProperty(exports, 'createTransportServer', {
1089
+ enumerable: true,
1090
+ get: function () {
1091
+ return _bakit_service.createTransportServer;
1092
+ }
1093
+ });
1094
+ Object.defineProperty(exports, 'instanceToObject', {
1095
+ enumerable: true,
1096
+ get: function () {
1097
+ return _bakit_utils.instanceToObject;
1098
+ }
1099
+ });
1100
+ Object.defineProperty(exports, 'isCommonJS', {
1101
+ enumerable: true,
1102
+ get: function () {
1103
+ return _bakit_utils.isCommonJS;
1104
+ }
1105
+ });
1106
+ Object.defineProperty(exports, 'isESM', {
1107
+ enumerable: true,
1108
+ get: function () {
1109
+ return _bakit_utils.isESM;
1110
+ }
1111
+ });
1112
+ Object.defineProperty(exports, 'isPlainObject', {
1113
+ enumerable: true,
1114
+ get: function () {
1115
+ return _bakit_utils.isPlainObject;
1116
+ }
1117
+ });
1118
+ Object.defineProperty(exports, 'isPromiseLike', {
1119
+ enumerable: true,
1120
+ get: function () {
1121
+ return _bakit_utils.isPromiseLike;
1122
+ }
1123
+ });
1124
+ Object.defineProperty(exports, 'promisify', {
1125
+ enumerable: true,
1126
+ get: function () {
1127
+ return _bakit_utils.promisify;
1128
+ }
1129
+ });
1130
+ Object.defineProperty(exports, 'sleep', {
1131
+ enumerable: true,
1132
+ get: function () {
1133
+ return _bakit_utils.sleep;
1134
+ }
1135
+ });