@open-discord-bots/framework 0.0.1 → 0.0.2
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/LICENSE.md +713 -0
- package/README.md +104 -0
- package/dist/api/api.d.ts +26 -0
- package/dist/api/api.js +44 -0
- package/dist/api/main.d.ts +133 -0
- package/dist/api/main.js +87 -0
- package/dist/api/modules/action.d.ts +34 -0
- package/dist/api/modules/action.js +58 -0
- package/dist/api/modules/base.d.ts +329 -0
- package/dist/api/modules/base.js +804 -0
- package/dist/api/modules/builder.d.ts +647 -0
- package/dist/api/modules/builder.js +1441 -0
- package/dist/api/modules/checker.d.ts +648 -0
- package/dist/api/modules/checker.js +1324 -0
- package/dist/api/modules/client.d.ts +768 -0
- package/dist/api/modules/client.js +1859 -0
- package/dist/api/modules/code.d.ts +33 -0
- package/dist/api/modules/code.js +57 -0
- package/dist/api/modules/config.d.ts +70 -0
- package/dist/api/modules/config.js +206 -0
- package/dist/api/modules/console.d.ts +305 -0
- package/dist/api/modules/console.js +598 -0
- package/dist/api/modules/cooldown.d.ts +138 -0
- package/dist/api/modules/cooldown.js +359 -0
- package/dist/api/modules/database.d.ts +135 -0
- package/dist/api/modules/database.js +271 -0
- package/dist/api/modules/event.d.ts +43 -0
- package/dist/api/modules/event.js +100 -0
- package/dist/api/modules/flag.d.ts +40 -0
- package/dist/api/modules/flag.js +72 -0
- package/dist/api/modules/fuse.d.ts +218 -0
- package/dist/api/modules/fuse.js +123 -0
- package/dist/api/modules/helpmenu.d.ts +106 -0
- package/dist/api/modules/helpmenu.js +167 -0
- package/dist/api/modules/language.d.ts +85 -0
- package/dist/api/modules/language.js +195 -0
- package/dist/api/modules/permission.d.ts +121 -0
- package/dist/api/modules/permission.js +314 -0
- package/dist/api/modules/plugin.d.ts +128 -0
- package/dist/api/modules/plugin.js +168 -0
- package/dist/api/modules/post.d.ts +44 -0
- package/dist/api/modules/post.js +92 -0
- package/dist/api/modules/progressbar.d.ts +108 -0
- package/dist/api/modules/progressbar.js +233 -0
- package/dist/api/modules/responder.d.ts +506 -0
- package/dist/api/modules/responder.js +1468 -0
- package/dist/api/modules/session.d.ts +58 -0
- package/dist/api/modules/session.js +171 -0
- package/dist/api/modules/startscreen.d.ts +165 -0
- package/dist/api/modules/startscreen.js +293 -0
- package/dist/api/modules/stat.d.ts +142 -0
- package/dist/api/modules/stat.js +293 -0
- package/dist/api/modules/verifybar.d.ts +54 -0
- package/dist/api/modules/verifybar.js +60 -0
- package/dist/api/modules/worker.d.ts +41 -0
- package/dist/api/modules/worker.js +93 -0
- package/dist/api/utils.d.ts +61 -0
- package/dist/api/utils.js +254 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +40 -0
- package/dist/startup/dump.d.ts +14 -0
- package/dist/startup/dump.js +79 -0
- package/dist/startup/errorHandling.d.ts +2 -0
- package/dist/startup/errorHandling.js +43 -0
- package/dist/startup/pluginLauncher.d.ts +2 -0
- package/dist/startup/pluginLauncher.js +202 -0
- package/package.json +9 -3
- package/src/api/api.ts +29 -0
- package/src/api/main.ts +189 -0
- package/src/api/modules/action.ts +58 -0
- package/src/api/modules/base.ts +811 -0
- package/src/api/modules/builder.ts +1554 -0
- package/src/api/modules/checker.ts +1549 -0
- package/src/api/modules/client.ts +2247 -0
- package/src/api/modules/code.ts +58 -0
- package/src/api/modules/config.ts +159 -0
- package/src/api/modules/console.ts +665 -0
- package/src/api/modules/cooldown.ts +348 -0
- package/src/api/modules/database.ts +278 -0
- package/src/api/modules/event.ts +99 -0
- package/src/api/modules/flag.ts +73 -0
- package/src/api/modules/fuse.ts +348 -0
- package/src/api/modules/helpmenu.ts +216 -0
- package/src/api/modules/language.ts +201 -0
- package/src/api/modules/permission.ts +340 -0
- package/src/api/modules/plugin.ts +242 -0
- package/src/api/modules/post.ts +90 -0
- package/src/api/modules/progressbar.ts +232 -0
- package/src/api/modules/responder.ts +1420 -0
- package/src/api/modules/session.ts +155 -0
- package/src/api/modules/startscreen.ts +320 -0
- package/src/api/modules/stat.ts +313 -0
- package/src/api/modules/verifybar.ts +61 -0
- package/src/api/modules/worker.ts +93 -0
- package/src/api/utils.ts +206 -0
- package/src/cli/cli.ts +151 -0
- package/src/cli/editConfig.ts +943 -0
- package/src/index.ts +6 -1
- package/src/startup/compilation.ts +186 -0
- package/src/startup/dump.ts +45 -0
- package/src/startup/errorHandling.ts +38 -0
- package/src/startup/pluginLauncher.ts +261 -0
- package/LICENSE +0 -21
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
///////////////////////////////////////
|
|
2
|
+
//HELP MODULE
|
|
3
|
+
///////////////////////////////////////
|
|
4
|
+
import { ODId, ODManager, ODManagerData, ODValidId } from "./base"
|
|
5
|
+
import { ODDebugger } from "./console"
|
|
6
|
+
|
|
7
|
+
/**## ODHelpMenuComponentRenderer `type`
|
|
8
|
+
* This is the callback of the help menu component renderer. It also contains information about how & where it is rendered.
|
|
9
|
+
*/
|
|
10
|
+
export type ODHelpMenuComponentRenderer = (page:number, category:number, location:number, mode:"slash"|"text") => string|Promise<string>
|
|
11
|
+
|
|
12
|
+
/**## ODHelpMenuComponent `class`
|
|
13
|
+
* This is an Open Discord help menu component.
|
|
14
|
+
*
|
|
15
|
+
* It can render something on the Open Discord help menu.
|
|
16
|
+
*/
|
|
17
|
+
export class ODHelpMenuComponent extends ODManagerData {
|
|
18
|
+
/**The priority of this component. The higher, the earlier it will appear in the help menu. */
|
|
19
|
+
priority: number
|
|
20
|
+
/**The render function for this component. */
|
|
21
|
+
render: ODHelpMenuComponentRenderer
|
|
22
|
+
|
|
23
|
+
constructor(id:ODValidId, priority:number, render:ODHelpMenuComponentRenderer){
|
|
24
|
+
super(id)
|
|
25
|
+
this.priority = priority
|
|
26
|
+
this.render = render
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**## ODHelpMenuTextComponent `class`
|
|
31
|
+
* This is an Open Discord help menu text component.
|
|
32
|
+
*
|
|
33
|
+
* It can render a static piece of text on the Open Discord help menu.
|
|
34
|
+
*/
|
|
35
|
+
export class ODHelpMenuTextComponent extends ODHelpMenuComponent {
|
|
36
|
+
constructor(id:ODValidId, priority:number, text:string){
|
|
37
|
+
super(id,priority,() => {
|
|
38
|
+
return text
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**## ODHelpMenuCommandComponentOption `interface`
|
|
44
|
+
* This interface contains a command option for the `ODHelpMenuCommandComponent`.
|
|
45
|
+
*/
|
|
46
|
+
export interface ODHelpMenuCommandComponentOption {
|
|
47
|
+
/**The name of this option. */
|
|
48
|
+
name:string,
|
|
49
|
+
/**Is this option optional? */
|
|
50
|
+
optional:boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**## ODHelpMenuCommandComponentSettings `interface`
|
|
54
|
+
* This interface contains the settings for the `ODHelpMenuCommandComponent`.
|
|
55
|
+
*/
|
|
56
|
+
export interface ODHelpMenuCommandComponentSettings {
|
|
57
|
+
/**The name of this text command. */
|
|
58
|
+
textName?:string,
|
|
59
|
+
/**The name of this slash command. */
|
|
60
|
+
slashName?:string,
|
|
61
|
+
/**Options available in the text command. */
|
|
62
|
+
textOptions?:ODHelpMenuCommandComponentOption[],
|
|
63
|
+
/**Options available in the slash command. */
|
|
64
|
+
slashOptions?:ODHelpMenuCommandComponentOption[],
|
|
65
|
+
/**The description for the text command. */
|
|
66
|
+
textDescription?:string,
|
|
67
|
+
/**The description for the slash command. */
|
|
68
|
+
slashDescription?:string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**## ODHelpMenuCommandComponent `class`
|
|
72
|
+
* This is an Open Discord help menu command component.
|
|
73
|
+
*
|
|
74
|
+
* It contains a useful helper to render a command in the Open Discord help menu.
|
|
75
|
+
*/
|
|
76
|
+
export class ODHelpMenuCommandComponent extends ODHelpMenuComponent {
|
|
77
|
+
constructor(id:ODValidId, priority:number, settings:ODHelpMenuCommandComponentSettings){
|
|
78
|
+
super(id,priority,(page,category,location,mode) => {
|
|
79
|
+
if (mode == "slash" && settings.slashName){
|
|
80
|
+
return `\`${settings.slashName}${(settings.slashOptions) ? this.#renderOptions(settings.slashOptions) : ""}\` ➜ ${settings.slashDescription ?? ""}`
|
|
81
|
+
|
|
82
|
+
}else if (mode == "text" && settings.textName){
|
|
83
|
+
return `\`${settings.textName}${(settings.textOptions) ? this.#renderOptions(settings.textOptions) : ""}\` ➜ ${settings.textDescription ?? ""}`
|
|
84
|
+
|
|
85
|
+
}else return ""
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**Utility function to render all command options. */
|
|
90
|
+
#renderOptions(options:ODHelpMenuCommandComponentOption[]){
|
|
91
|
+
return " "+options.map((opt) => (opt.optional) ? `[${opt.name}]` : `<${opt.name}>`).join(" ")
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**## ODHelpMenuCategory `class`
|
|
96
|
+
* This is an Open Discord help menu category.
|
|
97
|
+
*
|
|
98
|
+
* Every category in the help menu is an embed field by default.
|
|
99
|
+
* Try to limit the amount of components per category.
|
|
100
|
+
*/
|
|
101
|
+
export class ODHelpMenuCategory extends ODManager<ODHelpMenuComponent> {
|
|
102
|
+
/**The id of this category. */
|
|
103
|
+
id: ODId
|
|
104
|
+
/**The priority of this category. The higher, the earlier it will appear in the menu. */
|
|
105
|
+
priority: number
|
|
106
|
+
/**The name of this category. (can include emoji's) */
|
|
107
|
+
name: string
|
|
108
|
+
/**When enabled, it automatically starts this category on a new page. */
|
|
109
|
+
newPage: boolean
|
|
110
|
+
|
|
111
|
+
constructor(id:ODValidId, priority:number, name:string, newPage?:boolean){
|
|
112
|
+
super()
|
|
113
|
+
this.id = new ODId(id)
|
|
114
|
+
this.priority = priority
|
|
115
|
+
this.name = name
|
|
116
|
+
this.newPage = newPage ?? false
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**Render this category and it's components. */
|
|
120
|
+
async render(page:number, category:number, mode:"slash"|"text"){
|
|
121
|
+
//sort from high priority to low
|
|
122
|
+
const derefArray = [...this.getAll()]
|
|
123
|
+
derefArray.sort((a,b) => {
|
|
124
|
+
return b.priority-a.priority
|
|
125
|
+
})
|
|
126
|
+
const result: string[] = []
|
|
127
|
+
|
|
128
|
+
let i = 0
|
|
129
|
+
for (const component of derefArray){
|
|
130
|
+
try {
|
|
131
|
+
result.push(await component.render(page,category,i,mode))
|
|
132
|
+
}catch(err){
|
|
133
|
+
process.emit("uncaughtException",err)
|
|
134
|
+
}
|
|
135
|
+
i++
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//only return the non-empty components
|
|
139
|
+
return result.filter((component) => component !== "").join("\n\n")
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**## ODHelpMenuRenderResult `type`
|
|
144
|
+
* This is the array returned when the help menu has been rendered successfully.
|
|
145
|
+
*
|
|
146
|
+
* It contains a list of pages, which contain categories by name & value (content).
|
|
147
|
+
*/
|
|
148
|
+
export type ODHelpMenuRenderResult = {name:string, value:string}[][]
|
|
149
|
+
|
|
150
|
+
/**## ODHelpMenuManager `class`
|
|
151
|
+
* This is an Open Discord help menu manager.
|
|
152
|
+
*
|
|
153
|
+
* It is responsible for rendering the entire help menu content.
|
|
154
|
+
* You are also able to configure the amount of categories per page here.
|
|
155
|
+
*
|
|
156
|
+
* Fewer Categories == More Clean Menu
|
|
157
|
+
*/
|
|
158
|
+
export class ODHelpMenuManager extends ODManager<ODHelpMenuCategory> {
|
|
159
|
+
/**Alias to Open Discord debugger. */
|
|
160
|
+
#debug: ODDebugger
|
|
161
|
+
/**The amount of categories per-page. */
|
|
162
|
+
categoriesPerPage: number = 3
|
|
163
|
+
|
|
164
|
+
constructor(debug:ODDebugger){
|
|
165
|
+
super(debug,"help menu category")
|
|
166
|
+
this.#debug = debug
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
add(data:ODHelpMenuCategory, overwrite?:boolean): boolean {
|
|
170
|
+
data.useDebug(this.#debug,"help menu component")
|
|
171
|
+
return super.add(data,overwrite)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**Render this entire help menu & return a `ODHelpMenuRenderResult`. */
|
|
175
|
+
async render(mode:"slash"|"text"): Promise<ODHelpMenuRenderResult> {
|
|
176
|
+
//sort from high priority to low
|
|
177
|
+
const derefArray = [...this.getAll()]
|
|
178
|
+
derefArray.sort((a,b) => {
|
|
179
|
+
return b.priority-a.priority
|
|
180
|
+
})
|
|
181
|
+
const result: {name:string, value:string}[][] = []
|
|
182
|
+
let currentPage: {name:string, value:string}[] = []
|
|
183
|
+
|
|
184
|
+
for (const category of derefArray){
|
|
185
|
+
try {
|
|
186
|
+
const renderedCategory = await category.render(result.length,currentPage.length,mode)
|
|
187
|
+
|
|
188
|
+
if (renderedCategory !== ""){
|
|
189
|
+
//create new page when category wants to
|
|
190
|
+
if (currentPage.length > 0 && category.newPage){
|
|
191
|
+
result.push(currentPage)
|
|
192
|
+
currentPage = []
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
currentPage.push({
|
|
196
|
+
name:category.name,
|
|
197
|
+
value:renderedCategory
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
//create new page when page is full
|
|
201
|
+
if (currentPage.length >= this.categoriesPerPage){
|
|
202
|
+
result.push(currentPage)
|
|
203
|
+
currentPage = []
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}catch(err){
|
|
207
|
+
process.emit("uncaughtException",err)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//push current page when not-empty
|
|
212
|
+
if (currentPage.length > 0) result.push(currentPage)
|
|
213
|
+
|
|
214
|
+
return result
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
///////////////////////////////////////
|
|
2
|
+
//LANGUAGE MODULE
|
|
3
|
+
///////////////////////////////////////
|
|
4
|
+
import { ODId, ODManager, ODManagerData, ODPromiseVoid, ODSystemError, ODValidId } from "./base"
|
|
5
|
+
import nodepath from "path"
|
|
6
|
+
import { ODDebugger } from "./console"
|
|
7
|
+
import fs from "fs"
|
|
8
|
+
|
|
9
|
+
/**## ODLanguageMetadata `interface`
|
|
10
|
+
* This interface contains all metadata available in the language files.
|
|
11
|
+
*/
|
|
12
|
+
export interface ODLanguageMetadata {
|
|
13
|
+
/**The version of Open Discord this translation is made for. */
|
|
14
|
+
otversion:string,
|
|
15
|
+
/**The name of the language in english (with capital letter). */
|
|
16
|
+
language:string,
|
|
17
|
+
/**A list of translators (discord/github username) who've contributed to this language. */
|
|
18
|
+
translators:string[],
|
|
19
|
+
/**The last date that this translation has been modified (format: DD/MM/YYYY) */
|
|
20
|
+
lastedited:string,
|
|
21
|
+
/**When `true`, the translator made use of some sort of automation while creating the translation. (e.g. ChatGPT, Google Translate, DeepL, ...) */
|
|
22
|
+
automated:boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**## ODLanguageManager `class`
|
|
26
|
+
* This is an Open Discord language manager.
|
|
27
|
+
*
|
|
28
|
+
* It manages all languages in the bot and manages translation for you!
|
|
29
|
+
* Get a translation via the `getTranslation()` or `getTranslationWithParams()` methods.
|
|
30
|
+
*
|
|
31
|
+
* Add new languages using the `ODlanguage` class in your plugin!
|
|
32
|
+
*/
|
|
33
|
+
export class ODLanguageManager extends ODManager<ODLanguage> {
|
|
34
|
+
/**The currently selected language. */
|
|
35
|
+
current: ODLanguage|null = null
|
|
36
|
+
/**The currently selected backup language. (used when translation missing in current language) */
|
|
37
|
+
backup: ODLanguage|null = null
|
|
38
|
+
/**An alias to Open Discord debugger. */
|
|
39
|
+
#debug: ODDebugger
|
|
40
|
+
|
|
41
|
+
constructor(debug:ODDebugger, presets:boolean){
|
|
42
|
+
super(debug,"language")
|
|
43
|
+
if (presets) this.add(new ODLanguage("english","english.json"))
|
|
44
|
+
this.current = presets ? new ODLanguage("english","english.json") : null
|
|
45
|
+
this.backup = presets ? new ODLanguage("english","english.json") : null
|
|
46
|
+
this.#debug = debug
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**Set the current language by providing the ID of a language which is registered in this manager. */
|
|
50
|
+
setCurrentLanguage(id:ODValidId){
|
|
51
|
+
this.current = this.get(id)
|
|
52
|
+
const languageId = this.current?.id.value ?? "<unknown-id>"
|
|
53
|
+
const languageAutomated = this.current?.metadata?.automated.toString() ?? "<unknown-metadata>"
|
|
54
|
+
this.#debug.debug("Selected current language",[
|
|
55
|
+
{key:"id",value:languageId},
|
|
56
|
+
{key:"automated",value:languageAutomated},
|
|
57
|
+
])
|
|
58
|
+
}
|
|
59
|
+
/**Get the current language (same as `this.current`) */
|
|
60
|
+
getCurrentLanguage(){
|
|
61
|
+
return (this.current) ? this.current : null
|
|
62
|
+
}
|
|
63
|
+
/**Set the backup language by providing the ID of a language which is registered in this manager. */
|
|
64
|
+
setBackupLanguage(id:ODValidId){
|
|
65
|
+
this.backup = this.get(id)
|
|
66
|
+
const languageId = this.backup?.id.value ?? "<unknown-id>"
|
|
67
|
+
const languageAutomated = this.backup?.metadata?.automated.toString() ?? "<unknown-metadata>"
|
|
68
|
+
this.#debug.debug("Selected backup language",[
|
|
69
|
+
{key:"id",value:languageId},
|
|
70
|
+
{key:"automated",value:languageAutomated},
|
|
71
|
+
])
|
|
72
|
+
}
|
|
73
|
+
/**Get the backup language (same as `this.backup`) */
|
|
74
|
+
getBackupLanguage(){
|
|
75
|
+
return (this.backup) ? this.backup : null
|
|
76
|
+
}
|
|
77
|
+
/**Get the metadata of the current/backup language. */
|
|
78
|
+
getLanguageMetadata(frombackup?:boolean): ODLanguageMetadata|null {
|
|
79
|
+
if (frombackup) return (this.backup) ? this.backup.metadata : null
|
|
80
|
+
return (this.current) ? this.current.metadata : null
|
|
81
|
+
}
|
|
82
|
+
/**Get the ID (string) of the current language. (Not backup language) */
|
|
83
|
+
getCurrentLanguageId(){
|
|
84
|
+
return (this.current) ? this.current.id.value : ""
|
|
85
|
+
}
|
|
86
|
+
/**Get a translation string by JSON location. (e.g. `"checker.system.typeError"`) */
|
|
87
|
+
getTranslation(id:string): string|null {
|
|
88
|
+
if (!this.current) return this.#getBackupTranslation(id)
|
|
89
|
+
|
|
90
|
+
const splitted = id.split(".")
|
|
91
|
+
let currentObject = this.current.data
|
|
92
|
+
let result: string|false = false
|
|
93
|
+
splitted.forEach((id) => {
|
|
94
|
+
if (typeof currentObject[id] == "object"){
|
|
95
|
+
currentObject = currentObject[id]
|
|
96
|
+
}else if (typeof currentObject[id] == "string"){
|
|
97
|
+
result = currentObject[id]
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
if (typeof result == "string") return result
|
|
102
|
+
else return this.#getBackupTranslation(id)
|
|
103
|
+
}
|
|
104
|
+
/**Get a backup translation string by JSON location. (system only) */
|
|
105
|
+
#getBackupTranslation(id:string): string|null {
|
|
106
|
+
if (!this.backup) return null
|
|
107
|
+
|
|
108
|
+
const splitted = id.split(".")
|
|
109
|
+
let currentObject = this.backup.data
|
|
110
|
+
let result: string|false = false
|
|
111
|
+
splitted.forEach((id) => {
|
|
112
|
+
if (typeof currentObject[id] == "object"){
|
|
113
|
+
currentObject = currentObject[id]
|
|
114
|
+
}else if (typeof currentObject[id] == "string"){
|
|
115
|
+
result = currentObject[id]
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
if (typeof result == "string") return result
|
|
120
|
+
else return null
|
|
121
|
+
}
|
|
122
|
+
/**Get a backup translation string by JSON location and replace `{0}`,`{1}`,`{2}`,... with the provided parameters. */
|
|
123
|
+
getTranslationWithParams(id:string, params:string[]): string|null {
|
|
124
|
+
let translation = this.getTranslation(id)
|
|
125
|
+
if (!translation) return translation
|
|
126
|
+
|
|
127
|
+
params.forEach((value,index) => {
|
|
128
|
+
if (!translation) return
|
|
129
|
+
translation = translation.replace(`{${index}}`,value)
|
|
130
|
+
})
|
|
131
|
+
return translation
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**Init all language files. */
|
|
135
|
+
async init(){
|
|
136
|
+
for (const language of this.getAll()){
|
|
137
|
+
try{
|
|
138
|
+
await language.init()
|
|
139
|
+
}catch(err){
|
|
140
|
+
process.emit("uncaughtException",new ODSystemError(err))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**## ODLanguage `class`
|
|
147
|
+
* This is an Open Discord language file.
|
|
148
|
+
*
|
|
149
|
+
* It contains metadata and all translation strings available in this language.
|
|
150
|
+
* Register this class to an `ODLanguageManager` to use it!
|
|
151
|
+
*
|
|
152
|
+
* JSON languages should be created using the `ODJsonLanguage` class instead!
|
|
153
|
+
*/
|
|
154
|
+
export class ODLanguage extends ODManagerData {
|
|
155
|
+
/**The name of the file with extension. */
|
|
156
|
+
file: string = ""
|
|
157
|
+
/**The path to the file relative to the main directory. */
|
|
158
|
+
path: string = ""
|
|
159
|
+
/**The raw object data of the translation. */
|
|
160
|
+
data: any
|
|
161
|
+
/**The metadata of the language if available. */
|
|
162
|
+
metadata: ODLanguageMetadata|null = null
|
|
163
|
+
|
|
164
|
+
constructor(id:ODValidId, data:any){
|
|
165
|
+
super(id)
|
|
166
|
+
this.data = data
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**Init the language. */
|
|
170
|
+
init(): ODPromiseVoid {
|
|
171
|
+
//nothing
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**## ODJsonLanguage `class`
|
|
176
|
+
* This is an Open Discord JSON language file.
|
|
177
|
+
*
|
|
178
|
+
* It contains metadata and all translation strings from a certain JSON file (in `./languages/`).
|
|
179
|
+
* Register this class to an `ODLanguageManager` to use it!
|
|
180
|
+
*
|
|
181
|
+
* Use the `ODLanguage` class to use translations from non-JSON files!
|
|
182
|
+
*/
|
|
183
|
+
export class ODJsonLanguage extends ODLanguage {
|
|
184
|
+
constructor(id:ODValidId, file:string, customPath?:string){
|
|
185
|
+
super(id,{})
|
|
186
|
+
this.file = (file.endsWith(".json")) ? file : file+".json"
|
|
187
|
+
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**Init the langauge. */
|
|
191
|
+
init(): ODPromiseVoid {
|
|
192
|
+
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
|
|
193
|
+
try{
|
|
194
|
+
this.data = JSON.parse(fs.readFileSync(this.path).toString())
|
|
195
|
+
}catch(err){
|
|
196
|
+
process.emit("uncaughtException",err)
|
|
197
|
+
throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\"!")
|
|
198
|
+
}
|
|
199
|
+
if (this.data["_TRANSLATION"]) this.metadata = this.data["_TRANSLATION"]
|
|
200
|
+
}
|
|
201
|
+
}
|