@mostfeatured/dbi 0.0.10 → 0.0.13

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 (40) hide show
  1. package/dist/DBI.d.ts +42 -5
  2. package/dist/DBI.d.ts.map +1 -1
  3. package/dist/DBI.js +44 -2
  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.js +2 -2
  9. package/dist/methods/hookInteractionListeners.js.map +1 -1
  10. package/dist/methods/publishInteractions.d.ts +4 -1
  11. package/dist/methods/publishInteractions.d.ts.map +1 -1
  12. package/dist/methods/publishInteractions.js +83 -10
  13. package/dist/methods/publishInteractions.js.map +1 -1
  14. package/dist/types/ChatInput/ChatInput.d.ts.map +1 -1
  15. package/dist/types/ChatInput/ChatInput.js +2 -0
  16. package/dist/types/ChatInput/ChatInput.js.map +1 -1
  17. package/dist/types/InteractionLocale.d.ts +22 -0
  18. package/dist/types/InteractionLocale.d.ts.map +1 -0
  19. package/dist/types/InteractionLocale.js +15 -0
  20. package/dist/types/InteractionLocale.js.map +1 -0
  21. package/dist/types/MessageContextMenu.d.ts.map +1 -1
  22. package/dist/types/MessageContextMenu.js +2 -0
  23. package/dist/types/MessageContextMenu.js.map +1 -1
  24. package/dist/types/UserContextMenu.d.ts.map +1 -1
  25. package/dist/types/UserContextMenu.js +2 -0
  26. package/dist/types/UserContextMenu.js.map +1 -1
  27. package/dist/utils/MemoryStore.d.ts +1 -1
  28. package/dist/utils/MemoryStore.d.ts.map +1 -1
  29. package/dist/utils/MemoryStore.js +1 -1
  30. package/dist/utils/MemoryStore.js.map +1 -1
  31. package/package.json +2 -2
  32. package/src/DBI.ts +73 -8
  33. package/src/index.ts +3 -3
  34. package/src/methods/hookInteractionListeners.ts +2 -2
  35. package/src/methods/publishInteractions.ts +85 -8
  36. package/src/types/ChatInput/ChatInput.ts +3 -0
  37. package/src/types/InteractionLocale.ts +28 -0
  38. package/src/types/MessageContextMenu.ts +3 -0
  39. package/src/types/UserContextMenu.ts +3 -0
  40. package/src/utils/MemoryStore.ts +1 -1
@@ -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
  }
@@ -16,6 +16,9 @@ export class DBIChatInput extends DBIBaseInteraction {
16
16
  name: cfg.name.toLowerCase(),
17
17
  options: Array.isArray(cfg.options) ? cfg.options : []
18
18
  });
19
+
20
+ this.directMessages = cfg.directMessages ?? dbi.config.defaults.directMessages;
21
+ this.defaultMemberPermissions = cfg.defaultMemberPermissions ?? dbi.config.defaults.defaultMemberPermissions;
19
22
  }
20
23
  directMessages?: boolean;
21
24
  defaultMemberPermissions?: Discord.PermissionsString[];
@@ -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
+ }
@@ -15,6 +15,9 @@ export class DBIMessageContextMenu extends DBIBaseInteraction {
15
15
  ...(cfg as any),
16
16
  type: "MessageContextMenu"
17
17
  });
18
+
19
+ this.directMessages = cfg.directMessages ?? dbi.config.defaults.directMessages;
20
+ this.defaultMemberPermissions = cfg.defaultMemberPermissions ?? dbi.config.defaults.defaultMemberPermissions;
18
21
  }
19
22
 
20
23
  directMessages?: boolean;
@@ -14,6 +14,9 @@ export class DBIUserContextMenu extends DBIBaseInteraction {
14
14
  ...(cfg as any),
15
15
  type: "UserContextMenu"
16
16
  });
17
+
18
+ this.directMessages = cfg.directMessages ?? dbi.config.defaults.directMessages;
19
+ this.defaultMemberPermissions = cfg.defaultMemberPermissions ?? dbi.config.defaults.defaultMemberPermissions;
17
20
  }
18
21
  directMessages?: boolean;
19
22
  defaultMemberPermissions?: Discord.PermissionsString[];
@@ -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