@mostfeatured/dbi 0.0.9 → 0.0.12
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.
- package/dist/DBI.d.ts +40 -16
- package/dist/DBI.d.ts.map +1 -1
- package/dist/DBI.js +61 -17
- package/dist/DBI.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/methods/hookInteractionListeners.d.ts.map +1 -1
- package/dist/methods/hookInteractionListeners.js +8 -4
- package/dist/methods/hookInteractionListeners.js.map +1 -1
- package/dist/methods/publishInteractions.d.ts +4 -1
- package/dist/methods/publishInteractions.d.ts.map +1 -1
- package/dist/methods/publishInteractions.js +83 -10
- package/dist/methods/publishInteractions.js.map +1 -1
- package/dist/types/Button.d.ts +2 -6
- package/dist/types/Button.d.ts.map +1 -1
- package/dist/types/Button.js.map +1 -1
- package/dist/types/Interaction.d.ts +5 -0
- package/dist/types/Interaction.d.ts.map +1 -1
- package/dist/types/Interaction.js.map +1 -1
- package/dist/types/InteractionLocale.d.ts +22 -0
- package/dist/types/InteractionLocale.d.ts.map +1 -0
- package/dist/types/InteractionLocale.js +15 -0
- package/dist/types/InteractionLocale.js.map +1 -0
- package/dist/types/Locale.d.ts +3 -2
- package/dist/types/Locale.d.ts.map +1 -1
- package/dist/types/Locale.js +6 -4
- package/dist/types/Locale.js.map +1 -1
- package/dist/types/Modal.d.ts +2 -6
- package/dist/types/Modal.d.ts.map +1 -1
- package/dist/types/Modal.js.map +1 -1
- package/dist/types/SelectMenu.d.ts +2 -6
- package/dist/types/SelectMenu.d.ts.map +1 -1
- package/dist/types/SelectMenu.js.map +1 -1
- package/dist/utils/MemoryStore.d.ts +1 -1
- package/dist/utils/MemoryStore.d.ts.map +1 -1
- package/dist/utils/MemoryStore.js +1 -1
- package/dist/utils/MemoryStore.js.map +1 -1
- package/package.json +2 -2
- package/src/DBI.ts +91 -36
- package/src/index.ts +3 -3
- package/src/methods/hookInteractionListeners.ts +8 -4
- package/src/methods/publishInteractions.ts +85 -8
- package/src/types/Button.ts +2 -2
- package/src/types/Interaction.ts +2 -0
- package/src/types/InteractionLocale.ts +28 -0
- package/src/types/Locale.ts +6 -5
- package/src/types/Modal.ts +2 -2
- package/src/types/SelectMenu.ts +2 -2
- package/src/utils/MemoryStore.ts +1 -1
package/package.json
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
"@discordjs/rest": "^1.0.1",
|
|
4
4
|
"discord-api-types": "^0.37.0",
|
|
5
5
|
"discord-hybrid-sharding": "^1.7.4",
|
|
6
|
-
"discord.js": "^14.
|
|
6
|
+
"discord.js": "^14.3.0",
|
|
7
7
|
"lodash": "^4.17.21",
|
|
8
8
|
"snakecase-keys": "^5.4.2",
|
|
9
9
|
"stuffs": "^0.1.17"
|
|
10
10
|
},
|
|
11
11
|
"name": "@mostfeatured/dbi",
|
|
12
|
-
"version": "0.0.
|
|
12
|
+
"version": "0.0.12",
|
|
13
13
|
"main": "dist/index.js",
|
|
14
14
|
"type": "commonjs",
|
|
15
15
|
"private": false,
|
package/src/DBI.ts
CHANGED
|
@@ -15,28 +15,36 @@ import { hookEventListeners } from "./methods/hookEventListeners";
|
|
|
15
15
|
import eventMap from "./data/eventMap.json";
|
|
16
16
|
import { DBIModal, TDBIModalOmitted } from "./types/Modal";
|
|
17
17
|
import * as Sharding from "discord-hybrid-sharding";
|
|
18
|
+
import _ from "lodash";
|
|
19
|
+
import { DBIInteractionLocale, TDBIInteractionLocaleOmitted } from "./types/InteractionLocale";
|
|
20
|
+
|
|
21
|
+
export interface DBIStore {
|
|
22
|
+
get(key: string, defaultValue?: any): Promise<any>;
|
|
23
|
+
set(key: string, value: any): Promise<void>;
|
|
24
|
+
delete(key: string): Promise<void>;
|
|
25
|
+
has(key: string): Promise<boolean>;
|
|
26
|
+
}
|
|
18
27
|
|
|
19
28
|
export interface DBIConfig {
|
|
20
29
|
discord: {
|
|
21
30
|
token: string;
|
|
22
|
-
options
|
|
31
|
+
options: Discord.ClientOptions
|
|
23
32
|
}
|
|
24
|
-
defaults
|
|
25
|
-
locale
|
|
26
|
-
directMessages
|
|
27
|
-
defaultMemberPermissions
|
|
33
|
+
defaults: {
|
|
34
|
+
locale: TDBILocaleString,
|
|
35
|
+
directMessages: boolean,
|
|
36
|
+
defaultMemberPermissions: Discord.PermissionsString[]
|
|
28
37
|
};
|
|
29
38
|
|
|
30
|
-
sharding
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
del(key: string): Promise<void>;
|
|
36
|
-
has(key: string): Promise<boolean>;
|
|
37
|
-
}
|
|
39
|
+
sharding: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Persist store. (Default to MemoryStore thats not persis tho.)
|
|
42
|
+
*/
|
|
43
|
+
store: DBIStore;
|
|
38
44
|
}
|
|
39
45
|
|
|
46
|
+
export type TDBIConfigConstructor = Partial<DBIConfig>;
|
|
47
|
+
|
|
40
48
|
export interface DBIRegisterAPI {
|
|
41
49
|
ChatInput(cfg: TDBIChatInputOmitted): DBIChatInput;
|
|
42
50
|
ChatInputOptions: typeof DBIChatInputOptions;
|
|
@@ -46,11 +54,12 @@ export interface DBIRegisterAPI {
|
|
|
46
54
|
SelectMenu(cfg: TDBISelectMenuOmitted): DBISelectMenu;
|
|
47
55
|
MessageContextMenu(cfg: TDBIMessageContextMenuOmitted): DBIMessageContextMenu;
|
|
48
56
|
UserContextMenu(cfg: TDBIUserContextMenuOmitted): DBIUserContextMenu;
|
|
57
|
+
InteractionLocale(cfg: TDBIInteractionLocaleOmitted): DBIInteractionLocale;
|
|
49
58
|
Modal(cfg: TDBIModalOmitted): DBIModal;
|
|
50
|
-
onUnload(cb: ()=>Promise<any>);
|
|
59
|
+
onUnload(cb: () => Promise<any> | any): any;
|
|
51
60
|
}
|
|
52
61
|
|
|
53
|
-
export class DBI {
|
|
62
|
+
export class DBI<TOtherData = Record<string, any>> {
|
|
54
63
|
namespace: string;
|
|
55
64
|
config: DBIConfig;
|
|
56
65
|
client: Discord.Client<true>;
|
|
@@ -59,7 +68,8 @@ export class DBI {
|
|
|
59
68
|
events: Discord.Collection<string, DBIEvent>;
|
|
60
69
|
plugins: Discord.Collection<string, any>;
|
|
61
70
|
locales: Discord.Collection<string, DBILocale>;
|
|
62
|
-
|
|
71
|
+
interactionLocales: Discord.Collection<string, DBIInteractionLocale>;
|
|
72
|
+
other: TOtherData;
|
|
63
73
|
eventMap: Record<string, string[]>;
|
|
64
74
|
unloaders: Set<() => void>;
|
|
65
75
|
registers: Set<(...args: any[]) => any>;
|
|
@@ -69,7 +79,7 @@ export class DBI {
|
|
|
69
79
|
events: Events;
|
|
70
80
|
cluster?: Sharding.Client;
|
|
71
81
|
private _loaded: boolean;
|
|
72
|
-
constructor(namespace: string, config:
|
|
82
|
+
constructor(namespace: string, config: TDBIConfigConstructor) {
|
|
73
83
|
this.namespace = namespace;
|
|
74
84
|
|
|
75
85
|
config.store = config.store as any || new MemoryStore();
|
|
@@ -79,7 +89,9 @@ export class DBI {
|
|
|
79
89
|
directMessages: false,
|
|
80
90
|
...(config.defaults || {})
|
|
81
91
|
};
|
|
92
|
+
config.sharding = config.sharding ?? false;
|
|
82
93
|
|
|
94
|
+
// @ts-ignore
|
|
83
95
|
this.config = config;
|
|
84
96
|
|
|
85
97
|
this.data = {
|
|
@@ -87,7 +99,8 @@ export class DBI {
|
|
|
87
99
|
events: new Discord.Collection(),
|
|
88
100
|
plugins: new Discord.Collection(),
|
|
89
101
|
locales: new Discord.Collection(),
|
|
90
|
-
|
|
102
|
+
interactionLocales: new Discord.Collection(),
|
|
103
|
+
other: {} as TOtherData,
|
|
91
104
|
eventMap,
|
|
92
105
|
unloaders: new Set(),
|
|
93
106
|
registers: new Set(),
|
|
@@ -103,7 +116,7 @@ export class DBI {
|
|
|
103
116
|
shardCount: (Sharding as any).data.TOTAL_SHARDS
|
|
104
117
|
} : {})
|
|
105
118
|
});
|
|
106
|
-
this.cluster = config.sharding ? new Sharding.Client(this.client) :
|
|
119
|
+
this.cluster = config.sharding ? new Sharding.Client(this.client) : undefined;
|
|
107
120
|
this._hookListeners();
|
|
108
121
|
this._loaded = false;
|
|
109
122
|
}
|
|
@@ -128,11 +141,11 @@ export class DBI {
|
|
|
128
141
|
for await (const cb of this.data.registers) {
|
|
129
142
|
let ChatInput = function(cfg: DBIChatInput) {
|
|
130
143
|
let dbiChatInput = new DBIChatInput(self, cfg);
|
|
131
|
-
if (self.data.interactions.has(dbiChatInput.name)) throw new Error(`DBIChatInput "${dbiChatInput.name}" already loaded as "${self.data.interactions.get(dbiChatInput.name)
|
|
144
|
+
if (self.data.interactions.has(dbiChatInput.name)) throw new Error(`DBIChatInput "${dbiChatInput.name}" already loaded as "${self.data.interactions.get(dbiChatInput.name)?.type}"!`);
|
|
132
145
|
self.data.interactions.set(dbiChatInput.name, dbiChatInput);
|
|
133
146
|
return dbiChatInput;
|
|
134
147
|
};
|
|
135
|
-
ChatInput = Object.assign(ChatInput, class { constructor(...args) { return ChatInput.
|
|
148
|
+
ChatInput = Object.assign(ChatInput, class { constructor(...args: any[]) { return ChatInput.apply(this, args as any); } });
|
|
136
149
|
|
|
137
150
|
let Event = function(cfg: TDBIEventOmitted) {
|
|
138
151
|
let dbiEvent = new DBIEvent(self, cfg);
|
|
@@ -140,48 +153,47 @@ export class DBI {
|
|
|
140
153
|
self.data.events.set(dbiEvent.name, dbiEvent);
|
|
141
154
|
return dbiEvent;
|
|
142
155
|
};
|
|
143
|
-
Event = Object.assign(Event, class { constructor(...args) { return Event.
|
|
156
|
+
Event = Object.assign(Event, class { constructor(...args: any[]) { return Event.apply(this, args as any); } });
|
|
144
157
|
|
|
145
158
|
let Button = function(cfg: TDBIButtonOmitted) {
|
|
146
159
|
let dbiButton = new DBIButton(self, cfg);
|
|
147
|
-
if (self.data.interactions.has(dbiButton.name)) throw new Error(`DBIButton "${dbiButton.name}" already loaded as "${self.data.interactions.get(dbiButton.name)
|
|
160
|
+
if (self.data.interactions.has(dbiButton.name)) throw new Error(`DBIButton "${dbiButton.name}" already loaded as "${self.data.interactions.get(dbiButton.name)?.type}"!`);
|
|
148
161
|
self.data.interactions.set(dbiButton.name, dbiButton);
|
|
149
162
|
return dbiButton;
|
|
150
163
|
};
|
|
151
|
-
Button = Object.assign(Button, class { constructor(...args) { return Button.
|
|
164
|
+
Button = Object.assign(Button, class { constructor(...args: any[]) { return Button.apply(this, args as any); } });
|
|
152
165
|
|
|
153
166
|
let SelectMenu = function(cfg: TDBISelectMenuOmitted) {
|
|
154
167
|
let dbiSelectMenu = new DBISelectMenu(self, cfg);
|
|
155
|
-
if (self.data.interactions.has(dbiSelectMenu.name)) throw new Error(`DBISelectMenu "${dbiSelectMenu.name}" already loaded as "${self.data.interactions.get(dbiSelectMenu.name)
|
|
168
|
+
if (self.data.interactions.has(dbiSelectMenu.name)) throw new Error(`DBISelectMenu "${dbiSelectMenu.name}" already loaded as "${self.data.interactions.get(dbiSelectMenu.name)?.type}"!`);
|
|
156
169
|
self.data.interactions.set(dbiSelectMenu.name, dbiSelectMenu);
|
|
157
170
|
return dbiSelectMenu;
|
|
158
171
|
};
|
|
159
|
-
SelectMenu = Object.assign(SelectMenu, class { constructor(...args) { return SelectMenu.
|
|
172
|
+
SelectMenu = Object.assign(SelectMenu, class { constructor(...args: any[]) { return SelectMenu.apply(this, args as any); } });
|
|
160
173
|
|
|
161
174
|
let MessageContextMenu = function(cfg: TDBIMessageContextMenuOmitted) {
|
|
162
175
|
let dbiMessageContextMenu = new DBIMessageContextMenu(self, cfg);
|
|
163
|
-
if (self.data.interactions.has(dbiMessageContextMenu.name)) throw new Error(`DBIMessageContextMenu "${dbiMessageContextMenu.name}" already loaded as "${self.data.interactions.get(dbiMessageContextMenu.name)
|
|
176
|
+
if (self.data.interactions.has(dbiMessageContextMenu.name)) throw new Error(`DBIMessageContextMenu "${dbiMessageContextMenu.name}" already loaded as "${self.data.interactions.get(dbiMessageContextMenu.name)?.type}"!`);
|
|
164
177
|
self.data.interactions.set(dbiMessageContextMenu.name, dbiMessageContextMenu);
|
|
165
178
|
return dbiMessageContextMenu;
|
|
166
179
|
};
|
|
167
|
-
MessageContextMenu = Object.assign(MessageContextMenu, class { constructor(...args) { return MessageContextMenu.
|
|
180
|
+
MessageContextMenu = Object.assign(MessageContextMenu, class { constructor(...args: any[]) { return MessageContextMenu.apply(this, args as any); } });
|
|
168
181
|
|
|
169
182
|
let UserContextMenu = function(cfg: TDBIUserContextMenuOmitted) {
|
|
170
183
|
let dbiUserContextMenu = new DBIUserContextMenu(self, cfg);
|
|
171
|
-
if (self.data.interactions.has(dbiUserContextMenu.name)) throw new Error(`DBIUserContextMenu "${dbiUserContextMenu.name}" already loaded as "${self.data.interactions.get(dbiUserContextMenu.name)
|
|
184
|
+
if (self.data.interactions.has(dbiUserContextMenu.name)) throw new Error(`DBIUserContextMenu "${dbiUserContextMenu.name}" already loaded as "${self.data.interactions.get(dbiUserContextMenu.name)?.type}"!`);
|
|
172
185
|
self.data.interactions.set(dbiUserContextMenu.name, dbiUserContextMenu);
|
|
173
186
|
return dbiUserContextMenu;
|
|
174
187
|
};
|
|
175
|
-
UserContextMenu = Object.assign(UserContextMenu, class { constructor(...args) { return UserContextMenu.
|
|
188
|
+
UserContextMenu = Object.assign(UserContextMenu, class { constructor(...args: any[]) { return UserContextMenu.apply(this, args as any); } });
|
|
176
189
|
|
|
177
190
|
let Modal = function(cfg: TDBIModalOmitted) {
|
|
178
191
|
let dbiModal = new DBIModal(self, cfg);
|
|
179
|
-
if (self.data.interactions.has(dbiModal.name)) throw new Error(`DBIModal "${dbiModal.name}" already loaded as "${self.data.interactions.get(dbiModal.name)
|
|
192
|
+
if (self.data.interactions.has(dbiModal.name)) throw new Error(`DBIModal "${dbiModal.name}" already loaded as "${self.data.interactions.get(dbiModal.name)?.type}"!`);
|
|
180
193
|
self.data.interactions.set(dbiModal.name, dbiModal);
|
|
181
194
|
return dbiModal;
|
|
182
195
|
};
|
|
183
|
-
Modal = Object.assign(Modal, class { constructor(...args) { return Modal.
|
|
184
|
-
|
|
196
|
+
Modal = Object.assign(Modal, class { constructor(...args: any[]) { return Modal.apply(this, args as any); } });
|
|
185
197
|
|
|
186
198
|
let Locale = function(cfg: TDBILocaleConstructor) {
|
|
187
199
|
let dbiLocale = new DBILocale(self, cfg);
|
|
@@ -189,7 +201,15 @@ export class DBI {
|
|
|
189
201
|
self.data.locales.set(dbiLocale.name, dbiLocale);
|
|
190
202
|
return dbiLocale;
|
|
191
203
|
};
|
|
192
|
-
Locale = Object.assign(Locale, class { constructor(...args) { return Locale.
|
|
204
|
+
Locale = Object.assign(Locale, class { constructor(...args: any[]) { return Locale.apply(this, args as any); } });
|
|
205
|
+
|
|
206
|
+
let InteractionLocale = function(cfg: TDBIInteractionLocaleOmitted) {
|
|
207
|
+
let dbiInteractionLocale = new DBIInteractionLocale(self, cfg);
|
|
208
|
+
if (self.data.interactionLocales.has(dbiInteractionLocale.name)) throw new Error(`DBIInteractionLocale "${dbiInteractionLocale.name}" already loaded!`);
|
|
209
|
+
self.data.interactionLocales.set(dbiInteractionLocale.name, dbiInteractionLocale);
|
|
210
|
+
return dbiInteractionLocale;
|
|
211
|
+
};
|
|
212
|
+
InteractionLocale = Object.assign(InteractionLocale, class { constructor(...args: any[]) { return InteractionLocale.apply(this, args as any); } });
|
|
193
213
|
|
|
194
214
|
await cb({
|
|
195
215
|
ChatInput,
|
|
@@ -201,13 +221,46 @@ export class DBI {
|
|
|
201
221
|
MessageContextMenu,
|
|
202
222
|
UserContextMenu,
|
|
203
223
|
Modal,
|
|
204
|
-
|
|
224
|
+
InteractionLocale,
|
|
225
|
+
onUnload(cb: ()=> Promise<any> | any) {
|
|
205
226
|
self.data.registerUnloaders.add(cb);
|
|
206
227
|
},
|
|
207
228
|
});
|
|
208
229
|
}
|
|
209
230
|
}
|
|
210
231
|
|
|
232
|
+
/**
|
|
233
|
+
* Shorthands for modifying `dbi.data.other`
|
|
234
|
+
*/
|
|
235
|
+
get<K extends keyof TOtherData>(k: K, defaultValue?: TOtherData[K]): TOtherData[K] {
|
|
236
|
+
if (this.has(k as any)) {
|
|
237
|
+
this.set(k, defaultValue);
|
|
238
|
+
return defaultValue;
|
|
239
|
+
}
|
|
240
|
+
return _.get(this.data.other, k);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Shorthands for modifying `dbi.data.other`
|
|
245
|
+
*/
|
|
246
|
+
set<K extends keyof TOtherData>(k: K, v: TOtherData[K]): any {
|
|
247
|
+
this.data.other = _.set(this.data.other as any, k, v);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Shorthands for modifying `dbi.data.other`
|
|
252
|
+
*/
|
|
253
|
+
has(k: string): boolean {
|
|
254
|
+
return _.has(this.data.other, k as any);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Shorthands for modifying `dbi.data.other`
|
|
259
|
+
*/
|
|
260
|
+
delete(k: string): boolean {
|
|
261
|
+
return _.unset(this.data.other, k);
|
|
262
|
+
}
|
|
263
|
+
|
|
211
264
|
async login(): Promise<any> {
|
|
212
265
|
await this.client.login(this.config.discord.token);
|
|
213
266
|
}
|
|
@@ -237,13 +290,14 @@ export class DBI {
|
|
|
237
290
|
async publish(type: "Global", clear?: boolean): Promise<any>;
|
|
238
291
|
async publish(type: "Guild", guildId: string, clear?: boolean): Promise<any>;
|
|
239
292
|
|
|
240
|
-
async publish(...args) {
|
|
293
|
+
async publish(...args: any[]) {
|
|
241
294
|
let interactions = this.data.interactions.filter(i => i.type == "ChatInput" || i.type == "MessageContextMenu" || i.type == "UserContextMenu") as any;
|
|
242
295
|
switch (args[0]) {
|
|
243
296
|
case "Global": {
|
|
244
297
|
return await publishInteractions(
|
|
245
298
|
this.config.discord.token,
|
|
246
299
|
args[1] ? new Discord.Collection() : interactions,
|
|
300
|
+
this.data.interactionLocales,
|
|
247
301
|
args[0]
|
|
248
302
|
);
|
|
249
303
|
}
|
|
@@ -251,6 +305,7 @@ export class DBI {
|
|
|
251
305
|
return await publishInteractions(
|
|
252
306
|
this.config.discord.token,
|
|
253
307
|
args[2] ? new Discord.Collection() : interactions,
|
|
308
|
+
this.data.interactionLocales,
|
|
254
309
|
args[0],
|
|
255
310
|
args[1]
|
|
256
311
|
);
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DBI,
|
|
1
|
+
import { DBI, TDBIConfigConstructor } from "./DBI";
|
|
2
2
|
export { recursiveImport } from "./utils/recursiveImport";
|
|
3
3
|
export { MemoryStore } from "./utils/MemoryStore";
|
|
4
4
|
|
|
5
|
-
export function createDBI(namespace: string, cfg:
|
|
6
|
-
return new DBI(namespace, cfg);
|
|
5
|
+
export function createDBI<TOtherType = Record<string, any>>(namespace: string, cfg: TDBIConfigConstructor): DBI<TOtherType> {
|
|
6
|
+
return new DBI<TOtherType>(namespace, cfg);
|
|
7
7
|
}
|
|
@@ -25,7 +25,7 @@ export function hookInteractionListeners(dbi: DBI): () => any {
|
|
|
25
25
|
(
|
|
26
26
|
(i.type == "Button" || i.type == "SelectMenu" || i.type == "Modal")
|
|
27
27
|
&& isUsesCustomId
|
|
28
|
-
&& parsedId
|
|
28
|
+
&& parsedId?.name == i.name
|
|
29
29
|
)
|
|
30
30
|
)
|
|
31
31
|
});
|
|
@@ -68,10 +68,11 @@ export function hookInteractionListeners(dbi: DBI): () => any {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
for (const type in rateLimitKeyMap) {
|
|
71
|
-
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
let key = `RateLimit["${rateLimitKeyMap[type]}"]`;
|
|
72
73
|
let val = await dbi.config.store.get(key);
|
|
73
74
|
if (val && Date.now() > val.at + val.duration) {
|
|
74
|
-
await dbi.config.store.
|
|
75
|
+
await dbi.config.store.delete(key);
|
|
75
76
|
val = null;
|
|
76
77
|
}
|
|
77
78
|
if (val) {
|
|
@@ -90,7 +91,8 @@ export function hookInteractionListeners(dbi: DBI): () => any {
|
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
async function setRateLimit(type: string, duration: number) {
|
|
93
|
-
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
await dbi.config.store.set(`RateLimit["${rateLimitKeyMap[type]}"]`, { at: Date.now(), duration });
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
let other = {};
|
|
@@ -101,8 +103,10 @@ export function hookInteractionListeners(dbi: DBI): () => any {
|
|
|
101
103
|
dbi,
|
|
102
104
|
// @ts-ignore
|
|
103
105
|
interaction: inter as any,
|
|
106
|
+
// @ts-ignore
|
|
104
107
|
locale,
|
|
105
108
|
setRateLimit,
|
|
109
|
+
// @ts-ignore
|
|
106
110
|
data,
|
|
107
111
|
other
|
|
108
112
|
});
|
|
@@ -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
|
-
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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
|
}
|
package/src/types/Button.ts
CHANGED
|
@@ -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:
|
|
8
|
+
data: TDBIReferencedData[];
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export type TDBIButtonOmitted = Omit<DBIButton, "type" | "description" | "dbi" | "toJSON">;
|
package/src/types/Interaction.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/types/Locale.ts
CHANGED
|
@@ -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 }
|
|
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 =
|
|
26
|
+
this.data = convertLang(cfg.data);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
function
|
|
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,
|
|
37
|
+
return [key, convertLang(value)];
|
|
37
38
|
}
|
|
38
39
|
}))
|
|
39
40
|
}
|
package/src/types/Modal.ts
CHANGED
|
@@ -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:
|
|
9
|
+
data: TDBIReferencedData[];
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export type TDBIModalOmitted = Omit<DBIModal, "type" | "description" | "dbi" | "toJSON">;
|