@mostfeatured/dbi 0.1.29 → 0.1.30

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 (99) hide show
  1. package/.gitattributes +2 -2
  2. package/.hintrc +7 -7
  3. package/.vscode/settings.json +2 -2
  4. package/LICENSE +674 -674
  5. package/dist/DBI.js.map +1 -1
  6. package/dist/Events.js.map +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/methods/handleMessageCommands.js.map +1 -1
  9. package/dist/methods/hookEventListeners.js.map +1 -1
  10. package/dist/methods/hookInteractionListeners.js.map +1 -1
  11. package/dist/methods/publishInteractions.js.map +1 -1
  12. package/dist/types/ApplicationRoleConnectionMetadata.js.map +1 -1
  13. package/dist/types/Builders/ButtonBuilder.js.map +1 -1
  14. package/dist/types/Builders/ChannelSelectMenuBuilder.js.map +1 -1
  15. package/dist/types/Builders/MentionableSelectMenuBuilder.js.map +1 -1
  16. package/dist/types/Builders/ModalBuilder.js.map +1 -1
  17. package/dist/types/Builders/RoleSelectMenuBuilder.js.map +1 -1
  18. package/dist/types/Builders/StringSelectMenuBuilder.js.map +1 -1
  19. package/dist/types/Builders/UserSelectMenuBuilder.js.map +1 -1
  20. package/dist/types/ChatInput/ChatInput.js.map +1 -1
  21. package/dist/types/ChatInput/ChatInputOptions.d.ts +16 -16
  22. package/dist/types/ChatInput/ChatInputOptions.d.ts.map +1 -1
  23. package/dist/types/ChatInput/ChatInputOptions.js.map +1 -1
  24. package/dist/types/Components/Button.js.map +1 -1
  25. package/dist/types/Components/ChannelSelectMenu.js.map +1 -1
  26. package/dist/types/Components/MentionableSelectMenu.js.map +1 -1
  27. package/dist/types/Components/Modal.js.map +1 -1
  28. package/dist/types/Components/RoleSelectMenu.js.map +1 -1
  29. package/dist/types/Components/StringSelectMenu.js.map +1 -1
  30. package/dist/types/Components/UserSelectMenu.js.map +1 -1
  31. package/dist/types/Event.js.map +1 -1
  32. package/dist/types/Interaction.js.map +1 -1
  33. package/dist/types/other/CustomEvent.js.map +1 -1
  34. package/dist/types/other/FakeMessageInteraction.js.map +1 -1
  35. package/dist/types/other/InteractionLocale.js.map +1 -1
  36. package/dist/types/other/Locale.js.map +1 -1
  37. package/dist/types/other/MessageContextMenu.js.map +1 -1
  38. package/dist/types/other/UserContextMenu.js.map +1 -1
  39. package/dist/utils/MemoryStore.js.map +1 -1
  40. package/dist/utils/UtilTypes.js.map +1 -1
  41. package/dist/utils/customId.js.map +1 -1
  42. package/dist/utils/permissions.js.map +1 -1
  43. package/dist/utils/recursiveImport.js.map +1 -1
  44. package/dist/utils/recursiveUnload.js.map +1 -1
  45. package/dist/utils/unloadModule.js.map +1 -1
  46. package/examples/modal/dbi.js +29 -29
  47. package/examples/modal/login.js +14 -14
  48. package/examples/modal/package.json +15 -15
  49. package/examples/modal/publish.js +12 -12
  50. package/examples/modal/src/chatInput.js +37 -37
  51. package/examples/modal/src/components.js +32 -32
  52. package/examples/modal/src/event.js +19 -19
  53. package/examples/modal/src/interactionlocales.js +23 -23
  54. package/examples/modal/src/locales.js +37 -37
  55. package/examples/modal/src/modal.js +27 -27
  56. package/generated/namespaceData.d.ts +14 -14
  57. package/package.json +6 -6
  58. package/readme.md +591 -591
  59. package/src/DBI.ts +1090 -1090
  60. package/src/Events.ts +189 -189
  61. package/src/data/eventMap.json +247 -247
  62. package/src/index.ts +23 -23
  63. package/src/methods/handleMessageCommands.ts +482 -482
  64. package/src/methods/hookEventListeners.ts +118 -118
  65. package/src/methods/hookInteractionListeners.ts +260 -260
  66. package/src/methods/publishInteractions.ts +255 -255
  67. package/src/types/ApplicationRoleConnectionMetadata.ts +19 -19
  68. package/src/types/Builders/ButtonBuilder.ts +52 -52
  69. package/src/types/Builders/ChannelSelectMenuBuilder.ts +52 -52
  70. package/src/types/Builders/MentionableSelectMenuBuilder.ts +52 -52
  71. package/src/types/Builders/ModalBuilder.ts +52 -52
  72. package/src/types/Builders/RoleSelectMenuBuilder.ts +52 -52
  73. package/src/types/Builders/StringSelectMenuBuilder.ts +52 -52
  74. package/src/types/Builders/UserSelectMenuBuilder.ts +52 -52
  75. package/src/types/ChatInput/ChatInput.ts +27 -27
  76. package/src/types/ChatInput/ChatInputOptions.ts +388 -388
  77. package/src/types/Components/Button.ts +38 -38
  78. package/src/types/Components/ChannelSelectMenu.ts +42 -42
  79. package/src/types/Components/MentionableSelectMenu.ts +42 -42
  80. package/src/types/Components/Modal.ts +45 -45
  81. package/src/types/Components/RoleSelectMenu.ts +42 -42
  82. package/src/types/Components/StringSelectMenu.ts +42 -42
  83. package/src/types/Components/UserSelectMenu.ts +42 -42
  84. package/src/types/Event.ts +144 -144
  85. package/src/types/Interaction.ts +96 -96
  86. package/src/types/other/CustomEvent.ts +18 -18
  87. package/src/types/other/FakeMessageInteraction.ts +407 -407
  88. package/src/types/other/InteractionLocale.ts +33 -33
  89. package/src/types/other/Locale.ts +69 -69
  90. package/src/types/other/MessageContextMenu.ts +26 -26
  91. package/src/types/other/UserContextMenu.ts +24 -24
  92. package/src/utils/MemoryStore.ts +27 -27
  93. package/src/utils/UtilTypes.ts +10 -10
  94. package/src/utils/customId.ts +44 -44
  95. package/src/utils/permissions.ts +4 -4
  96. package/src/utils/recursiveImport.ts +22 -22
  97. package/src/utils/recursiveUnload.ts +24 -24
  98. package/src/utils/unloadModule.ts +6 -6
  99. package/tsconfig.json +40 -40
