@mostfeatured/dbi 0.0.64 → 0.0.65

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 (43) hide show
  1. package/dist/DBI.d.ts +179 -164
  2. package/dist/DBI.d.ts.map +1 -1
  3. package/dist/DBI.js +363 -332
  4. package/dist/DBI.js.map +1 -1
  5. package/dist/index.d.ts +16 -11
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +29 -21
  8. package/dist/index.js.map +1 -1
  9. package/dist/methods/hookEventListeners.d.ts.map +1 -1
  10. package/dist/methods/hookEventListeners.js +97 -87
  11. package/dist/methods/hookEventListeners.js.map +1 -1
  12. package/dist/methods/hookInteractionListeners.d.ts.map +1 -1
  13. package/dist/methods/hookInteractionListeners.js +129 -125
  14. package/dist/methods/hookInteractionListeners.js.map +1 -1
  15. package/dist/methods/publishInteractions.d.ts +8 -7
  16. package/dist/methods/publishInteractions.d.ts.map +1 -1
  17. package/dist/methods/publishInteractions.js +241 -236
  18. package/dist/methods/publishInteractions.js.map +1 -1
  19. package/dist/types/CustomEvent.js +19 -19
  20. package/dist/types/CustomEvent.js.map +1 -1
  21. package/dist/types/Event.d.ts +265 -263
  22. package/dist/types/Event.d.ts.map +1 -1
  23. package/dist/types/Event.js +24 -22
  24. package/dist/types/Event.js.map +1 -1
  25. package/dist/types/Interaction.d.ts +49 -47
  26. package/dist/types/Interaction.d.ts.map +1 -1
  27. package/dist/types/Interaction.js +26 -24
  28. package/dist/types/Interaction.js.map +1 -1
  29. package/dist/utils/recursiveImport.d.ts +5 -4
  30. package/dist/utils/recursiveImport.d.ts.map +1 -1
  31. package/dist/utils/recursiveImport.js +27 -26
  32. package/dist/utils/recursiveImport.js.map +1 -1
  33. package/generated/namespaceData.d.ts +2 -1
  34. package/package.json +2 -1
  35. package/src/DBI.ts +75 -23
  36. package/src/index.ts +11 -2
  37. package/src/methods/hookEventListeners.ts +21 -7
  38. package/src/methods/hookInteractionListeners.ts +6 -2
  39. package/src/methods/publishInteractions.ts +30 -25
  40. package/src/types/CustomEvent.ts +1 -1
  41. package/src/types/Event.ts +4 -2
  42. package/src/types/Interaction.ts +5 -2
  43. package/src/utils/recursiveImport.ts +4 -3
@@ -4,7 +4,7 @@ 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";
7
+ import { DBI, DBIClientData } from "../DBI";
8
8
  import { DBIInteractionLocale } from "../types/InteractionLocale";
9
9
  import { NamespaceEnums } from "../../generated/namespaceData";
10
10
 
@@ -12,29 +12,25 @@ const PUBLISHABLE_TYPES = ["ChatInput", "UserContextMenu", "MessageContextMenu"]
12
12
  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"];
13
13
 
