generated from Mara-Li/ts-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
135 lines (118 loc) · 3.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as Djs from "discord.js";
import { default as i18next } from "i18next";
import {
DiceTypeError,
EmptyObjectError,
FormulaError,
MaxGreater,
NoStatisticsError,
TooManyDice,
TooManyStats,
} from "@dicelette/core";
import { ALL_TRANSLATION_KEYS, logger } from "@main";
import { InvalidCsvContent, NoChannel, NoEmbed } from "@utils";
import { resources } from "./init";
export function ln(userLang: Djs.Locale) {
if (userLang === Djs.Locale.EnglishUS || userLang === Djs.Locale.EnglishGB)
return i18next.getFixedT("en");
const localeName = Object.entries(Djs.Locale).find(([name, abbr]) => {
return name === userLang || abbr === userLang;
});
return i18next.getFixedT(localeName?.[1] ?? "en");
}
export function lError(
e: Error,
interaction?: Djs.BaseInteraction,
userLang?: Djs.Locale
) {
const ul = ln(userLang ?? interaction?.locale ?? Djs.Locale.EnglishUS);
if (e instanceof DiceTypeError)
return ul("error.invalidDice.withDice", { dice: e.dice });
if (e instanceof FormulaError)
return ul("error.invalidFormula", { formula: e.formula });
if (e instanceof MaxGreater)
return ul("error.mustBeGreater", { max: e.max, value: e.value });
if (e instanceof EmptyObjectError) return ul("error.emptyDamage");
if (e instanceof TooManyDice) return ul("error.tooMuchDice");
if (e instanceof NoStatisticsError) return ul("error.emptyStats");
if (e instanceof TooManyStats) return ul("error.tooMuchStats");
if (e instanceof NoEmbed) return ul("error.noEmbed");
if (e instanceof InvalidCsvContent) return ul("error.csvContent", { fichier: e.file });
if (e instanceof NoChannel) return ul("error.channel", { channel: "" });
if (e instanceof Djs.DiscordAPIError) {
if (e.method === "DELETE") {
logger.warn("Error while deleting message", e);
return "";
}
if (e.code === 50001) return ul("error.missingPermission");
return ul("error.discord", { code: e.code, stack: e.stack });
}
if (e.message.includes(":warning:")) return ul("error.generic.e", { e });
return ul("error.generic.withWarning", { e });
}
/**
* Create an object with all the translations for a specific key
* @example
* ```ts
* cmdLn("hello"):
* {
* "en": "Hello",
* "fr": "Bonjour"
* }
* ```
* @param key i18n key
* @returns
*/
export function cmdLn(key: string) {
const localized: Djs.LocalizationMap = {};
const allValidLocale = Object.entries(Djs.Locale);
const allTranslatedLanguages = Object.keys(resources).filter(
(lang) => !lang.includes("en")
);
for (const [name, Locale] of allValidLocale) {
if (allTranslatedLanguages.includes(Locale)) {
const ul = ln(name as Djs.Locale);
localized[Locale as Djs.Locale] = ul(key);
}
}
return localized;
}
interface JsonObject {
//biome-ignore lint/suspicious/noExplicitAny: <explanation>
[key: string]: any;
}
export function flattenJson(
obj: JsonObject,
parentKey = "",
result: JsonObject = {}
): JsonObject {
for (const key in obj) {
// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
if (obj.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
flattenJson(obj[key], newKey, result);
} else {
result[newKey] = obj[key];
}
}
}
return result;
}
/**
* Get the translation key from the translation text
* @example : "Nom du personnage" => ""common.charName"
*/
export function findln(translatedText: string) {
const allLocales = Object.keys(resources);
for (const locale of allLocales) {
const ul = ln(locale as Djs.Locale);
for (const key of ALL_TRANSLATION_KEYS) {
if (ul(key).toLowerCase() === translatedText.toLowerCase()) {
logger.silly(`Key found: ${key}`);
return key;
}
}
}
return translatedText;
}