@@ -1,482 +1,482 @@
1
- import {
2
- ApplicationCommandType,
3
- ChatInputCommandInteraction,
4
- Message,
5
- MessagePayload,
6
- ApplicationCommandOptionType,
7
- } from "discord.js";
8
- import { NamespaceEnums } from "../../generated/namespaceData";
9
- import { DBI } from "../DBI";
10
- import { FakeMessageInteraction } from "../types/other/FakeMessageInteraction";
11
- import { TDBILocaleString } from "../types/other/Locale";
12
-
13
- const INTEGER_REGEX = /^-?\d+$/;
14
- const NUMBER_REGEX = /^-?\d+(?:\.\d+)?$/;
15
-
16
- export type TDBIMessageCommandArgumentErrorTypes =
17
- | "MissingRequiredOption"
18
- | "MinLength"
19
- | "MaxLength"
20
- | "InvalidChoice"
21
- | "InvalidInteger"
22
- | "MinInteger"
23
- | "MaxInteger"
24
- | "InvalidNumber"
25
- | "MinNumber"
26
- | "MaxNumber"
27
- | "InvalidBoolean"
28
- | "InvalidUser"
29
- | "InvalidChannel"
30
- | "InvalidRole"
31
- | "InvalidMentionable"
32
- | "InvalidCompleteChoice";
33
-
34
- export async function handleMessageCommands(
35
- dbi: DBI<NamespaceEnums>,
36
- message: Message
37
- ) {
38
- const chatInputs = dbi.data.interactions.filter(
39
- (i) => i.type === "ChatInput"
40
- );
41
- const prefixes = await dbi.config.messageCommands.prefixes({ message });
42
- if (!prefixes?.length) return;
43
- const content = message.content;
44
- const usedPrefix = prefixes.find((p) => content.startsWith(p));
45
- if (!usedPrefix) return;
46
- const contentWithoutPrefix = content.slice(usedPrefix?.length ?? 0);
47
- const contentLower = contentWithoutPrefix.toLowerCase();
48
-
49
- let locale: string =
50
- message.guild.preferredLocale?.split("-")?.at(0) ||
51
- (dbi.config.defaults.locale as any);
52
- let usedAlias: string | undefined;
53
- let chatInput = chatInputs.find((i) => {
54
- let found = contentLower.startsWith(i.name);
55
- if (found) return true;
56
- let alias = i.other?.messageCommand?.aliases?.find((a) =>
57
- contentLower.startsWith(a)
58
- );
59
- if (alias) {
60
- usedAlias = alias;
61
- return true;
62
- }
63
- return false;
64
- });
65
- let commandName = usedAlias ?? chatInput?.name;
66
-
67
- if (!chatInput) {
68
- fLoop: for (const [localeInterName, localeData] of dbi.data
69
- .interactionLocales) {
70
- for (const [localeName, translation] of Object.entries(
71
- localeData.data || {}
72
- )) {
73
- if (contentLower.startsWith(translation.name)) {
74
- commandName = translation.name;
75
- locale = localeName;
76
- chatInput = chatInputs.find((i) => i.name === localeData.name);
77
- break fLoop;
78
- }
79
- }
80
- }
81
- }
82
-
83
- if (!chatInput) return;
84
-
85
- const interaction = new FakeMessageInteraction(
86
- dbi,
87
- message,
88
- chatInput as any,
89
- locale,
90
- commandName,
91
- usedPrefix
92
- );
93
-
94
- const builtLocale = {
95
- user:
96
- dbi.data.locales.get(interaction.locale) ||
97
- dbi.data.locales.get(dbi.config.defaults.locale.name),
98
- guild: message.guild?.preferredLocale
99
- ? dbi.data.locales.get(
100
- message.guild?.preferredLocale?.split("-")?.at(0)
101
- ) || dbi.data.locales.get(dbi.config.defaults.locale.name)
102
- : null,
103
- };
104
-
105
- const { defaultMemberPermissions, directMessages } = chatInput as any;
106
-
107
- if (typeof directMessages !== "undefined" && !directMessages && !message.guild) {
108
- const res = await dbi.events.trigger(
109
- "messageCommandDirectMessageUsageError", {
110
- interaction,
111
- message,
112
- locale: builtLocale,
113
- dbiInteraction: chatInput
114
- });
115
- if (!res) return;
116
- }
117
-
118
- if (Array.isArray(defaultMemberPermissions) && message.guild && message.member) {
119
- const perms = message.member.permissions.toArray();
120
- if (!defaultMemberPermissions.every((p) => perms.includes(p))) {
121
- const res = await dbi.events.trigger(
122
- "messageCommandDefaultMemberPermissionsError", {
123
- interaction,
124
- message,
125
- locale: builtLocale,
126
- dbiInteraction: chatInput,
127
- permissions: defaultMemberPermissions
128
- });
129
- if (!res) return;
130
- }
131
- }
132
-
133
- if (chatInput.options.length) {
134
- let errorType: TDBIMessageCommandArgumentErrorTypes;
135
- let lastOption: any;
136
- let lastValue: any;
137
- let lastExtra: any;
138
- let lastIndex: number;
139
- for (let i = 0; i < chatInput.options.length; i++) {
140
- lastIndex = i;
141
- const option: any = interaction.dbiChatInputOptions[i];
142
- const value = interaction.parsedArgs.get(option.name)?.value;
143
-
144
- lastOption = option;
145
- lastValue = value;
146
-
147
- switch (option.type) {
148
- case ApplicationCommandOptionType.String: {
149
- if (!option.required && !value) break;
150
-
151
- if (option.autocomplete && option.onComplete) {
152
- let choices = await option.onComplete({
153
- interaction,
154
- value,
155
- });
156
- if (!choices.length)
157
- choices = await option.onComplete({
158
- interaction,
159
- value: "",
160
- });
161
- if (choices.length > 20)
162
- throw new Error("Autocomplete returned more than 20 choices.");
163
- lastExtra = choices;
164
- if (!choices.find((c) => c.name === value || c.value === value)) {
165
- if (value) {
166
- errorType = "InvalidCompleteChoice";
167
- break;
168
- } else if (option.required && !value) {
169
- errorType = "MissingRequiredOption";
170
- break;
171
- }
172
- }
173
- option._choices = choices;
174
- }
175
-
176
- if (option.choices) {
177
- const localeData = dbi.data.interactionLocales.get(
178
- chatInput.name
179
- )?.data;
180
- const choicesLocaleData =
181
- localeData?.[locale as TDBILocaleString]?.options?.[option.name]
182
- ?.choices;
183
- if (
184
- !option.choices.find(
185
- (c) =>
186
- c.name === value ||
187
- c.value === value ||
188
- (choicesLocaleData?.[c.value] &&
189
- choicesLocaleData?.[c.value] === value)
190
- )
191
- ) {
192
- lastExtra = option.choices.map((c) => ({
193
- name: choicesLocaleData?.[c.value] ?? c.name,
194
- value: c.value,
195
- }));
196
- if (value) {
197
- errorType = "InvalidChoice";
198
- break;
199
- } else if (option.required && !value) {
200
- errorType = "MissingRequiredOption";
201
- break;
202
- }
203
- }
204
- break;
205
- }
206
-
207
- if (option.required && !value) {
208
- errorType = "MissingRequiredOption";
209
- break;
210
- }
211
- if (option.minLength && value?.length < option.minLength) {
212
- errorType = "MinLength";
213
- break;
214
- }
215
- if (option.maxLength && value?.length > option.maxLength) {
216
- errorType = "MaxLength";
217
- break;
218
- }
219
-
220
- break;
221
- }
222
- case ApplicationCommandOptionType.Integer: {
223
- if (!option.required && !value) break;
224
-
225
- let parsedInt = parseInt(value);
226
-
227
- if (option.autocomplete && option.onComplete) {
228
- let choices = await option.onComplete({
229
- interaction,
230
- value,
231
- });
232
- if (!choices.length)
233
- choices = await option.onComplete({
234
- interaction,
235
- value: "",
236
- });
237
- if (choices.length > 20)
238
- throw new Error("Autocomplete returned more than 20 choices.");
239
- lastExtra = choices;
240
- if (
241
- !choices.find((c) => c.value === parsedInt || c.name === value)
242
- ) {
243
- if (value) {
244
- errorType = "InvalidCompleteChoice";
245
- break;
246
- } else if (option.required && !value) {
247
- errorType = "MissingRequiredOption";
248
- break;
249
- }
250
- }
251
- option._choices = choices;
252
- break;
253
- }
254
-
255
- if (option.choices) {
256
- const localeData = dbi.data.interactionLocales.get(
257
- chatInput.name
258
- )?.data;
259
- const choicesLocaleData =
260
- localeData?.[locale as TDBILocaleString]?.options?.[option.name]
261
- ?.choices;
262
- if (
263
- !option.choices.find(
264
- (c) =>
265
- c.value === parsedInt ||
266
- c.name === value ||
267
- (choicesLocaleData?.[c.value] &&
268
- choicesLocaleData?.[c.value] === value)
269
- )
270
- ) {
271
- lastExtra = option.choices.map((c) => ({
272
- name: choicesLocaleData?.[c.value] ?? c.name,
273
- value: c.value,
274
- }));
275
- if (value) {
276
- errorType = "InvalidChoice";
277
- break;
278
- } else if (option.required && !value) {
279
- errorType = "MissingRequiredOption";
280
- break;
281
- }
282
- }
283
- break;
284
- }
285
-
286
- if (!INTEGER_REGEX.test(value)) {
287
- errorType = "InvalidInteger";
288
- break;
289
- }
290
-
291
- if (option.minValue && parsedInt < option.minValue) {
292
- errorType = "MinInteger";
293
- break;
294
- }
295
-
296
- if (option.maxValue && parsedInt > option.maxValue) {
297
- errorType = "MaxInteger";
298
- break;
299
- }
300
-
301
- break;
302
- }
303
- case ApplicationCommandOptionType.Number: {
304
- if (!option.required && !value) break;
305
-
306
- let parsedFloat = parseFloat(value);
307
-
308
- if (option.autocomplete && option.onComplete) {
309
- let choices = await option.onComplete({
310
- interaction,
311
- value,
312
- });
313
- if (!choices.length)
314
- choices = await option.onComplete({
315
- interaction,
316
- value: "",
317
- });
318
- if (choices.length > 20)
319
- throw new Error("Autocomplete returned more than 20 choices.");
320
- lastExtra = choices;
321
- if (
322
- !choices.find((c) => c.value === parsedFloat || c.name === value)
323
- ) {
324
- if (value) {
325
- errorType = "InvalidCompleteChoice";
326
- break;
327
- } else if (option.required && !value) {
328
- errorType = "MissingRequiredOption";
329
- break;
330
- }
331
- }
332
- option._choices = choices;
333
- break;
334
- }
335
-
336
- if (option.choices) {
337
- const localeData = dbi.data.interactionLocales.get(
338
- chatInput.name
339
- )?.data;
340
- const choicesLocaleData =
341
- localeData?.[locale as TDBILocaleString]?.options?.[option.name]
342
- ?.choices;
343
- if (
344
- !option.choices.find(
345
- (c) =>
346
- c.value === parsedFloat ||
347
- c.name === value ||
348
- (choicesLocaleData?.[c.value] &&
349
- choicesLocaleData?.[c.value] === value)
350
- )
351
- ) {
352
- lastExtra = option.choices.map((c) => ({
353
- name: choicesLocaleData?.[c.value] ?? c.name,
354
- value: c.value,
355
- }));
356
- if (value) {
357
- errorType = "InvalidChoice";
358
- break;
359
- } else if (option.required && !value) {
360
- errorType = "MissingRequiredOption";
361
- break;
362
- }
363
- }
364
- break;
365
- }
366
-
367
- if (!NUMBER_REGEX.test(value)) {
368
- errorType = "InvalidNumber";
369
- break;
370
- }
371
-
372
- if (option.minValue && parsedFloat < option.minValue) {
373
- errorType = "MinNumber";
374
- break;
375
- }
376
-
377
- if (option.maxValue && parsedFloat > option.maxValue) {
378
- errorType = "MaxNumber";
379
- break;
380
- }
381
- break;
382
- }
383
- case ApplicationCommandOptionType.Boolean: {
384
- let boolKeys = Object.keys(
385
- dbi.config.messageCommands.typeAliases.booleans
386
- );
387
- if (option.required && !boolKeys.includes(value?.toLowerCase?.())) {
388
- errorType = "InvalidBoolean";
389
- break;
390
- }
391
- break;
392
- }
393
- case ApplicationCommandOptionType.User: {
394
- await message.client.users
395
- .fetch(interaction.options.getUserId(option.name))
396
- .catch(() => { });
397
- if (option.required && !interaction.options.getUser(option.name)) {
398
- errorType = "InvalidUser";
399
- break;
400
- }
401
- break;
402
- }
403
- case ApplicationCommandOptionType.Channel: {
404
- await message.client.channels
405
- .fetch(interaction.options.getChannelId(option.name))
406
- .catch(() => { });
407
- if (
408
- option.required &&
409
- !interaction.options.getChannel(
410
- option.name,
411
- null,
412
- option.channelTypes
413
- )
414
- ) {
415
- errorType = "InvalidChannel";
416
- break;
417
- }
418
- break;
419
- }
420
- case ApplicationCommandOptionType.Role: {
421
- await message.guild.roles
422
- .fetch(interaction.options.getRoleId(option.name))
423
- .catch(() => { });
424
- if (option.required && !interaction.options.getRole(option.name)) {
425
- errorType = "InvalidRole";
426
- break;
427
- }
428
- break;
429
- }
430
- case ApplicationCommandOptionType.Mentionable: {
431
- let mentionableId = interaction.options.getMentionableId(option.name);
432
- await message.guild.roles.fetch(mentionableId).catch(() => { });
433
- await message.client.channels.fetch(mentionableId).catch(() => { });
434
- await message.client.users.fetch(mentionableId).catch(() => { });
435
- if (
436
- option.required &&
437
- !interaction.options.getMentionable(option.name)
438
- ) {
439
- errorType = "InvalidMentionable";
440
- break;
441
- }
442
- break;
443
- }
444
- case ApplicationCommandOptionType.Attachment: {
445
- if (option.required && !value) {
446
- errorType = "MissingRequiredOption";
447
- }
448
- break;
449
- }
450
- }
451
-
452
- if (errorType) {
453
- break;
454
- } else {
455
- lastExtra = null;
456
- lastIndex = null;
457
- lastOption = null;
458
- lastValue = null;
459
- }
460
- }
461
-
462
- if (errorType) {
463
- let res = await dbi.events.trigger("messageCommandArgumentError", {
464
- interaction,
465
- message,
466
- locale: builtLocale,
467
- error: {
468
- type: errorType,
469
- option: lastOption,
470
- extra: lastExtra,
471
- index: lastIndex,
472
- },
473
- value: lastValue,
474
- dbiInteraction: chatInput,
475
- });
476
- if (!res) return;
477
- }
478
- }
479
-
480
- interaction.init();
481
- dbi.data.clients.first().client.emit("interactionCreate", interaction as any);
482
- }
1
+ import {
2
+ ApplicationCommandType,
3
+ ChatInputCommandInteraction,
4
+ Message,
5
+ MessagePayload,
6
+ ApplicationCommandOptionType,
7
+ } from "discord.js";
8
+ import { NamespaceEnums } from "../../generated/namespaceData";
9
+ import { DBI } from "../DBI";
10
+ import { FakeMessageInteraction } from "../types/other/FakeMessageInteraction";
11
+ import { TDBILocaleString } from "../types/other/Locale";
12
+
13
+ const INTEGER_REGEX = /^-?\d+$/;
14
+ const NUMBER_REGEX = /^-?\d+(?:\.\d+)?$/;
15
+
16
+ export type TDBIMessageCommandArgumentErrorTypes =
17
+ | "MissingRequiredOption"
18
+ | "MinLength"
19
+ | "MaxLength"
20
+ | "InvalidChoice"
21
+ | "InvalidInteger"
22
+ | "MinInteger"
23
+ | "MaxInteger"
24
+ | "InvalidNumber"
25
+ | "MinNumber"
26
+ | "MaxNumber"
27
+ | "InvalidBoolean"
28
+ | "InvalidUser"
29
+ | "InvalidChannel"
30
+ | "InvalidRole"
31
+ | "InvalidMentionable"
32
+ | "InvalidCompleteChoice";
33
+
34
+ export async function handleMessageCommands(
35
+ dbi: DBI<NamespaceEnums>,
36
+ message: Message
37
+ ) {
38
+ const chatInputs = dbi.data.interactions.filter(
39
+ (i) => i.type === "ChatInput"
40
+ );
41
+ const prefixes = await dbi.config.messageCommands.prefixes({ message });
42
+ if (!prefixes?.length) return;
43
+ const content = message.content;
44
+ const usedPrefix = prefixes.find((p) => content.startsWith(p));
45
+ if (!usedPrefix) return;
46
+ const contentWithoutPrefix = content.slice(usedPrefix?.length ?? 0);
47
+ const contentLower = contentWithoutPrefix.toLowerCase();
48
+
49
+ let locale: string =
50
+ message.guild.preferredLocale?.split("-")?.at(0) ||
51
+ (dbi.config.defaults.locale as any);
52
+ let usedAlias: string | undefined;
53
+ let chatInput = chatInputs.find((i) => {
54
+ let found = contentLower.startsWith(i.name);
55
+ if (found) return true;
56
+ let alias = i.other?.messageCommand?.aliases?.find((a) =>
57
+ contentLower.startsWith(a)
58
+ );
59
+ if (alias) {
60
+ usedAlias = alias;
61
+ return true;
62
+ }
63
+ return false;
64
+ });
65
+ let commandName = usedAlias ?? chatInput?.name;
66
+
67
+ if (!chatInput) {
68
+ fLoop: for (const [localeInterName, localeData] of dbi.data
69
+ .interactionLocales) {
70
+ for (const [localeName, translation] of Object.entries(
71
+ localeData.data || {}
72
+ )) {
73
+ if (contentLower.startsWith(translation.name)) {
74
+ commandName = translation.name;
75
+ locale = localeName;
76
+ chatInput = chatInputs.find((i) => i.name === localeData.name);
77
+ break fLoop;
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ if (!chatInput) return;
84
+
85
+ const interaction = new FakeMessageInteraction(
86
+ dbi,
87
+ message,
88
+ chatInput as any,
89
+ locale,
90
+ commandName,
91
+ usedPrefix
92
+ );
93
+
94
+ const builtLocale = {
95
+ user:
96
+ dbi.data.locales.get(interaction.locale) ||
97
+ dbi.data.locales.get(dbi.config.defaults.locale.name),
98
+ guild: message.guild?.preferredLocale
99
+ ? dbi.data.locales.get(
100
+ message.guild?.preferredLocale?.split("-")?.at(0)
101
+ ) || dbi.data.locales.get(dbi.config.defaults.locale.name)
102
+ : null,
103
+ };
104
+
105
+ const { defaultMemberPermissions, directMessages } = chatInput as any;
106
+
107
+ if (typeof directMessages !== "undefined" && !directMessages && !message.guild) {
108
+ const res = await dbi.events.trigger(
109
+ "messageCommandDirectMessageUsageError", {
110
+ interaction,
111
+ message,
112
+ locale: builtLocale,
113
+ dbiInteraction: chatInput
114
+ });
115
+ if (!res) return;
116
+ }
117
+
118
+ if (Array.isArray(defaultMemberPermissions) && message.guild && message.member) {
119
+ const perms = message.member.permissions.toArray();
120
+ if (!defaultMemberPermissions.every((p) => perms.includes(p))) {
121
+ const res = await dbi.events.trigger(
122
+ "messageCommandDefaultMemberPermissionsError", {
123
+ interaction,
124
+ message,
125
+ locale: builtLocale,
126
+ dbiInteraction: chatInput,
127
+ permissions: defaultMemberPermissions
128
+ });
129
+ if (!res) return;
130
+ }
131
+ }
132
+
133
+ if (chatInput.options.length) {
134
+ let errorType: TDBIMessageCommandArgumentErrorTypes;
135
+ let lastOption: any;
136
+ let lastValue: any;
137
+ let lastExtra: any;
138
+ let lastIndex: number;
139
+ for (let i = 0; i < chatInput.options.length; i++) {
140
+ lastIndex = i;
141
+ const option: any = interaction.dbiChatInputOptions[i];
142
+ const value = interaction.parsedArgs.get(option.name)?.value;
143
+
144
+ lastOption = option;
145
+ lastValue = value;
146
+
147
+ switch (option.type) {
148
+ case ApplicationCommandOptionType.String: {
149
+ if (!option.required && !value) break;
150
+
151
+ if (option.autocomplete && option.onComplete) {
152
+ let choices = await option.onComplete({
153
+ interaction,
154
+ value,
155
+ });
156
+ if (!choices.length)
157
+ choices = await option.onComplete({
158
+ interaction,
159
+ value: "",
160
+ });
161
+ if (choices.length > 20)
162
+ throw new Error("Autocomplete returned more than 20 choices.");
163
+ lastExtra = choices;
164
+ if (!choices.find((c) => c.name === value || c.value === value)) {
165
+ if (value) {
166
+ errorType = "InvalidCompleteChoice";
167
+ break;
168
+ } else if (option.required && !value) {
169
+ errorType = "MissingRequiredOption";
170
+ break;
171
+ }
172
+ }
173
+ option._choices = choices;
174
+ }
175
+
176
+ if (option.choices) {
177
+ const localeData = dbi.data.interactionLocales.get(
178
+ chatInput.name
179
+ )?.data;
180
+ const choicesLocaleData =
181
+ localeData?.[locale as TDBILocaleString]?.options?.[option.name]
182
+ ?.choices;
183
+ if (
184
+ !option.choices.find(
185
+ (c) =>
186
+ c.name === value ||
187
+ c.value === value ||
188
+ (choicesLocaleData?.[c.value] &&
189
+ choicesLocaleData?.[c.value] === value)
190
+ )
191
+ ) {
192
+ lastExtra = option.choices.map((c) => ({
193
+ name: choicesLocaleData?.[c.value] ?? c.name,
194
+ value: c.value,
195
+ }));
196
+ if (value) {
197
+ errorType = "InvalidChoice";
198
+ break;
199
+ } else if (option.required && !value) {
200
+ errorType = "MissingRequiredOption";
201
+ break;
202
+ }
203
+ }
204
+ break;
205
+ }
206
+
207
+ if (option.required && !value) {
208
+ errorType = "MissingRequiredOption";
209
+ break;
210
+ }
211
+ if (option.minLength && value?.length < option.minLength) {
212
+ errorType = "MinLength";
213
+ break;
214
+ }
215
+ if (option.maxLength && value?.length > option.maxLength) {
216
+ errorType = "MaxLength";
217
+ break;
218
+ }
219
+
220
+ break;
221
+ }
222
+ case ApplicationCommandOptionType.Integer: {
223
+ if (!option.required && !value) break;
224
+
225
+ let parsedInt = parseInt(value);
226
+
227
+ if (option.autocomplete && option.onComplete) {
228
+ let choices = await option.onComplete({
229
+ interaction,
230
+ value,
231
+ });
232
+ if (!choices.length)
233
+ choices = await option.onComplete({
234
+ interaction,
235
+ value: "",
236
+ });
237
+ if (choices.length > 20)
238
+ throw new Error("Autocomplete returned more than 20 choices.");
239
+ lastExtra = choices;
240
+ if (
241
+ !choices.find((c) => c.value === parsedInt || c.name === value)
242
+ ) {
243
+ if (value) {
244
+ errorType = "InvalidCompleteChoice";
245
+ break;
246
+ } else if (option.required && !value) {
247
+ errorType = "MissingRequiredOption";
248
+ break;
249
+ }
250
+ }
251
+ option._choices = choices;
252
+ break;
253
+ }
254
+
255
+ if (option.choices) {
256
+ const localeData = dbi.data.interactionLocales.get(
257
+ chatInput.name
258
+ )?.data;
259
+ const choicesLocaleData =
260
+ localeData?.[locale as TDBILocaleString]?.options?.[option.name]
261
+ ?.choices;
262
+ if (
263
+ !option.choices.find(
264
+ (c) =>
265
+ c.value === parsedInt ||
266
+ c.name === value ||
267
+ (choicesLocaleData?.[c.value] &&
268
+ choicesLocaleData?.[c.value] === value)
269
+ )
270
+ ) {
271
+ lastExtra = option.choices.map((c) => ({
272
+ name: choicesLocaleData?.[c.value] ?? c.name,
273
+ value: c.value,
274
+ }));
275
+ if (value) {
276
+ errorType = "InvalidChoice";
277
+ break;
278
+ } else if (option.required && !value) {
279
+ errorType = "MissingRequiredOption";
280
+ break;
281
+ }
282
+ }
283
+ break;
284
+ }
285
+
286
+ if (!INTEGER_REGEX.test(value)) {
287
+ errorType = "InvalidInteger";
288
+ break;
289
+ }
290
+
291
+ if (option.minValue && parsedInt < option.minValue) {
292
+ errorType = "MinInteger";
293
+ break;
294
+ }
295
+
296
+ if (option.maxValue && parsedInt > option.maxValue) {
297
+ errorType = "MaxInteger";
298
+ break;
299
+ }
300
+
301
+ break;
302
+ }
303
+ case ApplicationCommandOptionType.Number: {
304
+ if (!option.required && !value) break;
305
+
306
+ let parsedFloat = parseFloat(value);
307
+
308
+ if (option.autocomplete && option.onComplete) {
309
+ let choices = await option.onComplete({
310
+ interaction,
311
+ value,
312
+ });
313
+ if (!choices.length)
314
+ choices = await option.onComplete({
315
+ interaction,
316
+ value: "",
317
+ });
318
+ if (choices.length > 20)
319
+ throw new Error("Autocomplete returned more than 20 choices.");
320
+ lastExtra = choices;
321
+ if (
322
+ !choices.find((c) => c.value === parsedFloat || c.name === value)
323
+ ) {
324
+ if (value) {
325
+ errorType = "InvalidCompleteChoice";
326
+ break;
327
+ } else if (option.required && !value) {
328
+ errorType = "MissingRequiredOption";
329
+ break;
330
+ }
331
+ }
332
+ option._choices = choices;
333
+ break;
334
+ }
335
+
336
+ if (option.choices) {
337
+ const localeData = dbi.data.interactionLocales.get(
338
+ chatInput.name
339
+ )?.data;
340
+ const choicesLocaleData =
341
+ localeData?.[locale as TDBILocaleString]?.options?.[option.name]
342
+ ?.choices;
343
+ if (
344
+ !option.choices.find(
345
+ (c) =>
346
+ c.value === parsedFloat ||
347
+ c.name === value ||
348
+ (choicesLocaleData?.[c.value] &&
349
+ choicesLocaleData?.[c.value] === value)
350
+ )
351
+ ) {
352
+ lastExtra = option.choices.map((c) => ({
353
+ name: choicesLocaleData?.[c.value] ?? c.name,
354
+ value: c.value,
355
+ }));
356
+ if (value) {
357
+ errorType = "InvalidChoice";
358
+ break;
359
+ } else if (option.required && !value) {
360
+ errorType = "MissingRequiredOption";
361
+ break;
362
+ }
363
+ }
364
+ break;
365
+ }
366
+
367
+ if (!NUMBER_REGEX.test(value)) {
368
+ errorType = "InvalidNumber";
369
+ break;
370
+ }
371
+
372
+ if (option.minValue && parsedFloat < option.minValue) {
373
+ errorType = "MinNumber";
374
+ break;
375
+ }
376
+
377
+ if (option.maxValue && parsedFloat > option.maxValue) {
378
+ errorType = "MaxNumber";
379
+ break;
380
+ }
381
+ break;
382
+ }
383
+ case ApplicationCommandOptionType.Boolean: {
384
+ let boolKeys = Object.keys(
385
+ dbi.config.messageCommands.typeAliases.booleans
386
+ );
387
+ if (option.required && !boolKeys.includes(value?.toLowerCase?.())) {
388
+ errorType = "InvalidBoolean";
389
+ break;
390
+ }
391
+ break;
392
+ }
393
+ case ApplicationCommandOptionType.User: {
394
+ await message.client.users
395
+ .fetch(interaction.options.getUserId(option.name))
396
+ .catch(() => { });
397
+ if (option.required && !interaction.options.getUser(option.name)) {
398
+ errorType = "InvalidUser";
399
+ break;
400
+ }
401
+ break;
402
+ }
403
+ case ApplicationCommandOptionType.Channel: {
404
+ await message.client.channels
405
+ .fetch(interaction.options.getChannelId(option.name))
406
+ .catch(() => { });
407
+ if (
408
+ option.required &&
409
+ !interaction.options.getChannel(
410
+ option.name,
411
+ null,
412
+ option.channelTypes
413
+ )
414
+ ) {
415
+ errorType = "InvalidChannel";
416
+ break;
417
+ }
418
+ break;
419
+ }
420
+ case ApplicationCommandOptionType.Role: {
421
+ await message.guild.roles
422
+ .fetch(interaction.options.getRoleId(option.name))
423
+ .catch(() => { });
424
+ if (option.required && !interaction.options.getRole(option.name)) {
425
+ errorType = "InvalidRole";
426
+ break;
427
+ }
428
+ break;
429
+ }
430
+ case ApplicationCommandOptionType.Mentionable: {
431
+ let mentionableId = interaction.options.getMentionableId(option.name);
432
+ await message.guild.roles.fetch(mentionableId).catch(() => { });
433
+ await message.client.channels.fetch(mentionableId).catch(() => { });
434
+ await message.client.users.fetch(mentionableId).catch(() => { });
435
+ if (
436
+ option.required &&
437
+ !interaction.options.getMentionable(option.name)
438
+ ) {
439
+ errorType = "InvalidMentionable";
440
+ break;
441
+ }
442
+ break;
443
+ }
444
+ case ApplicationCommandOptionType.Attachment: {
445
+ if (option.required && !value) {
446
+ errorType = "MissingRequiredOption";
447
+ }
448
+ break;
449
+ }
450
+ }
451
+
452
+ if (errorType) {
453
+ break;
454
+ } else {
455
+ lastExtra = null;
456
+ lastIndex = null;
457
+ lastOption = null;
458
+ lastValue = null;
459
+ }
460
+ }
461
+
462
+ if (errorType) {
463
+ let res = await dbi.events.trigger("messageCommandArgumentError", {
464
+ interaction,
465
+ message,
466
+ locale: builtLocale,
467
+ error: {
468
+ type: errorType,
469
+ option: lastOption,
470
+ extra: lastExtra,
471
+ index: lastIndex,
472
+ },
473
+ value: lastValue,
474
+ dbiInteraction: chatInput,
475
+ });
476
+ if (!res) return;
477
+ }
478
+ }
479
+
480
+ interaction.init();
481
+ dbi.data.clients.first().client.emit("interactionCreate", interaction as any);
482
+ }