14
14
  export async function publishInteractions(
15
- clientToken: string,
15
+ clients: DBIClientData<NamespaceEnums>[],
16
16
  interactions: Discord.Collection<string, DBIChatInput<NamespaceEnums>>,
17
17
  interactionsLocales: Discord.Collection<string, DBIInteractionLocale>,
18
18
  publishType: "Guild" | "Global",
19
19
  guildId?: string
20
20
  ) {
21
21
  interactions = interactions.filter(i => PUBLISHABLE_TYPES.includes(i.type));
22
-
23
- const rest = new REST({ version: "10" });
24
- rest.setToken(clientToken);
25
-
26
- const me: RESTGetAPIUserResult = await rest.get(Routes.user()) as any;
27
22
  interactions = interactions.sort((a, b) => b.name.split(" ").length - a.name.split(" ").length);
28
23
 
29
- let body: RESTPutAPIApplicationCommandsJSONBody =
24
+ let body: {[k: string]: RESTPutAPIApplicationCommandsJSONBody} =
30
25
  interactions.reduce((all, current) => {
26
+ if (current.publish && !all[current.publish]) all[current.publish] = [];
31
27
  switch (current.type) {
32
28
  case "ChatInput": {
33
29
  let nameSplitted = current.name.split(" ");
34
30
  let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
35
31
  switch (nameSplitted.length) {
36
32
  case 1: {
37
- all.push({
33
+ all[current.publish].push({
38
34
  type: ApplicationCommandType.ChatInput,
39
35
  description: current.description,
40
36
  name: nameSplitted[0],
@@ -47,7 +43,7 @@ export async function publishInteractions(
47
43
  break;
48
44
  }
49
45
  case 2: {
50
- let baseItem = all.find(i => i.name == current.name.split(" ")[0] && i.type == ApplicationCommandType.ChatInput);
46
+ let baseItem = all[current.publish].find(i => i.name == current.name.split(" ")[0] && i.type == ApplicationCommandType.ChatInput);
51
47
  let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
52
48
  let option = {
53
49
  type: ApplicationCommandOptionType.Subcommand,
@@ -59,7 +55,7 @@ export async function publishInteractions(
59
55
  description_localizations: localeData.descriptionLocales,
60
56
  };
61
57
  if (!baseItem) {
62
- all.push({
58
+ all[current.publish].push({
63
59
  type: ApplicationCommandType.ChatInput,
64
60
  name: nameSplitted[0],
65
61
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
@@ -75,10 +71,10 @@ export async function publishInteractions(
75
71
  break;
76
72
  }
77
73
  case 3: {
78
- let level1Item = all.find(i => i.name == current.name.split(" ")[0] && i.type == ApplicationCommandType.ChatInput);
74
+ let level1Item = all[current.publish].find(i => i.name == current.name.split(" ")[0] && i.type == ApplicationCommandType.ChatInput);
79
75
  let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
80
76
  if (!level1Item) {
81
- all.push({
77
+ all[current.publish].push({
82
78
  type: ApplicationCommandType.ChatInput,
83
79
  name: nameSplitted[0],
84
80
  name_localizations: localeData.nameLocales(0),
@@ -143,7 +139,7 @@ export async function publishInteractions(
143
139
  }
144
140
  case "MessageContextMenu": {
145
141
  let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
146
- all.push({
142
+ all[current.publish].push({
147
143
  type: ApplicationCommandType.Message,
148
144
  name: current.name,
149
145
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
@@ -155,29 +151,38 @@ export async function publishInteractions(
155
151
  }
156
152
  case "UserContextMenu": {
157
153
  let localeData = formatLocale(interactionsLocales.get(current.name) ?? {} as any);
158
- all.push({
154
+ all[current.publish].push({
159
155
  type: ApplicationCommandType.User,
160
156
  name: current.name,
161
157
  default_member_permissions: reducePermissions(current.defaultMemberPermissions).toString(),
162
158
  dm_permission: current.directMessages,
163
159
  name_localizations: localeData.allNameLocales,
164
- description_localizations: localeData.descriptionLocales
160
+ description_localizations: localeData.descriptionLocales,
165
161
  });
166
162
  break;
167
163
  }
168
164
  }
169
165
 
170
166
  return all;
171
- }, []);
167
+ }, {} as {[k: string]: any});
168
+
169
+
170
+ for (let i = 0; i < clients.length; i++) {
171
+ const client = clients[i];
172
+ const rest = new REST({ version: "10" });
173
+ rest.setToken(client.token);
172
174
 
173
- switch (publishType) {
174
- case "Guild": {
175
- await rest.put(Routes.applicationGuildCommands(me.id, guildId), { body });
176
- break;
177
- }
178
- case "Global": {
179
- await rest.put(Routes.applicationCommands(me.id), { body });
180
- break;
175
+ const me: RESTGetAPIUserResult = await rest.get(Routes.user()) as any;
176
+
177
+ switch (publishType) {
178
+ case "Guild": {
179
+ await rest.put(Routes.applicationGuildCommands(me.id, guildId), { body: body[client.namespace] });
180
+ break;
181
+ }
182
+ case "Global": {
183
+ await rest.put(Routes.applicationCommands(me.id), { body: body[client.namespace] });
184
+ break;
185
+ }
181
186
  }
182
187
  }
183
188
 
@@ -8,7 +8,7 @@ export class DBICustomEvent<TNamespace extends NamespaceEnums, CEventName extend
8
8
  map: {[key: string]: string};
9
9
  type: string;
10
10
  trigger(args: NamespaceData[TNamespace]["customEvents"][CEventName]) {
11
- return this.dbi.client.emit(this.name as string, { ...args, _DIRECT_: true});
11
+ return this.dbi.data.clients.first().client.emit(this.name as string, { ...args, _DIRECT_: true});
12
12
  }
13
13
  constructor(dbi: DBI<TNamespace>, cfg: TDBICustomEventOmitted<TNamespace, CEventName>) {
14
14
  this.dbi = dbi;
@@ -1,6 +1,6 @@
1
1
  import Discord from "discord.js";
2
2
  import { NamespaceEnums, NamespaceData } from "../../generated/namespaceData";
3
- import { DBI } from "../DBI";
3
+ import { DBI, DBIClientData } from "../DBI";
4
4
  import { DBILocale } from "./Locale";
5
5
 
6
6
  export interface ClientEvents {
@@ -93,7 +93,7 @@ export interface ClientEvents {
93
93
  export type DBIEventCombinations<TNamespace extends NamespaceEnums> = {
94
94
  [K in keyof (ClientEvents & NamespaceData[TNamespace]["customEvents"])]: {
95
95
  name: K,
96
- onExecute: (ctx: (ClientEvents & NamespaceData[TNamespace]["customEvents"])[K] & { other: Record<string, any>, locale?: { guild: DBILocale<TNamespace> }, eventName: string }) => Promise<any> | any
96
+ onExecute: (ctx: (ClientEvents & NamespaceData[TNamespace]["customEvents"])[K] & { other: Record<string, any>, locale?: { guild: DBILocale<TNamespace> }, eventName: string, nextClient: DBIClientData<TNamespace> }) => Promise<any> | any
97
97
  }
98
98
  }[keyof (ClientEvents) | keyof NamespaceData[TNamespace]["customEvents"]];
99
99
 
@@ -102,6 +102,7 @@ export type TDBIEventOmitted<TNamespace extends NamespaceEnums> = Omit<DBIEvent<
102
102
  export class DBIEvent<TNamespace extends NamespaceEnums> {
103
103
  readonly type: "Event";
104
104
  other?: Record<string, any>;
105
+ triggerType?: "OneByOne" | "OneByOneGlobal" | "Random" | "First";
105
106
  id?: string;
106
107
  name: string;
107
108
  onExecute: (...args: any[]) => any;
@@ -115,5 +116,6 @@ export class DBIEvent<TNamespace extends NamespaceEnums> {
115
116
  this.name = cfg.name as any;
116
117
  this.onExecute = cfg.onExecute;
117
118
  this.ordered = cfg.ordered ?? false;
119
+ this.triggerType = cfg.triggerType ?? "OneByOneGlobal";
118
120
  }
119
121
  }
@@ -1,5 +1,5 @@
1
1
  import Discord from "discord.js";
2
- import { NamespaceEnums } from "../../generated/namespaceData";
2
+ import { NamespaceEnums, NamespaceData } from "../../generated/namespaceData";
3
3
  import { DBI } from "../DBI";
4
4
  import { DBIButton } from "./Button";
5
5
  import { DBIChatInput } from "./ChatInput/ChatInput";
@@ -28,6 +28,7 @@ export interface IDBIBaseExecuteCtx<TNamespace extends NamespaceEnums> {
28
28
  dbiInteraction: TDBIInteractions<TNamespace>;
29
29
  setRateLimit(type: TDBIRateLimitTypes, duration: number): Promise<any>;
30
30
  other: Record<string, any>;
31
+ clientNamespace: NamespaceData[TNamespace]["clientNamespaces"];
31
32
  }
32
33
 
33
34
  export type TDBIReferencedData = ({ [key: string]: any, $ref: string, $unRef(): boolean } | string | number);
@@ -66,8 +67,10 @@ export class DBIBaseInteraction<TNamespace extends NamespaceEnums> {
66
67
  this.type = cfg.type;
67
68
  this.options = cfg.options;
68
69
  this.other = cfg.other;
70
+ this.publish = cfg.publish ?? dbi.data.clients.first()?.namespace;
69
71
  }
70
-
72
+
73
+ publish?: NamespaceData[TNamespace]["clientNamespaces"];
71
74
  dbi: DBI<TNamespace>;
72
75
  name: string;
73
76
  description: string;
@@ -2,9 +2,10 @@ import fs from "fs";
2
2
  import path from "path";
3
3
 
4
4
  /**
5
- * Usage: `await recursiveImport("./src", [".js", ".ts"])`
5
+ * @example
6
+ * await recursiveImport("./src", [".js"], [".d.ts"])
6
7
  */
7
- export async function recursiveImport(folderPath: string, exts: string[] = [".js", ".ts"]): Promise<any> {
8
+ export async function recursiveImport(folderPath: string, exts: string[] = [".js"], ignore: string[] = [".d.ts",".js.map",".d.ts.map"]): Promise<any> {
8
9
  let files = await fs.promises.readdir(folderPath, { withFileTypes: true });
9
10
  let dirName = __dirname;
10
11
 
@@ -14,7 +15,7 @@ export async function recursiveImport(folderPath: string, exts: string[] = [".js
14
15
  if (!relative.includes(`${path.sep}-`)) {
15
16
  if (file.isDirectory()) {
16
17
  await recursiveImport(filePath, exts)
17
- } else if (exts.some(i => file.name.endsWith(i))) {
18
+ } else if (exts.some(i => file.name.endsWith(i)) && !ignore.some(i => file.name.endsWith(i))) {
18
19
  await import(filePath)
19
20
  }
20
21
  }