@mostfeatured/dbi 0.2.16 → 0.2.18

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.
Files changed (81) hide show
  1. package/dist/src/types/Components/HTMLComponentsV2/index.d.ts +4 -0
  2. package/dist/src/types/Components/HTMLComponentsV2/index.d.ts.map +1 -1
  3. package/dist/src/types/Components/HTMLComponentsV2/index.js +40 -5
  4. package/dist/src/types/Components/HTMLComponentsV2/index.js.map +1 -1
  5. package/dist/src/types/Event.d.ts +21 -13
  6. package/dist/src/types/Event.d.ts.map +1 -1
  7. package/dist/src/types/Event.js.map +1 -1
  8. package/dist/test/index.js +1 -1
  9. package/dist/test/index.js.map +1 -1
  10. package/generated/namespaceData.d.ts +3 -1
  11. package/package.json +6 -2
  12. package/.gitattributes +0 -2
  13. package/.hintrc +0 -8
  14. package/.vscode/settings.json +0 -3
  15. package/docs/ADVANCED_FEATURES.md +0 -840
  16. package/docs/API_REFERENCE.md +0 -929
  17. package/docs/CHAT_INPUT.md +0 -811
  18. package/docs/COMPONENTS.md +0 -1039
  19. package/docs/EVENTS.md +0 -568
  20. package/docs/GETTING_STARTED.md +0 -398
  21. package/docs/LOCALIZATION.md +0 -777
  22. package/docs/README.md +0 -345
  23. package/docs/SVELTE_COMPONENTS.md +0 -1111
  24. package/docs/llm/ADVANCED_FEATURES.txt +0 -521
  25. package/docs/llm/API_REFERENCE.txt +0 -659
  26. package/docs/llm/CHAT_INPUT.txt +0 -514
  27. package/docs/llm/COMPONENTS.txt +0 -595
  28. package/docs/llm/EVENTS.txt +0 -449
  29. package/docs/llm/GETTING_STARTED.txt +0 -296
  30. package/docs/llm/LOCALIZATION.txt +0 -501
  31. package/docs/llm/README.txt +0 -193
  32. package/docs/llm/SVELTE_COMPONENTS.txt +0 -566
  33. package/src/DBI.ts +0 -1007
  34. package/src/Events.ts +0 -189
  35. package/src/data/eventMap.json +0 -248
  36. package/src/index.ts +0 -23
  37. package/src/methods/handleMessageCommands.ts +0 -482
  38. package/src/methods/hookEventListeners.ts +0 -119
  39. package/src/methods/hookInteractionListeners.ts +0 -314
  40. package/src/methods/publishInteractions.ts +0 -256
  41. package/src/types/ApplicationRoleConnectionMetadata.ts +0 -19
  42. package/src/types/Builders/ButtonBuilder.ts +0 -53
  43. package/src/types/Builders/ChannelSelectMenuBuilder.ts +0 -53
  44. package/src/types/Builders/MentionableSelectMenuBuilder.ts +0 -53
  45. package/src/types/Builders/ModalBuilder.ts +0 -53
  46. package/src/types/Builders/RoleSelectMenuBuilder.ts +0 -53
  47. package/src/types/Builders/StringSelectMenuBuilder.ts +0 -53
  48. package/src/types/Builders/UserSelectMenuBuilder.ts +0 -53
  49. package/src/types/ChatInput/ChatInput.ts +0 -28
  50. package/src/types/ChatInput/ChatInputOptions.ts +0 -388
  51. package/src/types/Components/Button.ts +0 -39
  52. package/src/types/Components/ChannelSelectMenu.ts +0 -43
  53. package/src/types/Components/HTMLComponentsV2/HTMLComponentsV2Handlers.ts +0 -78
  54. package/src/types/Components/HTMLComponentsV2/index.ts +0 -761
  55. package/src/types/Components/HTMLComponentsV2/parser.ts +0 -649
  56. package/src/types/Components/HTMLComponentsV2/svelteParser.ts +0 -1503
  57. package/src/types/Components/HTMLComponentsV2/svelteRenderer.ts +0 -416
  58. package/src/types/Components/MentionableSelectMenu.ts +0 -43
  59. package/src/types/Components/Modal.ts +0 -46
  60. package/src/types/Components/RoleSelectMenu.ts +0 -43
  61. package/src/types/Components/StringSelectMenu.ts +0 -43
  62. package/src/types/Components/UserSelectMenu.ts +0 -43
  63. package/src/types/Event.ts +0 -145
  64. package/src/types/Interaction.ts +0 -100
  65. package/src/types/other/CustomEvent.ts +0 -19
  66. package/src/types/other/FakeMessageInteraction.ts +0 -408
  67. package/src/types/other/InteractionLocale.ts +0 -34
  68. package/src/types/other/Locale.ts +0 -70
  69. package/src/types/other/MessageContextMenu.ts +0 -27
  70. package/src/types/other/UserContextMenu.ts +0 -25
  71. package/src/utils/MemoryStore.ts +0 -28
  72. package/src/utils/UtilTypes.ts +0 -11
  73. package/src/utils/customId.ts +0 -49
  74. package/src/utils/permissions.ts +0 -5
  75. package/src/utils/recursiveImport.ts +0 -35
  76. package/src/utils/recursiveUnload.ts +0 -25
  77. package/src/utils/unloadModule.ts +0 -7
  78. package/test/index.ts +0 -176
  79. package/test/product-showcase.svelte +0 -558
  80. package/test/test.ts +0 -3
  81. package/tsconfig.json +0 -51
