@mostfeatured/dbi 0.1.13 → 0.1.15
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 +27 -16
- package/dist/DBI.d.ts.map +1 -1
- package/dist/DBI.js +5 -3
- package/dist/DBI.js.map +1 -1
- package/dist/Events.d.ts +27 -2
- package/dist/Events.d.ts.map +1 -1
- package/dist/Events.js +1 -1
- package/dist/Events.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/methods/handleMessageCommands.d.ts.map +1 -1
- package/dist/methods/handleMessageCommands.js +36 -9
- package/dist/methods/handleMessageCommands.js.map +1 -1
- package/dist/methods/hookEventListeners.d.ts.map +1 -1
- package/dist/methods/hookEventListeners.js +17 -9
- package/dist/methods/hookEventListeners.js.map +1 -1
- package/dist/methods/publishInteractions.d.ts +2 -2
- package/dist/methods/publishInteractions.d.ts.map +1 -1
- package/dist/methods/publishInteractions.js.map +1 -1
- package/dist/types/Components/ChannelSelectMenu.d.ts +1 -1
- package/dist/types/Components/ChannelSelectMenu.d.ts.map +1 -1
- package/dist/types/Components/ChannelSelectMenu.js.map +1 -1
- package/dist/types/Components/MentionableSelectMenu.d.ts +1 -1
- package/dist/types/Components/MentionableSelectMenu.d.ts.map +1 -1
- package/dist/types/Components/MentionableSelectMenu.js.map +1 -1
- package/dist/types/Components/Modal.d.ts +1 -1
- package/dist/types/Components/Modal.d.ts.map +1 -1
- package/dist/types/Components/Modal.js.map +1 -1
- package/dist/types/Components/RoleSelectMenu.d.ts +1 -1
- package/dist/types/Components/RoleSelectMenu.d.ts.map +1 -1
- package/dist/types/Components/RoleSelectMenu.js.map +1 -1
- package/dist/types/Components/StringSelectMenu.d.ts +1 -1
- package/dist/types/Components/StringSelectMenu.d.ts.map +1 -1
- package/dist/types/Components/StringSelectMenu.js.map +1 -1
- package/dist/types/Components/UserSelectMenu.d.ts +1 -1
- package/dist/types/Components/UserSelectMenu.d.ts.map +1 -1
- package/dist/types/Components/UserSelectMenu.js.map +1 -1
- package/dist/types/Event.d.ts +8 -3
- package/dist/types/Event.d.ts.map +1 -1
- package/dist/types/Event.js +1 -1
- package/dist/types/Event.js.map +1 -1
- package/dist/types/other/FakeMessageInteraction.d.ts +3 -3
- package/dist/types/other/FakeMessageInteraction.d.ts.map +1 -1
- package/dist/types/other/FakeMessageInteraction.js +12 -1
- package/dist/types/other/FakeMessageInteraction.js.map +1 -1
- package/package.json +1 -1
- package/src/DBI.ts +32 -19
- package/src/Events.ts +30 -3
- package/src/index.ts +1 -1
- package/src/methods/handleMessageCommands.ts +49 -18
- package/src/methods/hookEventListeners.ts +16 -9
- package/src/methods/publishInteractions.ts +10 -10
- package/src/types/Components/ChannelSelectMenu.ts +1 -1
- package/src/types/Components/MentionableSelectMenu.ts +1 -1
- package/src/types/Components/Modal.ts +1 -1
- package/src/types/Components/RoleSelectMenu.ts +1 -1
- package/src/types/Components/StringSelectMenu.ts +1 -1
- package/src/types/Components/UserSelectMenu.ts +1 -1
- package/src/types/Event.ts +10 -4
- package/src/types/other/FakeMessageInteraction.ts +15 -2
package/src/DBI.ts
CHANGED
|
@@ -69,14 +69,24 @@ export interface DBIStore {
|
|
|
69
69
|
has(key: string): Promise<boolean>;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
export type
|
|
72
|
+
export type TDBIClientData<TNamespace extends NamespaceEnums> = {
|
|
73
73
|
namespace: NamespaceData[TNamespace]["clientNamespaces"];
|
|
74
74
|
token: string;
|
|
75
75
|
options: Discord.ClientOptions;
|
|
76
76
|
client: Discord.Client<true>;
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
export
|
|
79
|
+
export type TDBIMessageCommandsActionCtx<TNamespace extends NamespaceEnums> = {
|
|
80
|
+
interaction: FakeMessageInteraction;
|
|
81
|
+
dbiInteraction: DBIChatInput<TNamespace>;
|
|
82
|
+
locale: { guild?: DBILocale<TNamespace>; user: DBILocale<TNamespace> };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type TDBIMessageCommandsPrefixesCtx = {
|
|
86
|
+
message: Discord.Message;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export interface DBIConfig<TNamespace extends NamespaceEnums> {
|
|
80
90
|
discord: {
|
|
81
91
|
namespace: string;
|
|
82
92
|
token: string;
|
|
@@ -87,7 +97,7 @@ export interface DBIConfig {
|
|
|
87
97
|
directMessages: boolean;
|
|
88
98
|
defaultMemberPermissions: Discord.PermissionsString[];
|
|
89
99
|
messageCommands: {
|
|
90
|
-
deferReplyContent(
|
|
100
|
+
deferReplyContent(ctx: TDBIMessageCommandsActionCtx<TNamespace>): MessagePayload | string | Promise<MessagePayload | string>;
|
|
91
101
|
};
|
|
92
102
|
};
|
|
93
103
|
|
|
@@ -113,14 +123,14 @@ export interface DBIConfig {
|
|
|
113
123
|
|
|
114
124
|
strict: boolean;
|
|
115
125
|
messageCommands?: {
|
|
116
|
-
prefixes: string[]
|
|
126
|
+
prefixes(ctx: TDBIMessageCommandsPrefixesCtx): string[] | Promise<string[]>;
|
|
117
127
|
typeAliases: {
|
|
118
128
|
booleans: Record<string, boolean>;
|
|
119
129
|
};
|
|
120
130
|
};
|
|
121
131
|
}
|
|
122
132
|
|
|
123
|
-
export interface DBIConfigConstructor {
|
|
133
|
+
export interface DBIConfigConstructor<TNamespace extends NamespaceEnums> {
|
|
124
134
|
discord:
|
|
125
135
|
| {
|
|
126
136
|
token: string;
|
|
@@ -137,7 +147,7 @@ export interface DBIConfigConstructor {
|
|
|
137
147
|
directMessages?: boolean;
|
|
138
148
|
defaultMemberPermissions?: Discord.PermissionsString[];
|
|
139
149
|
messageCommands?: {
|
|
140
|
-
deferReplyContent?: MessagePayload | string | ((
|
|
150
|
+
deferReplyContent?: MessagePayload | string | ((ctx: TDBIMessageCommandsActionCtx<TNamespace>) => MessagePayload | string | Promise<MessagePayload | string>);
|
|
141
151
|
};
|
|
142
152
|
};
|
|
143
153
|
|
|
@@ -169,7 +179,7 @@ export interface DBIConfigConstructor {
|
|
|
169
179
|
strict?: boolean;
|
|
170
180
|
|
|
171
181
|
messageCommands?: {
|
|
172
|
-
prefixes: string[];
|
|
182
|
+
prefixes: string[] | ((ctx: TDBIMessageCommandsPrefixesCtx) => string[] | Promise<string[]>);
|
|
173
183
|
typeAliases?: {
|
|
174
184
|
/**
|
|
175
185
|
* Example: {"yes": true, "no": false}
|
|
@@ -245,7 +255,7 @@ export class DBI<
|
|
|
245
255
|
TOtherData = Record<string, any>
|
|
246
256
|
> {
|
|
247
257
|
namespace: TNamespace;
|
|
248
|
-
config: DBIConfig
|
|
258
|
+
config: DBIConfig<TNamespace>;
|
|
249
259
|
data: {
|
|
250
260
|
interactions: Discord.Collection<string, TDBIInteractions<TNamespace>>;
|
|
251
261
|
events: Discord.Collection<string, DBIEvent<TNamespace>>;
|
|
@@ -258,14 +268,14 @@ export class DBI<
|
|
|
258
268
|
registers: Set<(...args: any[]) => any>;
|
|
259
269
|
registerUnloaders: Set<(...args: any[]) => any>;
|
|
260
270
|
refs: Map<string, { at: number; value: any; ttl?: number }>;
|
|
261
|
-
clients:
|
|
262
|
-
next(key?: string):
|
|
263
|
-
random():
|
|
264
|
-
random(size: number):
|
|
265
|
-
first():
|
|
271
|
+
clients: TDBIClientData<TNamespace>[] & {
|
|
272
|
+
next(key?: string): TDBIClientData<TNamespace>;
|
|
273
|
+
random(): TDBIClientData<TNamespace>;
|
|
274
|
+
random(size: number): TDBIClientData<TNamespace>[];
|
|
275
|
+
first(): TDBIClientData<TNamespace>;
|
|
266
276
|
get(
|
|
267
277
|
namespace: NamespaceData[TNamespace]["clientNamespaces"]
|
|
268
|
-
):
|
|
278
|
+
): TDBIClientData<TNamespace>;
|
|
269
279
|
indexes: Record<string, number>;
|
|
270
280
|
};
|
|
271
281
|
};
|
|
@@ -273,7 +283,7 @@ export class DBI<
|
|
|
273
283
|
cluster?: Sharding.ClusterClient<Discord.Client>;
|
|
274
284
|
private _loaded: boolean;
|
|
275
285
|
private _hooked: boolean;
|
|
276
|
-
constructor(namespace: TNamespace, config: DBIConfigConstructor) {
|
|
286
|
+
constructor(namespace: TNamespace, config: DBIConfigConstructor<TNamespace>) {
|
|
277
287
|
this.namespace = namespace as any;
|
|
278
288
|
const self = this;
|
|
279
289
|
|
|
@@ -306,11 +316,14 @@ export class DBI<
|
|
|
306
316
|
};
|
|
307
317
|
|
|
308
318
|
if (config.messageCommands) {
|
|
309
|
-
|
|
310
|
-
|
|
319
|
+
const { prefixes, typeAliases } = config.messageCommands;
|
|
320
|
+
|
|
321
|
+
if (Array.isArray(prefixes) && this.config.strict && !prefixes.length)
|
|
322
|
+
throw new Error("No prefixes provided.");
|
|
311
323
|
|
|
312
|
-
|
|
324
|
+
const prefixesFn = typeof prefixes === "function" ? prefixes : () => prefixes;
|
|
313
325
|
|
|
326
|
+
config.messageCommands.prefixes = prefixesFn;
|
|
314
327
|
config.messageCommands.typeAliases = {
|
|
315
328
|
booleans: typeAliases.booleans ?? {
|
|
316
329
|
true: true,
|
|
@@ -955,7 +968,7 @@ export class DBI<
|
|
|
955
968
|
|
|
956
969
|
client<TClientName extends NamespaceData[TNamespace]["clientNamespaces"]>(
|
|
957
970
|
name?: TClientName
|
|
958
|
-
):
|
|
971
|
+
): TDBIClientData<TNamespace> {
|
|
959
972
|
return name ? this.data.clients.get(name) : this.data.clients.first();
|
|
960
973
|
}
|
|
961
974
|
/**
|
package/src/Events.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { NamespaceEnums, NamespaceData } from "../generated/namespaceData";
|
|
2
2
|
import { DBI } from "./DBI";
|
|
3
3
|
import { TDBIMessageCommandArgumentErrorTypes } from "./methods/handleMessageCommands";
|
|
4
|
+
import { DBIChatInput } from "./types/ChatInput/ChatInput";
|
|
4
5
|
import { TDBIValueName } from "./types/ChatInput/ChatInputOptions";
|
|
5
6
|
import { ClientEvents, DBIEvent } from "./types/Event";
|
|
6
7
|
import { IDBIBaseExecuteCtx, TDBIRateLimitTypes } from "./types/Interaction";
|
|
7
8
|
import { FakeMessageInteraction } from "./types/other/FakeMessageInteraction";
|
|
8
9
|
import { DBILocale } from "./types/other/Locale";
|
|
9
|
-
import Discord from "discord.js";
|
|
10
|
+
import Discord, { PermissionsString } from "discord.js";
|
|
10
11
|
|
|
11
12
|
export type TDBIEventNames =
|
|
12
13
|
| "beforeInteraction"
|
|
@@ -16,7 +17,9 @@ export type TDBIEventNames =
|
|
|
16
17
|
| "afterEvent"
|
|
17
18
|
| "interactionError"
|
|
18
19
|
| "eventError"
|
|
19
|
-
| "messageCommandArgumentError"
|
|
20
|
+
| "messageCommandArgumentError"
|
|
21
|
+
| "messageCommandDirectMessageUsageError"
|
|
22
|
+
| "messageCommandDefaultMemberPermissionsError";
|
|
20
23
|
|
|
21
24
|
export type TDBIEventHandlerCtx<TNamespace extends NamespaceEnums> = {
|
|
22
25
|
[K in keyof (ClientEvents & NamespaceData[TNamespace]["customEvents"])]: {
|
|
@@ -46,7 +49,7 @@ export class Events<TNamespace extends NamespaceEnums> {
|
|
|
46
49
|
|
|
47
50
|
async trigger(name: TDBIEventNames, data: any): Promise<boolean> {
|
|
48
51
|
let handlers = this.handlers[name];
|
|
49
|
-
if (!handlers) return true;
|
|
52
|
+
if (!handlers?.length) return true;
|
|
50
53
|
for (let i = 0; i < handlers.length; i++) {
|
|
51
54
|
const handler = handlers[i];
|
|
52
55
|
let returned = await handler(data);
|
|
@@ -113,6 +116,30 @@ export class Events<TNamespace extends NamespaceEnums> {
|
|
|
113
116
|
};
|
|
114
117
|
value: any;
|
|
115
118
|
locale: { guild?: DBILocale<TNamespace>; user: DBILocale<TNamespace> };
|
|
119
|
+
dbiInteraction: DBIChatInput<TNamespace>;
|
|
120
|
+
}) => Promise<boolean> | boolean,
|
|
121
|
+
options?: { once: boolean }
|
|
122
|
+
): () => any;
|
|
123
|
+
|
|
124
|
+
on(
|
|
125
|
+
eventName: "messageCommandDefaultMemberPermissionsError",
|
|
126
|
+
handler: (data: {
|
|
127
|
+
message: Discord.Message;
|
|
128
|
+
interaction: FakeMessageInteraction;
|
|
129
|
+
locale: { guild?: DBILocale<TNamespace>; user: DBILocale<TNamespace> };
|
|
130
|
+
permissions: PermissionsString[];
|
|
131
|
+
dbiInteraction: DBIChatInput<TNamespace>;
|
|
132
|
+
}) => Promise<boolean> | boolean,
|
|
133
|
+
options?: { once: boolean }
|
|
134
|
+
): () => any;
|
|
135
|
+
|
|
136
|
+
on(
|
|
137
|
+
eventName: "messageCommandDirectMessageUsageError",
|
|
138
|
+
handler: (data: {
|
|
139
|
+
message: Discord.Message;
|
|
140
|
+
interaction: FakeMessageInteraction;
|
|
141
|
+
locale: { guild?: DBILocale<TNamespace>; user: DBILocale<TNamespace> };
|
|
142
|
+
dbiInteraction: DBIChatInput<TNamespace>;
|
|
116
143
|
}) => Promise<boolean> | boolean,
|
|
117
144
|
options?: { once: boolean }
|
|
118
145
|
): () => any;
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { recursiveUnload } from "./utils/recursiveUnload";
|
|
|
10
10
|
|
|
11
11
|
export const generatedPath = path.resolve(__dirname, "../generated");
|
|
12
12
|
|
|
13
|
-
export function createDBI<TNamespace extends NamespaceEnums, TOtherType = Record<string, any>>(namespace: TNamespace, cfg: DBIConfigConstructor): DBI<TNamespace, TOtherType> {
|
|
13
|
+
export function createDBI<TNamespace extends NamespaceEnums, TOtherType = Record<string, any>>(namespace: TNamespace, cfg: DBIConfigConstructor<TNamespace>): DBI<TNamespace, TOtherType> {
|
|
14
14
|
return new DBI<TNamespace, TOtherType>(namespace, cfg);
|
|
15
15
|
};
|
|
16
16
|
|
|
@@ -38,8 +38,8 @@ export async function handleMessageCommands(
|
|
|
38
38
|
const chatInputs = dbi.data.interactions.filter(
|
|
39
39
|
(i) => i.type === "ChatInput"
|
|
40
40
|
);
|
|
41
|
-
const prefixes = dbi.config.messageCommands.prefixes
|
|
42
|
-
if (!prefixes
|
|
41
|
+
const prefixes = await dbi.config.messageCommands.prefixes({ message });
|
|
42
|
+
if (!prefixes?.length) return;
|
|
43
43
|
const content = message.content;
|
|
44
44
|
const usedPrefix = prefixes.find((p) => content.startsWith(p));
|
|
45
45
|
if (!usedPrefix) return;
|
|
@@ -91,6 +91,45 @@ export async function handleMessageCommands(
|
|
|
91
91
|
usedPrefix
|
|
92
92
|
);
|
|
93
93
|
|
|
94
|
+
const builtLocale = {
|
|
95
|
+
user:
|
|
96
|
+
dbi.data.locales.get(interaction.locale) ||
|
|
97
|
+
dbi.data.locales.get(dbi.config.defaults.locale),
|
|
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)
|
|
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
|
+
|
|
94
133
|
if (chatInput.options.length) {
|
|
95
134
|
let errorType: TDBIMessageCommandArgumentErrorTypes;
|
|
96
135
|
let lastOption: any;
|
|
@@ -354,7 +393,7 @@ export async function handleMessageCommands(
|
|
|
354
393
|
case ApplicationCommandOptionType.User: {
|
|
355
394
|
await message.client.users
|
|
356
395
|
.fetch(interaction.options.getUserId(option.name))
|
|
357
|
-
.catch(() => {});
|
|
396
|
+
.catch(() => { });
|
|
358
397
|
if (option.required && !interaction.options.getUser(option.name)) {
|
|
359
398
|
errorType = "InvalidUser";
|
|
360
399
|
break;
|
|
@@ -364,7 +403,7 @@ export async function handleMessageCommands(
|
|
|
364
403
|
case ApplicationCommandOptionType.Channel: {
|
|
365
404
|
await message.client.channels
|
|
366
405
|
.fetch(interaction.options.getChannelId(option.name))
|
|
367
|
-
.catch(() => {});
|
|
406
|
+
.catch(() => { });
|
|
368
407
|
if (
|
|
369
408
|
option.required &&
|
|
370
409
|
!interaction.options.getChannel(
|
|
@@ -381,7 +420,7 @@ export async function handleMessageCommands(
|
|
|
381
420
|
case ApplicationCommandOptionType.Role: {
|
|
382
421
|
await message.guild.roles
|
|
383
422
|
.fetch(interaction.options.getRoleId(option.name))
|
|
384
|
-
.catch(() => {});
|
|
423
|
+
.catch(() => { });
|
|
385
424
|
if (option.required && !interaction.options.getRole(option.name)) {
|
|
386
425
|
errorType = "InvalidRole";
|
|
387
426
|
break;
|
|
@@ -390,9 +429,9 @@ export async function handleMessageCommands(
|
|
|
390
429
|
}
|
|
391
430
|
case ApplicationCommandOptionType.Mentionable: {
|
|
392
431
|
let mentionableId = interaction.options.getMentionableId(option.name);
|
|
393
|
-
await message.guild.roles.fetch(mentionableId).catch(() => {});
|
|
394
|
-
await message.client.channels.fetch(mentionableId).catch(() => {});
|
|
395
|
-
await message.client.users.fetch(mentionableId).catch(() => {});
|
|
432
|
+
await message.guild.roles.fetch(mentionableId).catch(() => { });
|
|
433
|
+
await message.client.channels.fetch(mentionableId).catch(() => { });
|
|
434
|
+
await message.client.users.fetch(mentionableId).catch(() => { });
|
|
396
435
|
if (
|
|
397
436
|
option.required &&
|
|
398
437
|
!interaction.options.getMentionable(option.name)
|
|
@@ -424,16 +463,7 @@ export async function handleMessageCommands(
|
|
|
424
463
|
let res = await dbi.events.trigger("messageCommandArgumentError", {
|
|
425
464
|
interaction,
|
|
426
465
|
message,
|
|
427
|
-
locale:
|
|
428
|
-
user:
|
|
429
|
-
dbi.data.locales.get(interaction.locale) ||
|
|
430
|
-
dbi.data.locales.get(dbi.config.defaults.locale),
|
|
431
|
-
guild: message.guild?.preferredLocale
|
|
432
|
-
? dbi.data.locales.get(
|
|
433
|
-
message.guild?.preferredLocale?.split("-")?.at(0)
|
|
434
|
-
) || dbi.data.locales.get(dbi.config.defaults.locale)
|
|
435
|
-
: null,
|
|
436
|
-
},
|
|
466
|
+
locale: builtLocale,
|
|
437
467
|
error: {
|
|
438
468
|
type: errorType,
|
|
439
469
|
option: lastOption,
|
|
@@ -441,6 +471,7 @@ export async function handleMessageCommands(
|
|
|
441
471
|
index: lastIndex,
|
|
442
472
|
},
|
|
443
473
|
value: lastValue,
|
|
474
|
+
dbiInteraction: chatInput,
|
|
444
475
|
});
|
|
445
476
|
if (!res) return;
|
|
446
477
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Guild } from "discord.js";
|
|
2
2
|
import { NamespaceEnums } from "../../generated/namespaceData";
|
|
3
3
|
import { DBI } from "../DBI";
|
|
4
|
+
import { TDBIEventOrder } from "../types/Event";
|
|
4
5
|
|
|
5
6
|
export function hookEventListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
6
7
|
|
|
@@ -40,26 +41,28 @@ export function hookEventListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
40
41
|
let locale = guildLocale ? { guild: guildLocale } : null;
|
|
41
42
|
|
|
42
43
|
|
|
43
|
-
let
|
|
44
|
-
let
|
|
44
|
+
let awaitedEvents = [];
|
|
45
|
+
let unAwaitedEvents = [];
|
|
45
46
|
for (let i = 0; i < dbi.data.events.size; i++) {
|
|
46
47
|
const value = dbi.data.events.at(i);
|
|
47
48
|
if (value.name == eventName) {
|
|
48
|
-
if (value.ordered) {
|
|
49
|
-
|
|
49
|
+
if (value.ordered?.await) {
|
|
50
|
+
awaitedEvents.push(value);
|
|
50
51
|
} else {
|
|
51
|
-
|
|
52
|
+
unAwaitedEvents.push(value);
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
let arg = { eventName, ...ctxArgs, other, locale };
|
|
57
58
|
|
|
58
|
-
for (let i = 0; i <
|
|
59
|
-
const value =
|
|
59
|
+
for (let i = 0; i < unAwaitedEvents.length; i++) {
|
|
60
|
+
const value = unAwaitedEvents[i];
|
|
60
61
|
|
|
61
62
|
if (value?.disabled) continue;
|
|
62
63
|
|
|
64
|
+
if (value.ordered?.delayBefore) await new Promise(resolve => setTimeout(resolve, value.ordered.delayBefore));
|
|
65
|
+
|
|
63
66
|
if (!(await dbi.events.trigger("beforeEvent", { ...arg, dbiEvent: value }))) continue;
|
|
64
67
|
|
|
65
68
|
if (dbi.config.strict) {
|
|
@@ -73,15 +76,18 @@ export function hookEventListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
73
76
|
dbi.events.trigger("eventError", { ...arg, error, dbiEvent: value });
|
|
74
77
|
}
|
|
75
78
|
}
|
|
79
|
+
if (value.ordered?.delayAfter) await new Promise(resolve => setTimeout(resolve, value.ordered.delayAfter));
|
|
76
80
|
}
|
|
77
81
|
|
|
78
|
-
for (let i = 0; i <
|
|
79
|
-
const value =
|
|
82
|
+
for (let i = 0; i < awaitedEvents.length; i++) {
|
|
83
|
+
const value = awaitedEvents[i];
|
|
80
84
|
|
|
81
85
|
if (value?.disabled) continue;
|
|
82
86
|
|
|
83
87
|
if (!(await dbi.events.trigger("beforeEvent", { ...arg, dbiEvent: value }))) continue;
|
|
84
88
|
|
|
89
|
+
if (value.ordered?.delayBefore) await new Promise(resolve => setTimeout(resolve, value.ordered.delayBefore));
|
|
90
|
+
|
|
85
91
|
if (dbi.config.strict) {
|
|
86
92
|
await value.onExecute({ ...arg, nextClient: getClientByEvent(value) });
|
|
87
93
|
} else {
|
|
@@ -93,6 +99,7 @@ export function hookEventListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
93
99
|
await dbi.events.trigger("eventError", { ...arg, error, dbiEvent: value });
|
|
94
100
|
}
|
|
95
101
|
}
|
|
102
|
+
if (value.ordered?.delayAfter) await new Promise(resolve => setTimeout(resolve, value.ordered.delayAfter));
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
dbi.events.trigger("afterEvent", arg)
|
|
@@ -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,
|
|
7
|
+
import { DBI, TDBIClientData } from "../DBI";
|
|
8
8
|
import { DBIInteractionLocale } from "../types/other/InteractionLocale";
|
|
9
9
|
import { NamespaceEnums } from "../../generated/namespaceData";
|
|
10
10
|
import { ApplicationRoleConnectionMetadataType, ApplicationRoleConnectionMetadata } from "../types/ApplicationRoleConnectionMetadata";
|
|
@@ -13,7 +13,7 @@ const PUBLISHABLE_TYPES = ["ChatInput", "UserContextMenu", "MessageContextMenu"]
|
|
|
13
13
|
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"];
|
|
14
14
|
|
|
15
15
|
export async function publishInteractions(
|
|
16
|
-
clients:
|
|
16
|
+
clients: TDBIClientData<NamespaceEnums>[],
|
|
17
17
|
interactions: Discord.Collection<string, DBIChatInput<NamespaceEnums>>,
|
|
18
18
|
interactionsLocales: Discord.Collection<string, DBIInteractionLocale>,
|
|
19
19
|
publishType: "Guild" | "Global",
|
|
@@ -22,7 +22,7 @@ export async function publishInteractions(
|
|
|
22
22
|
interactions = interactions.filter(i => PUBLISHABLE_TYPES.includes(i.type));
|
|
23
23
|
interactions = interactions.sort((a, b) => b.name.split(" ").length - a.name.split(" ").length);
|
|
24
24
|
|
|
25
|
-
let body: {[k: string]: RESTPutAPIApplicationCommandsJSONBody} =
|
|
25
|
+
let body: { [k: string]: RESTPutAPIApplicationCommandsJSONBody } =
|
|
26
26
|
interactions.reduce((all, current) => {
|
|
27
27
|
if (current.publish && !all[current.publish]) all[current.publish] = [];
|
|
28
28
|
switch (current.type) {
|
|
@@ -165,16 +165,16 @@ export async function publishInteractions(
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
return all;
|
|
168
|
-
}, {} as {[k: string]: any});
|
|
169
|
-
|
|
168
|
+
}, {} as { [k: string]: any });
|
|
169
|
+
|
|
170
170
|
|
|
171
171
|
for (let i = 0; i < clients.length; i++) {
|
|
172
172
|
const client = clients[i];
|
|
173
173
|
const rest = new REST({ version: "10" });
|
|
174
174
|
rest.setToken(client.token);
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
const me: RESTGetAPIUserResult = await rest.get(Routes.user()) as any;
|
|
177
|
-
|
|
177
|
+
|
|
178
178
|
switch (publishType) {
|
|
179
179
|
case "Guild": {
|
|
180
180
|
await rest.put(Routes.applicationGuildCommands(me.id, guildId), { body: body[client.namespace] || [] });
|
|
@@ -186,7 +186,7 @@ export async function publishInteractions(
|
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
|
-
|
|
189
|
+
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
export function localeifyOptions(options: any[], localeData: any): any[] {
|
|
@@ -237,7 +237,7 @@ export function formatLocale(locale: DBIInteractionLocale): any {
|
|
|
237
237
|
if (!optionLocale.choiceLocales) optionLocale.choiceLocales = {};
|
|
238
238
|
if (!optionLocale.choiceLocales[choiceOriginalName]) optionLocale.choiceLocales[choiceOriginalName] = {};
|
|
239
239
|
let choiceLocale = optionLocale.choiceLocales[choiceOriginalName];
|
|
240
|
-
|
|
240
|
+
|
|
241
241
|
choiceLocale[longLocale] = choiceName;
|
|
242
242
|
});
|
|
243
243
|
|
|
@@ -246,7 +246,7 @@ export function formatLocale(locale: DBIInteractionLocale): any {
|
|
|
246
246
|
})
|
|
247
247
|
});
|
|
248
248
|
});
|
|
249
|
-
|
|
249
|
+
|
|
250
250
|
return {
|
|
251
251
|
nameLocales,
|
|
252
252
|
allNameLocales,
|
|
@@ -24,7 +24,7 @@ export class DBIChannelSelectMenu<TNamespace extends NamespaceEnums> extends DBI
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare options
|
|
27
|
+
declare options?: SelectMenuDefaultOptions;
|
|
28
28
|
|
|
29
29
|
override onExecute(ctx: IDBIChannelSelectMenuExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
30
30
|
|
|
@@ -24,7 +24,7 @@ export class DBIMentionableSelectMenu<TNamespace extends NamespaceEnums> extends
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare options
|
|
27
|
+
declare options?: SelectMenuDefaultOptions;
|
|
28
28
|
|
|
29
29
|
override onExecute(ctx: IDBIMentionableSelectMenuExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
30
30
|
|
|
@@ -28,7 +28,7 @@ export class DBIModal<TNamespace extends NamespaceEnums> extends DBIBaseInteract
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
declare options
|
|
31
|
+
declare options?: ModalComponentData;
|
|
32
32
|
|
|
33
33
|
override onExecute(ctx: IDBIModalExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
34
34
|
|
|
@@ -24,7 +24,7 @@ export class DBIRoleSelectMenu<TNamespace extends NamespaceEnums> extends DBIBas
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare options
|
|
27
|
+
declare options?: SelectMenuDefaultOptions;
|
|
28
28
|
|
|
29
29
|
override onExecute(ctx: IDBIRoleSelectMenuExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
30
30
|
|
|
@@ -24,7 +24,7 @@ export class DBIStringSelectMenu<TNamespace extends NamespaceEnums> extends DBIB
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare options
|
|
27
|
+
declare options?: SelectMenuDefaultOptions;
|
|
28
28
|
|
|
29
29
|
override onExecute(ctx: IDBIStringSelectMenuExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
30
30
|
|
|
@@ -24,7 +24,7 @@ export class DBIUserSelectMenu<TNamespace extends NamespaceEnums> extends DBIBas
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare options
|
|
27
|
+
declare options?: SelectMenuDefaultOptions;
|
|
28
28
|
|
|
29
29
|
override onExecute(ctx: IDBIUserSelectMenuExecuteCtx<TNamespace>): Promise<void> | void { };
|
|
30
30
|
|
package/src/types/Event.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Discord from "discord.js";
|
|
2
2
|
import { NamespaceEnums, NamespaceData } from "../../generated/namespaceData";
|
|
3
|
-
import { DBI,
|
|
3
|
+
import { DBI, TDBIClientData } from "../DBI";
|
|
4
4
|
import { DBILocale } from "./other/Locale";
|
|
5
5
|
export interface ClientEvents {
|
|
6
6
|
applicationCommandPermissionsUpdate: { data: Discord.ApplicationCommandPermissionsUpdateData };
|
|
@@ -96,12 +96,18 @@ export interface ClientEvents {
|
|
|
96
96
|
export type DBIEventCombinations<TNamespace extends NamespaceEnums> = {
|
|
97
97
|
[K in keyof (ClientEvents & NamespaceData[TNamespace]["customEvents"])]: {
|
|
98
98
|
name: K,
|
|
99
|
-
onExecute: (ctx: (ClientEvents & NamespaceData[TNamespace]["customEvents"])[K] & { other: Record<string, any>, locale?: { guild: DBILocale<TNamespace> }, eventName: string, nextClient:
|
|
99
|
+
onExecute: (ctx: (ClientEvents & NamespaceData[TNamespace]["customEvents"])[K] & { other: Record<string, any>, locale?: { guild: DBILocale<TNamespace> }, eventName: string, nextClient: TDBIClientData<TNamespace> }) => Promise<any> | any
|
|
100
100
|
}
|
|
101
101
|
}[keyof (ClientEvents) | keyof NamespaceData[TNamespace]["customEvents"]];
|
|
102
102
|
|
|
103
103
|
export type TDBIEventOmitted<TNamespace extends NamespaceEnums> = Omit<DBIEvent<TNamespace>, "type" | "name" | "onExecute" | "client" | "dbi" | "toggle" | "disabled" | "at"> & { disabled?: boolean } & DBIEventCombinations<TNamespace>;
|
|
104
104
|
|
|
105
|
+
export type TDBIEventOrder = {
|
|
106
|
+
await?: boolean;
|
|
107
|
+
delayBefore?: number;
|
|
108
|
+
delayAfter?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
105
111
|
export class DBIEvent<TNamespace extends NamespaceEnums> {
|
|
106
112
|
readonly type: "Event";
|
|
107
113
|
other?: Record<string, any>;
|
|
@@ -109,7 +115,7 @@ export class DBIEvent<TNamespace extends NamespaceEnums> {
|
|
|
109
115
|
id?: string;
|
|
110
116
|
name: string;
|
|
111
117
|
onExecute: (...args: any[]) => any;
|
|
112
|
-
ordered?:
|
|
118
|
+
ordered?: TDBIEventOrder;
|
|
113
119
|
dbi: DBI<TNamespace>;
|
|
114
120
|
disabled: boolean = false;
|
|
115
121
|
flag?: string;
|
|
@@ -122,7 +128,7 @@ export class DBIEvent<TNamespace extends NamespaceEnums> {
|
|
|
122
128
|
this.other = cfg.other;
|
|
123
129
|
this.name = cfg.name as any;
|
|
124
130
|
this.onExecute = cfg.onExecute;
|
|
125
|
-
this.ordered = cfg.ordered
|
|
131
|
+
this.ordered = cfg.ordered;
|
|
126
132
|
this.triggerType = cfg.triggerType ?? "OneByOneGlobal";
|
|
127
133
|
this.disabled ??= cfg.disabled;
|
|
128
134
|
this.flag = cfg.flag;
|
|
@@ -43,7 +43,7 @@ export class FakeMessageInteraction /* implements ChatInputCommandInteraction */
|
|
|
43
43
|
_initialized: boolean = false;
|
|
44
44
|
_lastAction: string | undefined;
|
|
45
45
|
|
|
46
|
-
constructor(
|
|
46
|
+
constructor(public dbi: DBI<NamespaceEnums>, public message: Message, chatInput: TDBIInteractions<NamespaceEnums>, public locale: string, commandName: string, public usedPrefix: string) {
|
|
47
47
|
const self = this;
|
|
48
48
|
|
|
49
49
|
this.channelId = message.channel.id;
|
|
@@ -319,7 +319,20 @@ export class FakeMessageInteraction /* implements ChatInputCommandInteraction */
|
|
|
319
319
|
|
|
320
320
|
async deferReply(options: any): Promise<any> {
|
|
321
321
|
if (this.repliedMessage) throw new Error("Already deferred reply.");
|
|
322
|
-
this.repliedMessage = await this.message.reply(options?.content ?? (await this.dbi.config.defaults.messageCommands.deferReplyContent(
|
|
322
|
+
this.repliedMessage = await this.message.reply(options?.content ?? (await this.dbi.config.defaults.messageCommands.deferReplyContent({
|
|
323
|
+
// @ts-ignore
|
|
324
|
+
dbiInteraction: this.dbiChatInput,
|
|
325
|
+
interaction: this,
|
|
326
|
+
locale: {
|
|
327
|
+
user: this.dbi.data.locales.get(this.locale) ||
|
|
328
|
+
this.dbi.data.locales.get(this.dbi.config.defaults.locale),
|
|
329
|
+
guild: this.message.guild?.preferredLocale
|
|
330
|
+
? this.dbi.data.locales.get(
|
|
331
|
+
this.message.guild?.preferredLocale?.split("-")?.at(0)
|
|
332
|
+
) || this.dbi.data.locales.get(this.dbi.config.defaults.locale)
|
|
333
|
+
: null,
|
|
334
|
+
}
|
|
335
|
+
})));
|
|
323
336
|
this.deferred = true;
|
|
324
337
|
this._lastAction = "deferReply";
|
|
325
338
|
return this.repliedMessage;
|