@mostfeatured/dbi 0.0.8 → 0.0.11

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 (59) hide show
  1. package/dist/DBI.d.ts +40 -16
  2. package/dist/DBI.d.ts.map +1 -1
  3. package/dist/DBI.js +61 -17
  4. package/dist/DBI.js.map +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/methods/hookInteractionListeners.d.ts.map +1 -1
  9. package/dist/methods/hookInteractionListeners.js +9 -5
  10. package/dist/methods/hookInteractionListeners.js.map +1 -1
  11. package/dist/methods/publishInteractions.d.ts +4 -1
  12. package/dist/methods/publishInteractions.d.ts.map +1 -1
  13. package/dist/methods/publishInteractions.js +83 -10
  14. package/dist/methods/publishInteractions.js.map +1 -1
  15. package/dist/test.d.ts +2 -0
  16. package/dist/test.d.ts.map +1 -0
  17. package/dist/test.js +46 -0
  18. package/dist/test.js.map +1 -0
  19. package/dist/types/Button.d.ts +2 -6
  20. package/dist/types/Button.d.ts.map +1 -1
  21. package/dist/types/Button.js.map +1 -1
  22. package/dist/types/Interaction.d.ts +5 -0
  23. package/dist/types/Interaction.d.ts.map +1 -1
  24. package/dist/types/Interaction.js.map +1 -1
  25. package/dist/types/InteractionLocale.d.ts +22 -0
  26. package/dist/types/InteractionLocale.d.ts.map +1 -0
  27. package/dist/types/InteractionLocale.js +15 -0
  28. package/dist/types/InteractionLocale.js.map +1 -0
  29. package/dist/types/Locale.d.ts +3 -2
  30. package/dist/types/Locale.d.ts.map +1 -1
  31. package/dist/types/Locale.js +6 -4
  32. package/dist/types/Locale.js.map +1 -1
  33. package/dist/types/Modal.d.ts +2 -6
  34. package/dist/types/Modal.d.ts.map +1 -1
  35. package/dist/types/Modal.js.map +1 -1
  36. package/dist/types/SelectMenu.d.ts +2 -6
  37. package/dist/types/SelectMenu.d.ts.map +1 -1
  38. package/dist/types/SelectMenu.js.map +1 -1
  39. package/dist/utils/MemoryStore.d.ts +1 -1
  40. package/dist/utils/MemoryStore.d.ts.map +1 -1
  41. package/dist/utils/MemoryStore.js +1 -1
  42. package/dist/utils/MemoryStore.js.map +1 -1
  43. package/dist/utils/recursiveImport.d.ts +4 -1
  44. package/dist/utils/recursiveImport.d.ts.map +1 -1
  45. package/dist/utils/recursiveImport.js +6 -3
  46. package/dist/utils/recursiveImport.js.map +1 -1
  47. package/package.json +2 -3
  48. package/src/DBI.ts +91 -36
  49. package/src/index.ts +3 -3
  50. package/src/methods/hookInteractionListeners.ts +9 -5
  51. package/src/methods/publishInteractions.ts +85 -8
  52. package/src/types/Button.ts +2 -2
  53. package/src/types/Interaction.ts +2 -0
  54. package/src/types/InteractionLocale.ts +28 -0
  55. package/src/types/Locale.ts +6 -5
  56. package/src/types/Modal.ts +2 -2
  57. package/src/types/SelectMenu.ts +2 -2
  58. package/src/utils/MemoryStore.ts +1 -1
  59. package/src/utils/recursiveImport.ts +6 -3
@@ -4,12 +4,16 @@ import { REST } from "@discordjs/rest";
4
4
  import { Routes, RESTGetAPIUserResult, RESTPutAPIApplicationCommandsJSONBody, ApplicationCommandType, ApplicationCommandOptionType } from "discord-api-types/v9";
5
5
  import { reducePermissions } from "../utils/permissions";
6
6
  import snakecaseKeys from "snakecase-keys";
7
+ import { DBI } from "../DBI";
8
+ import { DBIInteractionLocale } from "../types/InteractionLocale";
7
9
 