package/src/DBI.ts DELETED
@@ -1,1007 +0,0 @@
1
- import Discord, { MessagePayload } from "discord.js";
2
- import {
3
- DBIChatInput,
4
- TDBIChatInputOmitted,
5
- } from "./types/ChatInput/ChatInput";
6
- import { DBIChatInputOptions } from "./types/ChatInput/ChatInputOptions";
7
- import { publishInteractions } from "./methods/publishInteractions";
8
- import { ClientEvents, DBIEvent, TDBIEventOmitted } from "./types/Event";
9
- import { MemoryStore } from "./utils/MemoryStore";
10
- import { hookInteractionListeners } from "./methods/hookInteractionListeners";
11
- import { Events } from "./Events";
12
- import {
13
- DBILocale,
14
- TDBILocaleConstructor,
15
- TDBILocaleString,
16
- } from "./types/other/Locale";
17
- import { DBIButton, TDBIButtonOmitted } from "./types/Components/Button";
18
- import {
19
- DBIStringSelectMenu,
20
- TDBIStringSelectMenuOmitted,
21
- } from "./types/Components/StringSelectMenu";
22
- import {
23
- DBIMessageContextMenu,
24
- TDBIMessageContextMenuOmitted,
25
- } from "./types/other/MessageContextMenu";
26
- import {
27
- DBIUserContextMenu,
28
- TDBIUserContextMenuOmitted,
29
- } from "./types/other/UserContextMenu";
30
- import { hookEventListeners } from "./methods/hookEventListeners";
31
- import eventMap from "./data/eventMap.json";
32
- import { DBIModal, TDBIModalOmitted } from "./types/Components/Modal";
33
- import * as Sharding from "discord-hybrid-sharding";
34
- import _ from "lodash";
35
- import {
36
- DBIInteractionLocale,
37
- TDBIInteractionLocaleOmitted,
38
- } from "./types/other/InteractionLocale";
39
- import { TDBIInteractions } from "./types/Interaction";
40
- import { NamespaceData, NamespaceEnums } from "../generated/namespaceData";
41
- import {
42
- DBICustomEvent,
43
- TDBICustomEventOmitted,
44
- } from "./types/other/CustomEvent";
45
- import aaq from "async-and-quick";
46
- import {
47
- DBIUserSelectMenu,
48
- TDBIUserSelectMenuOmitted,
49
- } from "./types/Components/UserSelectMenu";
50
- import {
51
- DBIMentionableSelectMenu,
52
- TDBIMentionableSelectMenuOmitted,
53
- } from "./types/Components/MentionableSelectMenu";
54
- import {
55
- DBIChannelSelectMenu,
56
- TDBIChannelSelectMenuOmitted,
57
- } from "./types/Components/ChannelSelectMenu";
58
- import {
59
- DBIRoleSelectMenu,
60
- TDBIRoleSelectMenuOmitted,
61
- } from "./types/Components/RoleSelectMenu";
62
- import { handleMessageCommands } from "./methods/handleMessageCommands";
63
- import { FakeMessageInteraction } from "./types/other/FakeMessageInteraction";
64
- import { DBIHTMLComponentsV2, TDBIHTMLComponentsV2Omitted } from "./types/Components/HTMLComponentsV2";
65
- import { DBIHTMLComponentsV2Handlers } from "./types/Components/HTMLComponentsV2/HTMLComponentsV2Handlers";
66
-
67
- export interface DBIStore {
68
- get(key: string, defaultValue?: any): Promise<any>;
69
- set(key: string, value: any): Promise<void>;
70
- delete(key: string): Promise<void>;
71
- has(key: string): Promise<boolean>;
72
- }
73
-
74
- export type TDBIClientData<TNamespace extends NamespaceEnums> = {
75
- namespace: NamespaceData[TNamespace]["clientNamespaces"];
76
- token: string;
77
- options: Discord.ClientOptions;
78
- client: Discord.Client<true>;
79
- };
80
-
81
- export type TDBIMessageCommandsActionCtx<TNamespace extends NamespaceEnums> = {
82
- interaction: FakeMessageInteraction;
83
- dbiInteraction: DBIChatInput<TNamespace>;
84
- locale: { guild?: DBILocale<TNamespace>; user: DBILocale<TNamespace> };
85
- }
86
-
87
- export type TDBIMessageCommandsPrefixesCtx = {
88
- message: Discord.Message;
89
- };
90
-
91
- export type TDBILocaleInvalidPathCtx<TNamespace extends NamespaceEnums> = {
92
- path: string;
93
- locale: DBILocale<TNamespace>;
94
- }
95
-
96
- export interface DBIConfig<TNamespace extends NamespaceEnums> {
97
- discord: {
98
- namespace: string;
99
- token: string;
100
- options: Discord.ClientOptions;
101
- }[];
102
- defaults: {
103
- locale: {
104
- name: TDBILocaleString;
105
- invalidPath(ctx: TDBILocaleInvalidPathCtx<TNamespace>): string;
106
- };
107
- directMessages: boolean;
108
- defaultMemberPermissions: Discord.PermissionsString[];
109
- messageCommands: {
110
- deferReplyContent(ctx: TDBIMessageCommandsActionCtx<TNamespace>): MessagePayload | string | Promise<MessagePayload | string>;
111
- };
112
- };
113
-
114
- sharding: "hybrid" | "default" | "off";
115
- /**
116
- * Persist store. (Default to MemoryStore thats not persis tho.)
117
- */
118
- store: DBIStore;
119
-
120
- references: {
121
- autoClear?: {
122
- check: number;
123
- ttl: number;
124
- };
125
- };
126
-
127
- inlineListeners: {
128
- autoClear?: {
129
- check: number;
130
- ttl: number;
131
- };
132
- }
133
-
134
- strict: boolean;
135
- messageCommands?: {
136
- prefixes(ctx: TDBIMessageCommandsPrefixesCtx): string[] | Promise<string[]>;
137
- typeAliases: {
138
- booleans: Record<string, boolean>;
139
- };
140
- };
141
- }
142
-
143
- export interface DBIConfigConstructor<TNamespace extends NamespaceEnums, OtherData extends Record<string, any>> {
144
- discord:
145
- | {
146
- token: string;
147
- options: Discord.ClientOptions;
148
- }
149
- | {
150
- namespace: string;
151
- token: string;
152
- options: Discord.ClientOptions;
153
- }[];
154
-
155
- defaults?: {
156
- locale?: {
157
- name?: TDBILocaleString;
158
- invalidPath?: string | ((ctx: TDBILocaleInvalidPathCtx<TNamespace>) => string);
159
- };
160
- directMessages?: boolean;
161
- defaultMemberPermissions?: Discord.PermissionsString[];
162
- messageCommands?: {
163
- deferReplyContent?: MessagePayload | string | ((ctx: TDBIMessageCommandsActionCtx<TNamespace>) => MessagePayload | string | Promise<MessagePayload | string>);
164
- };
165
- };
166
-
167
- sharding?: "hybrid" | "default" | "off";
168
- /**
169
- * Persist store. (Default to MemoryStore thats not persis tho.)
170
- */
171
- store?: DBIStore;
172
-
173
- references?: {
174
- autoClear?: {
175
- check: number;
176
- ttl: number;
177
- };
178
- };
179
-
180
- inlineListeners?: {
181
- autoClear?: {
182
- check: number;
183
- ttl: number;
184
- };
185
- };
186
-
187
- data?: {
188
- other?: OtherData;
189
- refs?: Map<string, { at: number; value: any; ttl?: number }>;
190
- };
191
-
192
- strict?: boolean;
193
-
194
- messageCommands?: {
195
- prefixes: string[] | ((ctx: TDBIMessageCommandsPrefixesCtx) => string[] | Promise<string[]>);
196
- typeAliases?: {
197
- /**
198
- * Example: {"yes": true, "no": false}
199
- */
200
- booleans?: Record<string, boolean>;
201
- };
202
- };
203
- }
204
-
205
- export interface DBIRegisterAPI<TNamespace extends NamespaceEnums> {
206
- ChatInput(cfg: TDBIChatInputOmitted<TNamespace>): DBIChatInput<TNamespace>;
207
- ChatInputOptions: DBIChatInputOptions<TNamespace>;
208
-
209
- Event(cfg: TDBIEventOmitted<TNamespace>): DBIEvent<TNamespace>;
210
- Locale(cfg: TDBILocaleConstructor<TNamespace>): DBILocale<TNamespace>;
211
- Button(cfg: TDBIButtonOmitted<TNamespace>): DBIButton<TNamespace>;
212
- StringSelectMenu(
213
- cfg: TDBIStringSelectMenuOmitted<TNamespace>
214
- ): DBIStringSelectMenu<TNamespace>;
215
- UserSelectMenu(
216
- cfg: TDBIUserSelectMenuOmitted<TNamespace>
217
- ): DBIUserSelectMenu<TNamespace>;
218
- RoleSelectMenu(
219
- cfg: TDBIRoleSelectMenuOmitted<TNamespace>
220
- ): DBIRoleSelectMenu<TNamespace>;
221
- ChannelSelectMenu(
222
- cfg: TDBIChannelSelectMenuOmitted<TNamespace>
223
- ): DBIChannelSelectMenu<TNamespace>;
224
- MentionableSelectMenu(
225
- cfg: TDBIMentionableSelectMenuOmitted<TNamespace>
226
- ): DBIMentionableSelectMenu<TNamespace>;
227
- MessageContextMenu(
228
- cfg: TDBIMessageContextMenuOmitted<TNamespace>
229
- ): DBIMessageContextMenu<TNamespace>;
230
- UserContextMenu(
231
- cfg: TDBIUserContextMenuOmitted<TNamespace>
232
- ): DBIUserContextMenu<TNamespace>;
233
- InteractionLocale(cfg: TDBIInteractionLocaleOmitted): DBIInteractionLocale;
234
- Modal(cfg: TDBIModalOmitted<TNamespace>): DBIModal<TNamespace>;
235
- CustomEvent<T extends keyof NamespaceData[TNamespace]["customEvents"]>(
236
- cfg: TDBICustomEventOmitted<TNamespace, T>
237
- ): DBICustomEvent<TNamespace, T>;
238
-
239
- HTMLComponentsV2(cfg: TDBIHTMLComponentsV2Omitted<TNamespace>): DBIHTMLComponentsV2<TNamespace>;
240
- HTMLComponentsV2Handlers: DBIHTMLComponentsV2Handlers<TNamespace>;
241
-
242
- createInlineEvent(cfg: Omit<TDBIEventOmitted<TNamespace>, "id">): DBIEvent<TNamespace>;
243
- createInlineButton(cfg: Omit<TDBIButtonOmitted<TNamespace>, "id" | "name">): DBIButton<TNamespace>;
244
- createInlineStringSelectMenu(
245
- cfg: Omit<TDBIStringSelectMenuOmitted<TNamespace>, "id" | "name">
246
- ): DBIStringSelectMenu<TNamespace>;
247
- createInlineUserSelectMenu(
248
- cfg: Omit<TDBIUserSelectMenuOmitted<TNamespace>, "id" | "name">
249
- ): DBIUserSelectMenu<TNamespace>;
250
- createInlineRoleSelectMenu(
251
- cfg: Omit<TDBIRoleSelectMenuOmitted<TNamespace>, "id" | "name">
252
- ): DBIRoleSelectMenu<TNamespace>;
253
- createInlineChannelSelectMenu(
254
- cfg: Omit<TDBIChannelSelectMenuOmitted<TNamespace>, "id" | "name">
255
- ): DBIChannelSelectMenu<TNamespace>;
256
- createInlineMentionableSelectMenu(
257
- cfg: Omit<TDBIMentionableSelectMenuOmitted<TNamespace>, "id" | "name">
258
- ): DBIMentionableSelectMenu<TNamespace>;
259
- createInlineModal(cfg: Omit<TDBIModalOmitted<TNamespace>, "id" | "name">): DBIModal<TNamespace>;
260
-
261
- onUnload(cb: () => Promise<any> | any): any;
262
- }
263
-
264
- export class DBI<
265
- TNamespace extends NamespaceEnums,
266
- TOtherData extends Record<string, any> = Record<string, any>
267
- > {
268
- namespace: TNamespace;
269
- config: DBIConfig<TNamespace>;
270
- data: {
271
- interactions: Discord.Collection<string, TDBIInteractions<TNamespace>>;
272
- events: Discord.Collection<string, DBIEvent<TNamespace>>;
273
- locales: Discord.Collection<string, DBILocale<TNamespace>>;
274
- interactionLocales: Discord.Collection<string, DBIInteractionLocale>;
275
- other: TOtherData;
276
- eventMap: Record<string, string[] | { [k: string]: string }>;
277
- customEventNames: Set<string>;
278
- unloaders: Set<() => void>;
279
- registers: Set<(...args: any[]) => any>;
280
- registerUnloaders: Set<(...args: any[]) => any>;
281
- refs: Map<string, { at: number; value: any; ttl?: number }>;
282
- clients: TDBIClientData<TNamespace>[] & {
283
- next(key?: string): TDBIClientData<TNamespace>;
284
- random(): TDBIClientData<TNamespace>;
285
- random(size: number): TDBIClientData<TNamespace>[];
286
- first(): TDBIClientData<TNamespace>;
287
- get(
288
- namespace: NamespaceData[TNamespace]["clientNamespaces"]
289
- ): TDBIClientData<TNamespace>;
290
- indexes: Record<string, number>;
291
- };
292
- };
293
- events: Events<TNamespace>;
294
- cluster?: Sharding.ClusterClient<Discord.Client>;
295
- private _loaded: boolean;
296
- private _hooked: boolean;
297
- constructor(namespace: TNamespace, config: DBIConfigConstructor<TNamespace, TOtherData>) {
298
- this.namespace = namespace as any;
299
- const self = this;
300
-
301
- config.store = (config.store as any) || new MemoryStore();
302
- config.defaults = {
303
- locale: (() => {
304
- const invalidPath = config.defaults?.locale?.invalidPath;
305
- return {
306
- ...(config.defaults?.locale || {}),
307
- invalidPath: (typeof invalidPath === "function" ? invalidPath : (ctx: TDBILocaleInvalidPathCtx<TNamespace>) => (invalidPath || `Invalid locale path "${ctx.path}".`)) as any
308
- }
309
- })(),
310
- defaultMemberPermissions: [],
311
- directMessages: false,
312
- ...(config.defaults || {}),
313
- messageCommands: (() => {
314
- const deferReplyContent = config.defaults?.messageCommands?.deferReplyContent
315
- return {
316
- ...(config.defaults?.messageCommands || {}),
317
- deferReplyContent: (typeof deferReplyContent === "function" ? deferReplyContent : () => (deferReplyContent || "Loading...")) as any
318
- }
319
- })(),
320
- };
321
-
322
- config.sharding = config.sharding ?? "off";
323
- config.strict = config.strict ?? true;
324
- config.references = {
325
- autoClear: undefined,
326
- ...(config.references || {}),
327
- };
328
- config.inlineListeners = {
329
- autoClear: {
330
- check: 60000,
331
- ttl: 900000,
332
- },
333
- ...(config.inlineListeners || {}),
334
- };
335
-
336
- if (config.messageCommands) {
337
- const { prefixes, typeAliases } = config.messageCommands;
338
-
339
- if (Array.isArray(prefixes) && config.strict && !prefixes.length)
340
- throw new Error("No prefixes provided.");
341
-
342
- const prefixesFn = typeof prefixes === "function" ? prefixes : () => prefixes;
343
-
344
- config.messageCommands.prefixes = prefixesFn;
345
- config.messageCommands.typeAliases = {
346
- booleans: typeAliases.booleans ?? {
347
- true: true,
348
- false: false,
349
- yes: true,
350
- no: false,
351
- y: true,
352
- n: false,
353
- "1": true,
354
- "0": false,
355
- },
356
- };
357
- }
358
-
359
- // @ts-ignore
360
- this.config = config;
361
-
362
- this.data = {
363
- interactions: new Discord.Collection(),
364
- events: new Discord.Collection(),
365
- locales: new Discord.Collection(),
366
- interactionLocales: new Discord.Collection(),
367
- other: (config.data?.other as any) ?? ({} as TOtherData),
368
- eventMap: { ...eventMap },
369
- customEventNames: new Set(),
370
- unloaders: new Set(),
371
- registers: new Set(),
372
- registerUnloaders: new Set(),
373
- refs: config.data?.refs ?? new Map(),
374
- clients: Object.assign([], {
375
- next(key = "global") {
376
- this.indexes[key] = ((this.indexes[key] ?? -1) + 1) % this.length;
377
- return this[this.indexes[key]];
378
- },
379
- random(size?: number) {
380
- if (typeof size === "number") {
381
- return this.sort(() => Math.random() - 0.5).slice(0, size);
382
- } else {
383
- return this[Math.floor(Math.random() * this.length)];
384
- }
385
- },
386
- first() {
387
- return this[0];
388
- },
389
- get(namespace: string) {
390
- return this.find((i: any) => i.namespace === namespace);
391
- },
392
- indexes: {},
393
- }) as any,
394
- };
395
-
396
- this.events = new Events(this as any);
397
-
398
- config.discord = Array.isArray(config.discord)
399
- ? config.discord
400
- : [
401
- {
402
- token: config.discord.token,
403
- options: config.discord.options,
404
- namespace: "default",
405
- },
406
- ];
407
-
408
- this.data.clients.push(...(config.discord as any));
409
- for (let clientContext of this.data.clients) {
410
- let client = new Discord.Client({
411
- ...((clientContext.options || {}) as any),
412
- ...(config.sharding == "hybrid"
413
- ? {
414
- shards: Sharding.getInfo().SHARD_LIST,
415
- shardCount: Sharding.getInfo().TOTAL_SHARDS,
416
- }
417
- : {}),
418
- });
419
- clientContext.client = client as Discord.Client<true>;
420
- }
421
-
422
- if (this.data.clients.length === 0) throw new Error("No clients provided.");
423
- if (
424
- this.data.clients.length !== 1 &&
425
- !(config.sharding && config.sharding === "off")
426
- )
427
- throw new Error("Sharding only supports 1 client.");
428
-
429
- this.cluster =
430
- config.sharding == "hybrid"
431
- ? new Sharding.ClusterClient(this.data.clients[0].client)
432
- : undefined;
433
- this._loaded = false;
434
- this._hooked = false;
435
- }
436
-
437
- private async _hookListeners() {
438
- if (this._hooked) return;
439
- const self = this;
440
- this._hooked = true;
441
- this.data.unloaders.add(hookInteractionListeners(this as any));
442
- this.data.unloaders.add(hookEventListeners(this as any));
443
- if (typeof this.config.references.autoClear !== "undefined") {
444
- this.data.unloaders.add(
445
- (() => {
446
- let interval = setInterval(() => {
447
- this.data.refs.forEach(({ at, ttl }, key) => {
448
- if (
449
- Date.now() >
450
- at + (ttl || this.config.references.autoClear.ttl)
451
- ) {
452
- this.data.refs.delete(key);
453
- }
454
- });
455
- }, this.config.references.autoClear.check);
456
- return () => {
457
- clearInterval(interval);
458
- };
459
- })()
460
- );
461
- }
462
- if (typeof this.config.inlineListeners.autoClear !== "undefined") {
463
- this.data.unloaders.add(
464
- (() => {
465
- let interval = setInterval(() => {
466
- this.data.interactions.forEach((i, key) => {
467
- if (
468
- i.ttl &&
469
- (Date.now() >
470
- i.at + (i.ttl || this.config.inlineListeners.autoClear.ttl))
471
- ) {
472
- this.data.interactions.delete(key);
473
- }
474
- });
475
- this.data.events.forEach((i, key) => {
476
- if (
477
- i.ttl &&
478
- (Date.now() >
479
- i.at + (i.ttl || this.config.inlineListeners.autoClear.ttl))
480
- ) {
481
- this.data.events.delete(key);
482
- }
483
- });
484
- }, this.config.inlineListeners.autoClear.check);
485
- return () => {
486
- clearInterval(interval);
487
- };
488
- })()
489
- );
490
- }
491
- if (typeof this.config.messageCommands !== "undefined") {
492
- this.data.unloaders.add(
493
- (() => {
494
- const { client } = this.client();
495
-
496
- function onMessage(message: Discord.Message) {
497
- handleMessageCommands(self as any, message);
498
- }
499
-
500
- client.on("messageCreate", onMessage);
501
- return () => {
502
- client.off("messageCreate", onMessage);
503
- };
504
- })()
505
- );
506
- }
507
- }
508
-
509
- private async _unhookListeners() {
510
- if (!this._hooked) return;
511
- this._hooked = false;
512
- this.data.unloaders.forEach((f) => {
513
- f();
514
- });
515
- this.data.unloaders.clear();
516
- }
517
-
518
- private async _unregisterAll() {
519
- for await (const cb of this.data.registerUnloaders) {
520
- await cb();
521
- }
522
- this.data.events.clear();
523
- this.data.interactions.clear();
524
- this.data.interactionLocales.clear();
525
- this.data.locales.clear();
526
- this.data.registerUnloaders.clear();
527
- this.data.refs.clear();
528
-
529
- this.data.customEventNames.forEach((value) => {
530
- delete this.data.eventMap[value];
531
- });
532
- this.data.customEventNames.clear();
533
- }
534
-
535
- private async _registerAll(flags: string[] = []) {
536
- const self = this;
537
- const ChatInputOptions = new DBIChatInputOptions(self);
538
- const HTMLComponentsV2Handlers = new DBIHTMLComponentsV2Handlers(self);
539
-
540
- const randomInlineId = () => `inline:${Math.random().toString(36).slice(2)}`;
541
-
542
- for await (const cb of this.data.registers) {
543
- let ChatInput = function (cfg: DBIChatInput<TNamespace>) {
544
- let dbiChatInput = new DBIChatInput(self, cfg);
545
- if (self.data.interactions.has(dbiChatInput.name))
546
- throw new Error(
547
- `DBIChatInput "${dbiChatInput.name}" already loaded as "${self.data.interactions.get(dbiChatInput.name)?.type
548
- }"!`
549
- );
550
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(dbiChatInput.name, dbiChatInput);
551
- return dbiChatInput;
552
- };
553
-
554
- let Event = function (cfg: TDBIEventOmitted<TNamespace>) {
555
- let dbiEvent = new DBIEvent(self as any, cfg);
556
- if (
557
- self.config.strict &&
558
- self.data.events.has(dbiEvent.id || dbiEvent.name)
559
- )
560
- throw new Error(
561
- `DBIEvent "${dbiEvent.id || dbiEvent.name}" already loaded!`
562
- );
563
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.events.set(dbiEvent.id || dbiEvent.name, dbiEvent);
564
- return dbiEvent;
565
- };
566
-
567
- let createInlineEvent = function (cfg: Omit<TDBIEventOmitted<TNamespace>, "id">) {
568
- return Event({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id: randomInlineId() } as any);
569
- }
570
-
571
- let Button = function (cfg: TDBIButtonOmitted<TNamespace>) {
572
- let dbiButton = new DBIButton(self as any, cfg);
573
- if (self.config.strict && self.data.interactions.has(dbiButton.name))
574
- throw new Error(
575
- `DBIButton "${dbiButton.name}" already loaded as "${self.data.interactions.get(dbiButton.name)?.type
576
- }"!`
577
- );
578
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(dbiButton.name, dbiButton as any);
579
- return dbiButton;
580
- };
581
-
582
- let createInlineButton = function (cfg: Omit<TDBIButtonOmitted<TNamespace>, "name" | "id">) {
583
- let id = randomInlineId();
584
- return Button({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
585
- }
586
-
587
- let StringSelectMenu = function (
588
- cfg: TDBIStringSelectMenuOmitted<TNamespace>
589
- ) {
590
- let dbiStringSelectMenu = new DBIStringSelectMenu(self as any, cfg);
591
- if (
592
- self.config.strict &&
593
- self.data.interactions.has(dbiStringSelectMenu.name)
594
- )
595
- throw new Error(
596
- `DBIStringSelectMenu "${dbiStringSelectMenu.name
597
- }" already loaded as "${self.data.interactions.get(dbiStringSelectMenu.name)?.type
598
- }"!`
599
- );
600
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
601
- dbiStringSelectMenu.name,
602
- dbiStringSelectMenu as any
603
- );
604
- return dbiStringSelectMenu;
605
- };
606
-
607
- let createInlineStringSelectMenu = function (cfg: Omit<TDBIStringSelectMenuOmitted<TNamespace>, "id" | "name">) {
608
- let id = randomInlineId();
609
- return StringSelectMenu({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
610
- }
611
-
612
- let UserSelectMenu = function (
613
- cfg: TDBIUserSelectMenuOmitted<TNamespace>
614
- ) {
615
- let dbiUserSelectMenu = new DBIUserSelectMenu(self as any, cfg);
616
- if (
617
- self.config.strict &&
618
- self.data.interactions.has(dbiUserSelectMenu.name)
619
- )
620
- throw new Error(
621
- `DBIUserSelectMenu "${dbiUserSelectMenu.name}" already loaded as "${self.data.interactions.get(dbiUserSelectMenu.name)?.type
622
- }"!`
623
- );
624
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
625
- dbiUserSelectMenu.name,
626
- dbiUserSelectMenu as any
627
- );
628
- return dbiUserSelectMenu;
629
- };
630
-
631
- let createInlineUserSelectMenu = function (cfg: Omit<TDBIUserSelectMenuOmitted<TNamespace>, "id" | "name">) {
632
- let id = randomInlineId();
633
- return UserSelectMenu({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
634
- }
635
-
636
- let RoleSelectMenu = function (
637
- cfg: TDBIRoleSelectMenuOmitted<TNamespace>
638
- ) {
639
- let dbiRoleSelectMenu = new DBIRoleSelectMenu(self as any, cfg);
640
- if (
641
- self.config.strict &&
642
- self.data.interactions.has(dbiRoleSelectMenu.name)
643
- )
644
- throw new Error(
645
- `DBIRoleSelectMenu "${dbiRoleSelectMenu.name}" already loaded as "${self.data.interactions.get(dbiRoleSelectMenu.name)?.type
646
- }"!`
647
- );
648
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
649
- dbiRoleSelectMenu.name,
650
- dbiRoleSelectMenu as any
651
- );
652
- return dbiRoleSelectMenu;
653
- };
654
-
655
- let createInlineRoleSelectMenu = function (cfg: Omit<TDBIRoleSelectMenuOmitted<TNamespace>, "id" | "name">) {
656
- let id = randomInlineId();
657
- return RoleSelectMenu({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
658
- }
659
-
660
- let ChannelSelectMenu = function (
661
- cfg: TDBIChannelSelectMenuOmitted<TNamespace>
662
- ) {
663
- let dbiChannelSelectMenu = new DBIChannelSelectMenu(self as any, cfg);
664
- if (
665
- self.config.strict &&
666
- self.data.interactions.has(dbiChannelSelectMenu.name)
667
- )
668
- throw new Error(
669
- `DBIChannelSelectMenu "${dbiChannelSelectMenu.name
670
- }" already loaded as "${self.data.interactions.get(dbiChannelSelectMenu.name)?.type
671
- }"!`
672
- );
673
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
674
- dbiChannelSelectMenu.name,
675
- dbiChannelSelectMenu as any
676
- );
677
- return dbiChannelSelectMenu;
678
- };
679
-
680
- let createInlineChannelSelectMenu = function (cfg: Omit<TDBIChannelSelectMenuOmitted<TNamespace>, "id" | "name">) {
681
- let id = randomInlineId();
682
- return ChannelSelectMenu({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
683
- }
684
-
685
- let MentionableSelectMenu = function (
686
- cfg: TDBIMentionableSelectMenuOmitted<TNamespace>
687
- ) {
688
- let dbiMentionableSelectMenu = new DBIMentionableSelectMenu(
689
- self as any,
690
- cfg
691
- );
692
- if (
693
- self.config.strict &&
694
- self.data.interactions.has(dbiMentionableSelectMenu.name)
695
- )
696
- throw new Error(
697
- `DBIMentionableSelectMenu "${dbiMentionableSelectMenu.name
698
- }" already loaded as "${self.data.interactions.get(dbiMentionableSelectMenu.name)?.type
699
- }"!`
700
- );
701
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
702
- dbiMentionableSelectMenu.name,
703
- dbiMentionableSelectMenu as any
704
- );
705
- return dbiMentionableSelectMenu;
706
- };
707
-
708
- let createInlineMentionableSelectMenu = function (cfg: Omit<TDBIMentionableSelectMenuOmitted<TNamespace>, "id" | "name">) {
709
- let id = randomInlineId();
710
- return MentionableSelectMenu({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
711
- }
712
-
713
- let MessageContextMenu = function (
714
- cfg: TDBIMessageContextMenuOmitted<TNamespace>
715
- ) {
716
- let dbiMessageContextMenu = new DBIMessageContextMenu(self as any, cfg);
717
- if (
718
- self.config.strict &&
719
- self.data.interactions.has(dbiMessageContextMenu.name)
720
- )
721
- throw new Error(
722
- `DBIMessageContextMenu "${dbiMessageContextMenu.name
723
- }" already loaded as "${self.data.interactions.get(dbiMessageContextMenu.name)?.type
724
- }"!`
725
- );
726
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
727
- dbiMessageContextMenu.name,
728
- dbiMessageContextMenu as any
729
- );
730
- return dbiMessageContextMenu;
731
- };
732
-
733
- let UserContextMenu = function (
734
- cfg: TDBIUserContextMenuOmitted<TNamespace>
735
- ) {
736
- let dbiUserContextMenu = new DBIUserContextMenu(self as any, cfg);
737
- if (
738
- self.config.strict &&
739
- self.data.interactions.has(dbiUserContextMenu.name)
740
- )
741
- throw new Error(
742
- `DBIUserContextMenu "${dbiUserContextMenu.name
743
- }" already loaded as "${self.data.interactions.get(dbiUserContextMenu.name)?.type
744
- }"!`
745
- );
746
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
747
- dbiUserContextMenu.name,
748
- dbiUserContextMenu as any
749
- );
750
- return dbiUserContextMenu;
751
- };
752
-
753
- let Modal = function (cfg: TDBIModalOmitted<TNamespace>) {
754
- let dbiModal = new DBIModal(self as any, cfg);
755
- if (self.config.strict && self.data.interactions.has(dbiModal.name))
756
- throw new Error(
757
- `DBIModal "${dbiModal.name}" already loaded as "${self.data.interactions.get(dbiModal.name)?.type
758
- }"!`
759
- );
760
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(dbiModal.name, dbiModal as any);
761
- return dbiModal;
762
- };
763
-
764
- let createInlineModal = function (cfg: Omit<TDBIModalOmitted<TNamespace>, "name" | "id">) {
765
- let id = randomInlineId();
766
- return Modal({ ...cfg, ttl: cfg?.ttl || self.config.inlineListeners.autoClear?.ttl, id, name: id } as any);
767
- }
768
-
769
- let Locale = function (cfg: TDBILocaleConstructor<TNamespace>) {
770
- let dbiLocale = new DBILocale(self as any, cfg);
771
- if (
772
- self.config.strict &&
773
- self.data.interactionLocales.has(dbiLocale.name)
774
- )
775
- throw new Error(`DBILocale "${dbiLocale.name}" already loaded!`);
776
- if (self.data.locales.has(dbiLocale.name))
777
- dbiLocale.mergeLocale(self.data.locales.get(dbiLocale.name));
778
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.locales.set(dbiLocale.name, dbiLocale);
779
- return dbiLocale;
780
- };
781
-
782
- let CustomEvent = function (cfg: TDBICustomEventOmitted<TNamespace>) {
783
- let dbiCustomEvent = new DBICustomEvent(self, cfg) as any;
784
- if (self.config.strict && self.data.eventMap[dbiCustomEvent.name])
785
- throw new Error(
786
- `DBICustomEvent "${dbiCustomEvent.name}" already loaded!`
787
- );
788
- self.data.eventMap[dbiCustomEvent.name] = dbiCustomEvent.map;
789
- self.data.customEventNames.add(dbiCustomEvent.name);
790
- return dbiCustomEvent;
791
- };
792
-
793
- let InteractionLocale = function (cfg: TDBIInteractionLocaleOmitted) {
794
- let dbiInteractionLocale = new DBIInteractionLocale(self, cfg);
795
- if (
796
- self.config.strict &&
797
- self.data.interactions.has(dbiInteractionLocale.name)
798
- )
799
- throw new Error(
800
- `DBIInteractionLocale "${dbiInteractionLocale.name}" already loaded!`
801
- );
802
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactionLocales.set(
803
- dbiInteractionLocale.name,
804
- dbiInteractionLocale
805
- );
806
- return dbiInteractionLocale;
807
- };
808
-
809
- let HTMLComponentsV2 = function (
810
- cfg: TDBIHTMLComponentsV2Omitted<TNamespace>
811
- ) {
812
- let dbiHTMLComponentsV2 = new DBIHTMLComponentsV2(self as any, cfg);
813
- if (
814
- self.config.strict &&
815
- self.data.interactions.has(dbiHTMLComponentsV2.name)
816
- )
817
- throw new Error(
818
- `DBIHTMLComponentsV2 "${dbiHTMLComponentsV2.name
819
- }" already loaded as "${self.data.interactions.get(dbiHTMLComponentsV2.name)?.type
820
- }"!`
821
- );
822
- if (!cfg.flag || flags.includes("all") || flags.includes(cfg.flag)) self.data.interactions.set(
823
- dbiHTMLComponentsV2.name,
824
- dbiHTMLComponentsV2 as any
825
- );
826
- return dbiHTMLComponentsV2;
827
- };
828
-
829
- await cb({
830
- ChatInput,
831
- Event,
832
- ChatInputOptions,
833
- Locale,
834
- Button,
835
- StringSelectMenu,
836
- UserSelectMenu,
837
- RoleSelectMenu,
838
- ChannelSelectMenu,
839
- MentionableSelectMenu,
840
- MessageContextMenu,
841
- UserContextMenu,
842
- CustomEvent,
843
- Modal,
844
- HTMLComponentsV2,
845
- HTMLComponentsV2Handlers,
846
- InteractionLocale,
847
- createInlineButton,
848
- createInlineEvent,
849
- createInlineStringSelectMenu,
850
- createInlineUserSelectMenu,
851
- createInlineRoleSelectMenu,
852
- createInlineChannelSelectMenu,
853
- createInlineMentionableSelectMenu,
854
- createInlineModal,
855
- onUnload(cb: () => Promise<any> | any) {
856
- self.data.registerUnloaders.add(cb);
857
- },
858
- });
859
- }
860
- self.data.interactions.sort((a, b) => b.name.length - a.name.length);
861
- }
862
-
863
- emit<
864
- TEventName extends keyof (NamespaceData[TNamespace]["customEvents"] &
865
- ClientEvents)
866
- >(
867
- name: TEventName,
868
- args: (NamespaceData[TNamespace]["customEvents"] & ClientEvents)[TEventName]
869
- ): void {
870
- this.data.clients.forEach((d) =>
871
- d.client.emit(name as any, { ...args, _DIRECT_: true } as any)
872
- );
873
- }
874
-
875
- /**
876
- * this.data.interactions.get(name)
877
- */
878
- interaction<
879
- TInteractionName extends keyof NamespaceData[TNamespace]["interactionMapping"]
880
- >(
881
- name: TInteractionName
882
- ): NamespaceData[TNamespace]["interactionMapping"][TInteractionName] {
883
- return this.data.interactions.get(name as any) as any;
884
- }
885
-
886
- client<TClientName extends NamespaceData[TNamespace]["clientNamespaces"]>(
887
- name?: TClientName
888
- ): TDBIClientData<TNamespace> {
889
- return name ? this.data.clients.get(name) : this.data.clients.first();
890
- }
891
- /**
892
- * this.data.events.get(name)
893
- */
894
- event<TEventName extends NamespaceData[TNamespace]["eventNames"]>(
895
- name: TEventName
896
- ): DBIEvent<TNamespace> {
897
- return this.data.events.get(name);
898
- }
899
-
900
- /**
901
- * this.data.locales.get(name)
902
- */
903
- locale<TLocaleName extends NamespaceData[TNamespace]["localeNames"]>(
904
- name: TLocaleName
905
- ): DBILocale<TNamespace> {
906
- return this.data.locales.get(name) as any;
907
- }
908
-
909
- /**
910
- * Shorthands for modifying `dbi.data.other`
911
- */
912
- get<K extends keyof TOtherData>(
913
- k: K,
914
- defaultValue?: TOtherData[K]
915
- ): TOtherData[K] {
916
- if (defaultValue && !this.has(k as any)) {
917
- this.set(k, defaultValue);
918
- return defaultValue;
919
- }
920
- return _.get(this.data.other, k);
921
- }
922
-
923
- /**
924
- * Shorthands for modifying `dbi.data.other`
925
- */
926
- set<K extends keyof TOtherData>(k: K, v: TOtherData[K]): any {
927
- this.data.other = _.set(this.data.other as any, k, v);
928
- }
929
-
930
- /**
931
- * Shorthands for modifying `dbi.data.other`
932
- */
933
- has(k: string): boolean {
934
- return _.has(this.data.other, k as any);
935
- }
936
-
937
- /**
938
- * Shorthands for modifying `dbi.data.other`
939
- */
940
- delete(k: string): boolean {
941
- return _.unset(this.data.other, k);
942
- }
943
-
944
- async login(): Promise<any> {
945
- await aaq.quickForEach(this.data.clients, async (clientContext) => {
946
- await clientContext.client.login(
947
- this.config.sharding == "default" ? null : clientContext.token
948
- );
949
- });
950
- this.events.trigger("clientsReady", undefined, true);
951
- }
952
-
953
- async register(cb: (api: DBIRegisterAPI<TNamespace>) => void): Promise<any> {
954
- this.data.registers.add(cb);
955
- }
956
-
957
- async load(...flags: string[]): Promise<boolean> {
958
- if (this._loaded) return false;
959
- await this._registerAll(flags);
960
- await this._hookListeners();
961
- this._loaded = true;
962
- return true;
963
- }
964
-
965
- async unload(): Promise<boolean> {
966
- if (!this._loaded) return false;
967
- await this._unregisterAll();
968
- await this._unhookListeners();
969
- this._loaded = false;
970
- return true;
971
- }
972
-
973
- get loaded(): boolean {
974
- return this._loaded;
975
- }
976
-
977
- async publish(type: "Global", clear?: boolean): Promise<any>;
978
- async publish(type: "Guild", guildId: string, clear?: boolean): Promise<any>;
979
-
980
- async publish(...args: any[]) {
981
- let interactions = this.data.interactions.filter(
982
- (i) =>
983
- i.type == "ChatInput" ||
984
- i.type == "MessageContextMenu" ||
985
- i.type == "UserContextMenu"
986
- ) as any;
987
- switch (args[0]) {
988
- case "Global": {
989
- return await publishInteractions(
990
- this.data.clients,
991
- args[1] ? new Discord.Collection() : interactions,
992
- this.data.interactionLocales,
993
- args[0]
994
- );
995
- }
996
- case "Guild": {
997
- return await publishInteractions(
998
- this.data.clients,
999
- args[2] ? new Discord.Collection() : interactions,
1000
- this.data.interactionLocales,
1001
- args[0],
1002
- args[1]
1003
- );
1004
- }
1005
- }
1006
- }
1007
- }