@mostfeatured/dbi 0.1.21 → 0.1.22
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/.vscode/settings.json +3 -0
- package/dist/DBI.d.ts +12 -2
- package/dist/DBI.d.ts.map +1 -1
- package/dist/DBI.js +8 -2
- package/dist/DBI.js.map +1 -1
- package/dist/methods/handleMessageCommands.js +2 -2
- package/dist/methods/handleMessageCommands.js.map +1 -1
- package/dist/methods/hookEventListeners.js +1 -1
- package/dist/methods/hookEventListeners.js.map +1 -1
- package/dist/methods/hookInteractionListeners.js +2 -2
- package/dist/methods/hookInteractionListeners.js.map +1 -1
- package/dist/types/other/FakeMessageInteraction.js +2 -2
- package/dist/types/other/FakeMessageInteraction.js.map +1 -1
- package/dist/types/other/Locale.d.ts +4 -2
- package/dist/types/other/Locale.d.ts.map +1 -1
- package/dist/types/other/Locale.js +30 -14
- package/dist/types/other/Locale.js.map +1 -1
- package/package.json +44 -44
- package/readme.md +3 -1
- package/src/DBI.ts +22 -4
- package/src/methods/handleMessageCommands.ts +2 -2
- package/src/methods/hookEventListeners.ts +1 -1
- package/src/methods/hookInteractionListeners.ts +2 -2
- package/src/types/other/FakeMessageInteraction.ts +2 -2
- package/src/types/other/Locale.ts +29 -16
package/src/DBI.ts
CHANGED
|
@@ -86,6 +86,11 @@ export type TDBIMessageCommandsPrefixesCtx = {
|
|
|
86
86
|
message: Discord.Message;
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
+
export type TDBILocaleInvalidPathCtx<TNamespace extends NamespaceEnums> = {
|
|
90
|
+
path: string;
|
|
91
|
+
locale: DBILocale<TNamespace>;
|
|
92
|
+
}
|
|
93
|
+
|
|
89
94
|
export interface DBIConfig<TNamespace extends NamespaceEnums> {
|
|
90
95
|
discord: {
|
|
91
96
|
namespace: string;
|
|
@@ -93,7 +98,10 @@ export interface DBIConfig<TNamespace extends NamespaceEnums> {
|
|
|
93
98
|
options: Discord.ClientOptions;
|
|
94
99
|
}[];
|
|
95
100
|
defaults: {
|
|
96
|
-
locale:
|
|
101
|
+
locale: {
|
|
102
|
+
name: TDBILocaleString;
|
|
103
|
+
invalidPath(ctx: TDBILocaleInvalidPathCtx<TNamespace>): string;
|
|
104
|
+
};
|
|
97
105
|
directMessages: boolean;
|
|
98
106
|
defaultMemberPermissions: Discord.PermissionsString[];
|
|
99
107
|
messageCommands: {
|
|
@@ -143,7 +151,10 @@ export interface DBIConfigConstructor<TNamespace extends NamespaceEnums> {
|
|
|
143
151
|
}[];
|
|
144
152
|
|
|
145
153
|
defaults?: {
|
|
146
|
-
locale?:
|
|
154
|
+
locale?: {
|
|
155
|
+
name?: TDBILocaleString;
|
|
156
|
+
invalidPath?: string | ((ctx: TDBILocaleInvalidPathCtx<TNamespace>) => string);
|
|
157
|
+
};
|
|
147
158
|
directMessages?: boolean;
|
|
148
159
|
defaultMemberPermissions?: Discord.PermissionsString[];
|
|
149
160
|
messageCommands?: {
|
|
@@ -283,18 +294,25 @@ export class DBI<
|
|
|
283
294
|
|
|
284
295
|
config.store = (config.store as any) || new MemoryStore();
|
|
285
296
|
config.defaults = {
|
|
286
|
-
locale:
|
|
297
|
+
locale: (() => {
|
|
298
|
+
const invalidPath = config.defaults?.locale?.invalidPath;
|
|
299
|
+
return {
|
|
300
|
+
...(config.defaults?.locale || {}),
|
|
301
|
+
invalidPath: (typeof invalidPath === "function" ? invalidPath : (ctx: TDBILocaleInvalidPathCtx<TNamespace>) => invalidPath || `Invalid "${ctx.path}" locale path.`) as any
|
|
302
|
+
}
|
|
303
|
+
})(),
|
|
287
304
|
defaultMemberPermissions: [],
|
|
288
305
|
directMessages: false,
|
|
289
306
|
...(config.defaults || {}),
|
|
290
307
|
messageCommands: (() => {
|
|
291
|
-
|
|
308
|
+
const deferReplyContent = config.defaults?.messageCommands?.deferReplyContent
|
|
292
309
|
return {
|
|
293
310
|
...(config.defaults?.messageCommands || {}),
|
|
294
311
|
deferReplyContent: (typeof deferReplyContent === "function" ? deferReplyContent : () => deferReplyContent || "Loading...") as any
|
|
295
312
|
}
|
|
296
313
|
})(),
|
|
297
314
|
};
|
|
315
|
+
|
|
298
316
|
config.sharding = config.sharding ?? "off";
|
|
299
317
|
config.strict = config.strict ?? true;
|
|
300
318
|
config.references = {
|
|
@@ -94,11 +94,11 @@ export async function handleMessageCommands(
|
|
|
94
94
|
const builtLocale = {
|
|
95
95
|
user:
|
|
96
96
|
dbi.data.locales.get(interaction.locale) ||
|
|
97
|
-
dbi.data.locales.get(dbi.config.defaults.locale),
|
|
97
|
+
dbi.data.locales.get(dbi.config.defaults.locale.name),
|
|
98
98
|
guild: message.guild?.preferredLocale
|
|
99
99
|
? dbi.data.locales.get(
|
|
100
100
|
message.guild?.preferredLocale?.split("-")?.at(0)
|
|
101
|
-
) || dbi.data.locales.get(dbi.config.defaults.locale)
|
|
101
|
+
) || dbi.data.locales.get(dbi.config.defaults.locale.name)
|
|
102
102
|
: null,
|
|
103
103
|
};
|
|
104
104
|
|
|
@@ -36,7 +36,7 @@ export function hookEventListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
36
36
|
if (current instanceof Guild) return current?.preferredLocale?.split?.("-")?.[0];
|
|
37
37
|
return all;
|
|
38
38
|
}, null);
|
|
39
|
-
let guildLocale = guildLocaleName ? (dbi.data.locales.has(guildLocaleName) ? dbi.data.locales.get(guildLocaleName) : dbi.data.locales.get(dbi.config.defaults.locale)) : null;
|
|
39
|
+
let guildLocale = guildLocaleName ? (dbi.data.locales.has(guildLocaleName) ? dbi.data.locales.get(guildLocaleName) : dbi.data.locales.get(dbi.config.defaults.locale.name)) : null;
|
|
40
40
|
|
|
41
41
|
let locale = guildLocale ? { guild: guildLocale } : null;
|
|
42
42
|
|
|
@@ -49,7 +49,7 @@ export function hookInteractionListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
49
49
|
let userLocaleName = inter.locale.split("-")[0];
|
|
50
50
|
let userLocale = dbi.data.locales.has(userLocaleName)
|
|
51
51
|
? dbi.data.locales.get(userLocaleName)
|
|
52
|
-
: dbi.data.locales.get(dbi.config.defaults.locale);
|
|
52
|
+
: dbi.data.locales.get(dbi.config.defaults.locale.name);
|
|
53
53
|
|
|
54
54
|
let guildLocaleName = inter.guild
|
|
55
55
|
? inter.guild.preferredLocale.split("-")[0]
|
|
@@ -57,7 +57,7 @@ export function hookInteractionListeners(dbi: DBI<NamespaceEnums>): () => any {
|
|
|
57
57
|
let guildLocale = guildLocaleName
|
|
58
58
|
? dbi.data.locales.has(guildLocaleName)
|
|
59
59
|
? dbi.data.locales.get(guildLocaleName)
|
|
60
|
-
: dbi.data.locales.get(dbi.config.defaults.locale)
|
|
60
|
+
: dbi.data.locales.get(dbi.config.defaults.locale.name)
|
|
61
61
|
: null;
|
|
62
62
|
|
|
63
63
|
let locale = {
|
|
@@ -325,11 +325,11 @@ export class FakeMessageInteraction /* implements ChatInputCommandInteraction */
|
|
|
325
325
|
interaction: this,
|
|
326
326
|
locale: {
|
|
327
327
|
user: this.dbi.data.locales.get(this.locale) ||
|
|
328
|
-
this.dbi.data.locales.get(this.dbi.config.defaults.locale),
|
|
328
|
+
this.dbi.data.locales.get(this.dbi.config.defaults.locale.name),
|
|
329
329
|
guild: this.message.guild?.preferredLocale
|
|
330
330
|
? this.dbi.data.locales.get(
|
|
331
331
|
this.message.guild?.preferredLocale?.split("-")?.at(0)
|
|
332
|
-
) || this.dbi.data.locales.get(this.dbi.config.defaults.locale)
|
|
332
|
+
) || this.dbi.data.locales.get(this.dbi.config.defaults.locale.name)
|
|
333
333
|
: null,
|
|
334
334
|
}
|
|
335
335
|
})));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import stuffs from "stuffs";
|
|
2
2
|
import { NamespaceData, NamespaceEnums } from "../../../generated/namespaceData";
|
|
3
3
|
import { DBI } from "../../DBI";
|
|
4
|
-
|
|
4
|
+
import _ from "lodash";
|
|
5
5
|
|
|
6
6
|
export interface DBILangObject {
|
|
7
7
|
[property: string]: DBILangObject & ((...args: any[]) => string);
|
|
@@ -18,7 +18,7 @@ export type TDBILocaleConstructor<TNamespace extends NamespaceEnums> = Omit<DBIL
|
|
|
18
18
|
export class DBILocale<TNamespace extends NamespaceEnums> {
|
|
19
19
|
name: TDBILocaleString;
|
|
20
20
|
data: NamespaceData[TNamespace]["contentLocale"];
|
|
21
|
-
|
|
21
|
+
_data: DBILangConstructorObject;
|
|
22
22
|
dbi: DBI<TNamespace, {}>;
|
|
23
23
|
flag?: string
|
|
24
24
|
constructor(dbi: DBI<TNamespace, {}>, cfg: TDBILocaleConstructor<TNamespace>) {
|
|
@@ -26,27 +26,40 @@ export class DBILocale<TNamespace extends NamespaceEnums> {
|
|
|
26
26
|
this.name = cfg.name;
|
|
27
27
|
this.flag = cfg.flag;
|
|
28
28
|
this._data = cfg.data;
|
|
29
|
-
this.data =
|
|
29
|
+
this.data = createInfinitePathProxy((path, ...args) => {
|
|
30
|
+
return this.format(path.join("."), ...args);
|
|
31
|
+
});;
|
|
30
32
|
}
|
|
31
33
|
mergeLocale(locale: DBILocale<TNamespace>): DBILocale<TNamespace> {
|
|
32
34
|
this._data = stuffs.defaultify(locale._data, this._data, true) as any;
|
|
33
|
-
this.data = convertLang<TNamespace>(this._data);
|
|
34
|
-
|
|
35
|
-
locale.data = this.data;
|
|
36
35
|
locale._data = this._data;
|
|
37
36
|
|
|
38
37
|
return this;
|
|
39
38
|
}
|
|
39
|
+
get(path: string): string | null {
|
|
40
|
+
return _.get(this._data as any, path) as string || null;
|
|
41
|
+
}
|
|
42
|
+
format(path: string, ...args: any[]): string {
|
|
43
|
+
const value = this.get(path);
|
|
44
|
+
if (!value) {
|
|
45
|
+
const defaultLocale = this.dbi.locale(this.dbi.config.defaults.locale.name);
|
|
46
|
+
if (!defaultLocale) return this.dbi.config.defaults.locale.invalidPath({
|
|
47
|
+
locale: this,
|
|
48
|
+
path,
|
|
49
|
+
});
|
|
50
|
+
return defaultLocale.format(path, ...args);
|
|
51
|
+
}
|
|
52
|
+
return stuffs.mapReplace(value, args.map((t, i) => [new RegExp(`\\{${i}(;[^}]+)?\\}`, "g"), t]));
|
|
53
|
+
}
|
|
40
54
|
}
|
|
41
55
|
|
|
42
|
-
export function
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
return [
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return [key, convertLang(value)];
|
|
56
|
+
export function createInfinitePathProxy(onApplyPath: (path: string[], ...args: any[]) => string, path: string[] = []): any {
|
|
57
|
+
return new Proxy(() => { }, {
|
|
58
|
+
get(target, key) {
|
|
59
|
+
return createInfinitePathProxy(onApplyPath, [...path, key.toString()]);
|
|
60
|
+
},
|
|
61
|
+
apply(target, thisArg, args) {
|
|
62
|
+
return onApplyPath(path, ...args);
|
|
50
63
|
}
|
|
51
|
-
})
|
|
52
|
-
}
|
|
64
|
+
});
|
|
65
|
+
}
|