8
10
  const PUBLISHABLE_TYPES = ["ChatInput", "UserContextMenu", "MessageContextMenu"];
11
+ const ORIGINAL_LOCALES = ["da", "de", "en-GB", "en-US", "es-ES", "fr", "hr", "it", "lt", "hu", "nl", "no", "pl", "pt-BR", "ro", "fi", "sv-SE", "vi", "tr", "cs", "el", "bg", "ru", "uk", "hi", "th", "zh-CN", "ja", "zh-TW", "ko"];
9
12
 
10
13
  export async function publishInteractions(
11
14
  clientToken: string,
12
15
  interactions: Discord.Collection<string, DBIChatInput>,
16
+ interactionsLocales: Discord.Collection<string, DBIInteractionLocale>,
13
17
  publishType: "Guild" | "Global",
14
18
  guildId?: string
15
19
  ) {
@@ -20,11 +24,12 @@ export async function publishInteractions(
20
24
 
21
25
  const me: RESTGetAPIUserResult = await rest.get(Routes.user()) as any;
22
26
 
23
- const body: RESTPutAPIApplicationCommandsJSONBody =
27
+ let body: RESTPutAPIApplicationCommandsJSONBody =
24
28
  interactions.reduce((all, current) => {
25
29
  switch (current.type) {
26
30
  case "ChatInput": {
27
31
  let nameSplitted = current.name.split(" ");
32
+ let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
28
33
  switch (nameSplitted.length) {
29
34
  case 1: {
30
35
  all.push({
@@ -33,24 +38,30 @@ export async function publishInteractions(
33
38
  name: nameSplitted[0],
34
39
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
35
40
  dm_permission: current.directMessages,
36
- options: snakecaseKeys(current.options || [])
41
+ options: localeifyOptions(current.options || [], localeData.optionsLocales),
42
+ name_localizations: localeData.nameLocales(0),
43
+ description_localizations: localeData.descriptionLocales,
37
44
  });
38
45
  break;
39
46
  }
40
47
  case 2: {
41
48
  let baseItem = all.find(i => i.name == current.name.split(" ")[0] && i.type == "ChatInput");
49
+ let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
42
50
  let option = {
43
51
  type: ApplicationCommandOptionType.Subcommand,
44
52
  name: nameSplitted[1],
45
53
  description: current.description,
46
54
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
47
55
  dm_permission: current.directMessages,
48
- options: snakecaseKeys(current.options || [])
56
+ options: localeifyOptions(current.options || [], localeData.optionsLocales),
57
+ name_localizations: localeData.nameLocales(1),
58
+ description_localizations: localeData.descriptionLocales,
49
59
  };
50
60
  if (!baseItem) {
51
61
  all.push({
52
62
  type: ApplicationCommandType.ChatInput,
53
63
  name: nameSplitted[0],
64
+ name_localizations: localeData.nameLocales(0),
54
65
  description: "...",
55
66
  options: [
56
67
  option
@@ -63,15 +74,18 @@ export async function publishInteractions(
63
74
  }
64
75
  case 3: {
65
76
  let level1Item = all.find(i => i.name == current.name.split(" ")[0] && i.type == "ChatInput");
77
+ let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
66
78
  if (!level1Item) {
67
79
  all.push({
68
80
  type: ApplicationCommandType.ChatInput,
69
81
  name: nameSplitted[0],
82
+ name_localizations: localeData.nameLocales(0),
70
83
  description: "...",
71
84
  options: [
72
85
  {
73
86
  type: ApplicationCommandOptionType.SubcommandGroup,
74
87
  name: nameSplitted[1],
88
+ name_localizations: localeData.nameLocales(1),
75
89
  description: "...",
76
90
  options: [
77
91
  {
@@ -80,7 +94,9 @@ export async function publishInteractions(
80
94
  description: current.description,
81
95
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
82
96
  dm_permission: current.directMessages,
83
- options: snakecaseKeys(current.options || [])
97
+ options: localeifyOptions(current.options || [], localeData.optionsLocales),
98
+ name_localizations: localeData.nameLocales(2),
99
+ description_localizations: localeData.descriptionLocales,
84
100
  }
85
101
  ]
86
102
  }
@@ -92,6 +108,7 @@ export async function publishInteractions(
92
108
  level1Item.options.push({
93
109
  type: ApplicationCommandOptionType.SubcommandGroup,
94
110
  name: nameSplitted[1],
111
+ name_localizations: localeData.nameLocales(1),
95
112
  description: "...",
96
113
  options: [
97
114
  {
@@ -100,7 +117,9 @@ export async function publishInteractions(
100
117
  description: current.description,
101
118
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
102
119
  dm_permission: current.directMessages,
103
- options: snakecaseKeys(current.options || [])
120
+ options: localeifyOptions(current.options || [], localeData.optionsLocales),
121
+ name_localizations: localeData.nameLocales(2),
122
+ description_localizations: localeData.descriptionLocales
104
123
  }
105
124
  ]
106
125
  })
@@ -111,7 +130,9 @@ export async function publishInteractions(
111
130
  description: current.description,
112
131
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
113
132
  dm_permission: current.directMessages,
114
- options: snakecaseKeys(current.options || [])
133
+ options: localeifyOptions(current.options || [], localeData.optionsLocales),
134
+ name_localizations: localeData.nameLocales(2),
135
+ description_localizations: localeData.descriptionLocales,
115
136
  });
116
137
  }
117
138
  }
@@ -121,20 +142,26 @@ export async function publishInteractions(
121
142
  break;
122
143
  }
123
144
  case "MessageContextMenu": {
145
+ let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
124
146
  all.push({
125
147
  type: ApplicationCommandType.Message,
126
148
  name: current.name,
127
149
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
128
- dm_permission: current.directMessages
150
+ dm_permission: current.directMessages,
151
+ name_localizations: localeData.allNameLocales,
152
+ description_localizations: localeData.descriptionLocales,
129
153
  });
130
154
  break;
131
155
  }
132
156
  case "UserContextMenu": {
157
+ let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
133
158
  all.push({
134
159
  type: ApplicationCommandType.User,
135
160
  name: current.name,
136
161
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
137
- dm_permission: current.directMessages
162
+ dm_permission: current.directMessages,
163
+ name_localizations: localeData.allNameLocales,
164
+ description_localizations: localeData.descriptionLocales
138
165
  });
139
166
  break;
140
167
  }
@@ -143,6 +170,7 @@ export async function publishInteractions(
143
170
  return all;
144
171
  }, []);
145
172
 
173
+ body = snakecaseKeys(body);
146
174
 
147
175
  switch (publishType) {
148
176
  case "Global": {
@@ -155,4 +183,53 @@ export async function publishInteractions(
155
183
  }
156
184
  }
157
185
 
186
+ }
187
+
188
+ export function localeifyOptions(options: any[], localeData: any): any[] {
189
+ return options.map(i => {
190
+ return localeData[i.name] ? Object.assign(i, {
191
+ name_localizations: localeData[i.name].nameLocales,
192
+ description_localizations: localeData[i.name].descriptionLocales,
193
+ }) : i;
194
+ })
195
+ }
196
+
197
+ export function formatLocale(locale: DBIInteractionLocale): any {
198
+ let allNameLocales = {};
199
+ let descriptionLocales = {};
200
+ let optionsLocales = {};
201
+
202
+ function nameLocales(index) {
203
+ return Object.fromEntries(Object.entries(allNameLocales).map(i => [i[0], (i[1] as string).split(" ").at(index)]));
204
+ }
205
+
206
+ if (!locale?.data) return {
207
+ nameLocales,
208
+ allNameLocales,
209
+ descriptionLocales,
210
+ optionsLocales
211
+ };
212
+
213
+ Object.entries(locale.data).forEach(([shortLocale, localeData]) => {
214
+ let longAliases = ORIGINAL_LOCALES.filter(i => i.split("-").at(0) == shortLocale);
215
+ longAliases.forEach((longLocale) => {
216
+ allNameLocales[longLocale] = localeData.name;
217
+ descriptionLocales[longLocale] = localeData.description;
218
+ Object.entries(localeData.options || []).forEach(([optionName, optionData]) => {
219
+ if (!optionsLocales[optionName]) optionsLocales[optionName] = {};
220
+ if (!optionsLocales[optionName].nameLocales) optionsLocales[optionName].nameLocales = {};
221
+ if (!optionsLocales[optionName].descriptionLocales) optionsLocales[optionName].descriptionLocales = {};
222
+
223
+ optionsLocales[optionName].nameLocales[longLocale] = optionData.name;
224
+ optionsLocales[optionName].descriptionLocales[longLocale] = optionData.description;
225
+ })
226
+ });
227
+ });
228
+
229
+ return {
230
+ nameLocales,
231
+ allNameLocales,
232
+ descriptionLocales,
233
+ optionsLocales
234
+ }
158
235
  }
@@ -1,11 +1,11 @@
1
1
  import Discord from "discord.js";
2
2
  import { DBI } from "../DBI";
3
- import { DBIBaseInteraction, IDBIBaseExecuteCtx } from "./Interaction";
3
+ import { DBIBaseInteraction, IDBIBaseExecuteCtx, TDBIReferencedData } from "./Interaction";
4
4
  import { customIdBuilder } from "../utils/customId";
5
5
 
6
6
  export interface IDBIButtonExecuteCtx extends IDBIBaseExecuteCtx {
7
7
  interaction: Discord.ButtonInteraction<Discord.CacheType>;
8
- data: ({ [key: string]: any, $ref: string, $unRef(): boolean } | string | number)[];
8
+ data: TDBIReferencedData[];
9
9
  }
10
10
 
11
11
  export type TDBIButtonOmitted = Omit<DBIButton, "type" | "description" | "dbi" | "toJSON">;
@@ -20,6 +20,8 @@ export interface IDBIBaseExecuteCtx {
20
20
  other: Record<string, any>;
21
21
  }
22
22
 
23
+ export type TDBIReferencedData = ({ [key: string]: any, $ref: string, $unRef(): boolean } | string | number);
24
+
23
25
  export type TDBIInteractionTypes =
24
26
  | "ChatInput"
25
27
  | "UserContextMenu"
@@ -0,0 +1,28 @@
1
+ import { DBI } from "../DBI";
2
+ import { TDBILocaleString } from "./Locale";
3
+
4
+ export type TDBIInteractionLocaleData = {
5
+ [K in TDBILocaleString]?: {
6
+ name: string;
7
+ description: string;
8
+ options?: {
9
+ [k: string]: {
10
+ name: string;
11
+ description: string;
12
+ }
13
+ }
14
+ };
15
+ };
16
+
17
+ export type TDBIInteractionLocaleOmitted = Omit<DBIInteractionLocale, "dbi">;
18
+
19
+ export class DBIInteractionLocale {
20
+ name: string;
21
+ data: TDBIInteractionLocaleData;
22
+ dbi: DBI;
23
+ constructor(dbi, cfg: TDBIInteractionLocaleOmitted) {
24
+ this.dbi = dbi;
25
+ this.name = cfg.name;
26
+ this.data = cfg.data;
27
+ }
28
+ }
@@ -1,3 +1,4 @@
1
+ // @ts-ignore
1
2
  import * as stuffs from "stuffs";
2
3
  import { DBI } from "../DBI";
3
4
 
@@ -11,7 +12,7 @@ export interface LangConstructorObject {
11
12
 
12
13
  export type TDBILocaleString = "en" | "bg" | "zh" | "hr" | "cs" | "da" | "nl" | "fi" | "fr" | "de" | "el" | "hi" | "hu" | "it" | "ja" | "ko" | "no" | "pl" | "pt" | "ro" | "ru" | "es" | "sv" | "th" | "tr" | "uk" | "vi";
13
14
 
14
- export type TDBILocaleConstructor = Omit<DBILocale & { data: LangConstructorObject }, "dbi">;
15
+ export type TDBILocaleConstructor = Omit<DBILocale, "data" | "dbi"> & { data: LangConstructorObject };
15
16
 
16
17
  export class DBILocale {
17
18
  name: TDBILocaleString;
@@ -22,18 +23,18 @@ export class DBILocale {
22
23
  this.dbi = dbi;
23
24
  this.name = cfg.name;
24
25
  this._data = cfg.data;
25
- this.data = convert(cfg.data);
26
+ this.data = convertLang(cfg.data);
26
27
  }
27
28
  }
28
29
 
29
- function convert(data) {
30
+ export function convertLang(data: LangConstructorObject): LangObject {
30
31
  return Object.fromEntries(Object.entries(data).map(([key, value]) => {
31
32
  if (typeof value === "string") {
32
- return [key, (...args) => {
33
+ return [key, (...args: any[]) => {
33
34
  return stuffs.mapReplace(value, Object.fromEntries(args.map((t, i) => [`{${i}}`, t])))
34
35
  }]
35
36
  } else {
36
- return [key, convert(value)];
37
+ return [key, convertLang(value)];
37
38
  }
38
39
  }))
39
40
  }
@@ -1,12 +1,12 @@
1
1
  import { DBI } from "../DBI";
2
- import { DBIBaseInteraction, IDBIBaseExecuteCtx } from "./Interaction";
2
+ import { DBIBaseInteraction, IDBIBaseExecuteCtx, TDBIReferencedData } from "./Interaction";
3
3
  import Discord from "discord.js";
4
4
  import { customIdBuilder } from "../utils/customId";
5
5
 
6
6
  export interface IDBIModalExecuteCtx extends IDBIBaseExecuteCtx {
7
7
  interaction: Discord.ModalSubmitInteraction<Discord.CacheType>;
8
8
 
9
- data: ({ [key: string]: any, $ref: string, $unRef(): boolean } | string | number)[];
9
+ data: TDBIReferencedData[];
10
10
  }
11
11
 
12
12
  export type TDBIModalOmitted = Omit<DBIModal, "type" | "description" | "dbi" | "toJSON">;
@@ -1,11 +1,11 @@
1
1
  import Discord from "discord.js";
2
2
  import { DBI } from "../DBI";
3
- import { DBIBaseInteraction, IDBIBaseExecuteCtx } from "./Interaction";
3
+ import { DBIBaseInteraction, IDBIBaseExecuteCtx, TDBIReferencedData } from "./Interaction";
4
4
  import { customIdBuilder } from "../utils/customId";
5
5
 
6
6
  export interface IDBISelectMenuExecuteCtx extends IDBIBaseExecuteCtx {
7
7
  interaction: Discord.ButtonInteraction<Discord.CacheType>;
8
- data: (string | number | { [key: string]: any, $ref: string, $unRef(): boolean })[];
8
+ data: TDBIReferencedData[];
9
9
  }
10
10
 
11
11
  export type TDBISelectMenuOmitted = Omit<DBIBaseInteraction, "type" | "description" | "dbi" | "toJSON">;
@@ -18,7 +18,7 @@ export class MemoryStore {
18
18
  return this.store = _.set(this.store, key, value);
19
19
  }
20
20
 
21
- async del(key: string): Promise<boolean> {
21
+ async delete(key: string): Promise<boolean> {
22
22
  return _.unset(this.store, key);
23
23
  }
24
24
 
@@ -1,14 +1,17 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
 
4
- export async function recursiveImport(folderPath: string): Promise<any> {
4
+ /**
5
+ * Usage: `await recursiveImport("./src", [".js", ".ts"])`
6
+ */
7
+ export async function recursiveImport(folderPath: string, exts: string[] = [".js", ".ts"]): Promise<any> {
5
8
  let files = await fs.promises.readdir(folderPath, { withFileTypes: true });
6
9
 
7
10
  for (const file of files) {
8
11
  let filePath = path.resolve(folderPath, file.name);
9
12
  if (file.isDirectory()) {
10
- await recursiveImport(filePath)
11
- } else if (filePath.endsWith(".js")) {
13
+ await recursiveImport(filePath, exts)
14
+ } else if (exts.some(i => filePath.endsWith(i))) {
12
15
  await import(filePath)
13
16
  }
14
17
  }