@open-discord-bots/framework 0.2.17 → 0.3.1
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/api/index.d.ts +16 -15
- package/dist/api/index.js +16 -15
- package/dist/api/main.d.ts +31 -23
- package/dist/api/main.js +3 -1
- package/dist/api/modules/action.d.ts +2 -2
- package/dist/api/modules/action.js +1 -5
- package/dist/api/modules/base.d.ts +29 -11
- package/dist/api/modules/base.js +78 -80
- package/dist/api/modules/builder.d.ts +2 -11
- package/dist/api/modules/builder.js +0 -4
- package/dist/api/modules/checker.d.ts +28 -7
- package/dist/api/modules/checker.js +33 -37
- package/dist/api/modules/client.d.ts +66 -14
- package/dist/api/modules/client.js +146 -132
- package/dist/api/modules/component.d.ts +928 -0
- package/dist/api/modules/component.js +1346 -0
- package/dist/api/modules/config.d.ts +30 -2
- package/dist/api/modules/config.js +90 -7
- package/dist/api/modules/console.d.ts +16 -4
- package/dist/api/modules/console.js +25 -25
- package/dist/api/modules/cooldown.d.ts +5 -5
- package/dist/api/modules/cooldown.js +1 -17
- package/dist/api/modules/database.d.ts +21 -13
- package/dist/api/modules/database.js +0 -23
- package/dist/api/modules/event.d.ts +4 -2
- package/dist/api/modules/event.js +8 -10
- package/dist/api/modules/fuse.d.ts +1 -1
- package/dist/api/modules/helpmenu.d.ts +11 -9
- package/dist/api/modules/helpmenu.js +24 -22
- package/dist/api/modules/language.d.ts +4 -3
- package/dist/api/modules/language.js +9 -16
- package/dist/api/modules/permission.d.ts +10 -1
- package/dist/api/modules/permission.js +17 -20
- package/dist/api/modules/plugin.d.ts +2 -1
- package/dist/api/modules/plugin.js +2 -2
- package/dist/api/modules/post.d.ts +12 -4
- package/dist/api/modules/post.js +36 -10
- package/dist/api/modules/progressbar.d.ts +18 -6
- package/dist/api/modules/progressbar.js +35 -35
- package/dist/api/modules/responder.d.ts +97 -28
- package/dist/api/modules/responder.js +213 -176
- package/dist/api/modules/session.d.ts +11 -2
- package/dist/api/modules/session.js +16 -16
- package/dist/api/modules/startscreen.d.ts +2 -3
- package/dist/api/modules/startscreen.js +8 -9
- package/dist/api/modules/statistic.d.ts +2 -1
- package/dist/api/modules/statistic.js +4 -7
- package/dist/api/modules/worker.d.ts +2 -1
- package/dist/api/modules/worker.js +3 -3
- package/package.json +3 -2
- package/src/api/index.ts +16 -15
- package/src/api/main.ts +33 -24
- package/src/api/modules/action.ts +2 -4
- package/src/api/modules/base.ts +77 -79
- package/src/api/modules/builder.ts +2 -14
- package/src/api/modules/checker.ts +36 -37
- package/src/api/modules/client.ts +144 -136
- package/src/api/modules/component.ts +1826 -0
- package/src/api/modules/config.ts +86 -7
- package/src/api/modules/console.ts +25 -25
- package/src/api/modules/cooldown.ts +8 -13
- package/src/api/modules/database.ts +24 -32
- package/src/api/modules/event.ts +6 -10
- package/src/api/modules/fuse.ts +1 -1
- package/src/api/modules/helpmenu.ts +31 -27
- package/src/api/modules/language.ts +11 -16
- package/src/api/modules/permission.ts +17 -20
- package/src/api/modules/plugin.ts +2 -2
- package/src/api/modules/post.ts +31 -10
- package/src/api/modules/progressbar.ts +36 -37
- package/src/api/modules/responder.ts +234 -185
- package/src/api/modules/session.ts +15 -15
- package/src/api/modules/startscreen.ts +9 -10
- package/src/api/modules/statistic.ts +4 -7
- package/src/api/modules/worker.ts +3 -3
- package/src/api/modules/component.txt +0 -350
|
@@ -6,6 +6,7 @@ import nodepath from "path"
|
|
|
6
6
|
import { ODDebugger } from "./console.js"
|
|
7
7
|
import fs from "fs"
|
|
8
8
|
import * as fjs from "formatted-json-stringify"
|
|
9
|
+
import { jsonc } from "jsonc"
|
|
9
10
|
|
|
10
11
|
/**## ODConfigManagerIdConstraint `type`
|
|
11
12
|
* The constraint/layout for id mappings/interfaces of the `ODConfigManager` class.
|
|
@@ -20,16 +21,18 @@ export type ODConfigManagerIdConstraint = Record<string,ODConfig<any>>
|
|
|
20
21
|
* You can use this class to get/change/add a config file (`ODConfig`) in your plugin!
|
|
21
22
|
*/
|
|
22
23
|
export class ODConfigManager<IdList extends ODConfigManagerIdConstraint = ODConfigManagerIdConstraint> extends ODManager<ODConfig<any>> {
|
|
23
|
-
/**Alias to Open Discord debugger. */
|
|
24
|
-
#debug: ODDebugger
|
|
25
|
-
|
|
26
24
|
constructor(debug:ODDebugger){
|
|
27
25
|
super(debug,"config")
|
|
28
|
-
this.#debug = debug
|
|
29
26
|
}
|
|
27
|
+
|
|
30
28
|
add(data:ODConfig<any>|ODConfig<any>[],overwrite?:boolean): boolean {
|
|
31
|
-
if (
|
|
32
|
-
|
|
29
|
+
if (this.debug){
|
|
30
|
+
if (Array.isArray(data)){
|
|
31
|
+
for (const d of data){
|
|
32
|
+
d.useDebug(this.debug)
|
|
33
|
+
}
|
|
34
|
+
}else data.useDebug(this.debug)
|
|
35
|
+
}
|
|
33
36
|
return super.add(data,overwrite)
|
|
34
37
|
}
|
|
35
38
|
/**Init all config files. */
|
|
@@ -71,7 +74,7 @@ export class ODConfigManager<IdList extends ODConfigManagerIdConstraint = ODConf
|
|
|
71
74
|
*
|
|
72
75
|
* You can use this class if you want to create your own config implementation (e.g. `yml`, `xml`,...)!
|
|
73
76
|
*/
|
|
74
|
-
export class ODConfig<Data extends any> extends ODManagerData {
|
|
77
|
+
export abstract class ODConfig<Data extends any> extends ODManagerData {
|
|
75
78
|
/**The name of the file with extension. */
|
|
76
79
|
file: string = ""
|
|
77
80
|
/**The path to the file relative to the main directory. */
|
|
@@ -182,4 +185,80 @@ export class ODJsonConfig<Data extends any> extends ODConfig<Data> {
|
|
|
182
185
|
throw new ODSystemError("Unable to save config \""+nodepath.join("./",this.path)+"\"!")
|
|
183
186
|
}
|
|
184
187
|
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**## ODJsonCommentsConfig `class`
|
|
191
|
+
* An Open Discord JSONC (`.jsonc`) config.
|
|
192
|
+
* Use this class to get & edit variables from the config files or to create your own JSON config!
|
|
193
|
+
* @example
|
|
194
|
+
* //create a config from: ./config/test.jsonc with the id "some-config"
|
|
195
|
+
* const config = new api.ODJsonCommentsConfig("some-config","test.jsonc")
|
|
196
|
+
*
|
|
197
|
+
* //create a config with custom dir: ./plugins/testplugin/test.jsonc
|
|
198
|
+
* const config = new api.ODJsonCommentsConfig("plugin-config","test.jsonc","./plugins/testplugin/")
|
|
199
|
+
*/
|
|
200
|
+
export class ODJsonCommentsConfig<Data extends any> extends ODConfig<Data> {
|
|
201
|
+
formatter: fjs.custom.BaseFormatter
|
|
202
|
+
|
|
203
|
+
constructor(id:ODValidId, file:string, customPath?:string, formatter?:fjs.custom.BaseFormatter){
|
|
204
|
+
super(id,{})
|
|
205
|
+
this.file = (file.endsWith(".jsonc")) ? file : file+".jsonc"
|
|
206
|
+
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./config/",this.file)
|
|
207
|
+
this.formatter = formatter ?? new fjs.DefaultFormatter(null,true," ")
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**Init the config. */
|
|
211
|
+
init(): ODPromiseVoid {
|
|
212
|
+
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse JSONC config \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
|
|
213
|
+
try{
|
|
214
|
+
this.data = jsonc.parse(fs.readFileSync(this.path).toString())
|
|
215
|
+
super.init()
|
|
216
|
+
}catch(err){
|
|
217
|
+
process.emit("uncaughtException",err)
|
|
218
|
+
throw new ODSystemError("Unable to parse JSONC config \""+nodepath.join("./",this.path)+"\"!")
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**Reload the config. Be aware that this doesn't update the config data everywhere in the bot! */
|
|
222
|
+
reload(){
|
|
223
|
+
if (!this.initiated) throw new ODSystemError("Unable to reload JSONC config \""+nodepath.join("./",this.path)+"\", the file hasn't been initiated yet!")
|
|
224
|
+
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to JSONC reload config \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
|
|
225
|
+
try{
|
|
226
|
+
this.data = jsonc.parse(fs.readFileSync(this.path).toString())
|
|
227
|
+
super.reload()
|
|
228
|
+
this.reloadListeners.forEach((cb) => {
|
|
229
|
+
try{
|
|
230
|
+
cb()
|
|
231
|
+
}catch(err){
|
|
232
|
+
process.emit("uncaughtException",err)
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
}catch(err){
|
|
236
|
+
process.emit("uncaughtException",err)
|
|
237
|
+
throw new ODSystemError("Unable to reload JSONC config \""+nodepath.join("./",this.path)+"\"!")
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**Save the edited config to the filesystem. This is used by the Interactive Setup CLI. It's not recommended to use this while the bot is running. */
|
|
241
|
+
save(): ODPromiseVoid {
|
|
242
|
+
if (!this.initiated) throw new ODSystemError("Unable to save JSONC config \""+nodepath.join("./",this.path)+"\", the file hasn't been initiated yet!")
|
|
243
|
+
try{
|
|
244
|
+
const contents = this.formatter.stringify(this.data as ODValidJsonType)
|
|
245
|
+
fs.writeFileSync(this.path,contents)
|
|
246
|
+
super.save()
|
|
247
|
+
}catch(err){
|
|
248
|
+
process.emit("uncaughtException",err)
|
|
249
|
+
throw new ODSystemError("Unable to save JSONC config \""+nodepath.join("./",this.path)+"\"!")
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**## ODMemoryConfig `class`
|
|
255
|
+
* An Open Discord memory config.
|
|
256
|
+
* This config lives in-memory and does not have any connection to the filesystem.
|
|
257
|
+
*
|
|
258
|
+
* It is perfect for temporary configs or using the `ODChecker` without a real config file.
|
|
259
|
+
*/
|
|
260
|
+
export class ODMemoryConfig<Data extends any> extends ODConfig<Data> {
|
|
261
|
+
constructor(id:ODValidId,data:Data){
|
|
262
|
+
super(id,data)
|
|
263
|
+
}
|
|
185
264
|
}
|
|
@@ -278,10 +278,10 @@ export class ODConsoleManager {
|
|
|
278
278
|
if (this.debugfile) this.debugfile.writeConsoleMessage(newMessage)
|
|
279
279
|
this.history.push(newMessage)
|
|
280
280
|
}
|
|
281
|
-
this
|
|
281
|
+
this.purgeHistory()
|
|
282
282
|
}
|
|
283
283
|
/**Shorten the history when it exceeds the max history length! */
|
|
284
|
-
|
|
284
|
+
protected purgeHistory(){
|
|
285
285
|
if (this.history.length > this.historylength) this.history.shift()
|
|
286
286
|
}
|
|
287
287
|
}
|
|
@@ -310,16 +310,16 @@ export class ODDebugFileManager {
|
|
|
310
310
|
this.version = version
|
|
311
311
|
this.maxlines = maxlines
|
|
312
312
|
|
|
313
|
-
this
|
|
313
|
+
this.writeStartupStats()
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
/**Check if the debug file exists */
|
|
317
|
-
|
|
317
|
+
protected existsDebugFile(){
|
|
318
318
|
return fs.existsSync(this.path)
|
|
319
319
|
}
|
|
320
320
|
/**Read from the debug file */
|
|
321
|
-
|
|
322
|
-
if (this
|
|
321
|
+
protected readDebugFile(){
|
|
322
|
+
if (this.existsDebugFile()){
|
|
323
323
|
try {
|
|
324
324
|
return fs.readFileSync(this.path).toString()
|
|
325
325
|
}catch{
|
|
@@ -330,8 +330,8 @@ export class ODDebugFileManager {
|
|
|
330
330
|
}
|
|
331
331
|
}
|
|
332
332
|
/**Write to the debug file and shorten it when needed. */
|
|
333
|
-
|
|
334
|
-
const currenttext = this
|
|
333
|
+
protected writeDebugFile(text:string){
|
|
334
|
+
const currenttext = this.readDebugFile()
|
|
335
335
|
if (currenttext){
|
|
336
336
|
const splitted = currenttext.split("\n")
|
|
337
337
|
|
|
@@ -343,12 +343,12 @@ export class ODDebugFileManager {
|
|
|
343
343
|
fs.writeFileSync(this.path,splitted.join("\n"))
|
|
344
344
|
}else{
|
|
345
345
|
//write new file:
|
|
346
|
-
const newtext = this
|
|
346
|
+
const newtext = this.createStatsText()+text
|
|
347
347
|
fs.writeFileSync(this.path,newtext)
|
|
348
348
|
}
|
|
349
349
|
}
|
|
350
350
|
/**Generate the statistics/header of the debug file (containing the version) */
|
|
351
|
-
|
|
351
|
+
protected createStatsText(){
|
|
352
352
|
const date = new Date()
|
|
353
353
|
const dstring = `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
|
|
354
354
|
return [
|
|
@@ -360,8 +360,8 @@ export class ODDebugFileManager {
|
|
|
360
360
|
].join("\n")
|
|
361
361
|
}
|
|
362
362
|
/**Write the statistics/header to the debug file on startup */
|
|
363
|
-
|
|
364
|
-
const currenttext = this
|
|
363
|
+
protected writeStartupStats(){
|
|
364
|
+
const currenttext = this.readDebugFile()
|
|
365
365
|
if (currenttext){
|
|
366
366
|
//edit previous file:
|
|
367
367
|
const splitted = currenttext.split("\n")
|
|
@@ -371,31 +371,31 @@ export class ODDebugFileManager {
|
|
|
371
371
|
splitted.splice(0,((splitted.length+11) - this.maxlines))
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
splitted.unshift(this
|
|
374
|
+
splitted.unshift(this.createStatsText())
|
|
375
375
|
splitted.push("\n---------------------------------------------------------------------\n---------------------------------------------------------------------\n")
|
|
376
376
|
|
|
377
377
|
fs.writeFileSync(this.path,splitted.join("\n"))
|
|
378
378
|
}else{
|
|
379
379
|
//write new file:
|
|
380
|
-
const newtext = this
|
|
380
|
+
const newtext = this.createStatsText()
|
|
381
381
|
fs.writeFileSync(this.path,newtext)
|
|
382
382
|
}
|
|
383
383
|
}
|
|
384
384
|
/**Write an `ODConsoleMessage` to the debug file */
|
|
385
385
|
writeConsoleMessage(message:ODConsoleMessage){
|
|
386
|
-
this
|
|
386
|
+
this.writeDebugFile(message.toDebugString())
|
|
387
387
|
}
|
|
388
388
|
/**Write an `ODError` to the debug file */
|
|
389
389
|
writeErrorMessage(error:ODError){
|
|
390
|
-
this
|
|
390
|
+
this.writeDebugFile(error.toDebugString())
|
|
391
391
|
}
|
|
392
392
|
/**Write custom text to the debug file */
|
|
393
393
|
writeText(text:string){
|
|
394
|
-
this
|
|
394
|
+
this.writeDebugFile(text)
|
|
395
395
|
}
|
|
396
396
|
/**Write a custom note to the debug file (starting with `[NOTE]:`) */
|
|
397
397
|
writeNote(text:string){
|
|
398
|
-
this
|
|
398
|
+
this.writeDebugFile("[NOTE]: "+text)
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
|
|
@@ -597,7 +597,7 @@ export class ODLiveStatusManager<IdList extends ODLiveStatusManagerIdConstraint
|
|
|
597
597
|
/**The class responsible for rendering the livestatus messages. */
|
|
598
598
|
renderer: ODLiveStatusRenderer
|
|
599
599
|
/**A reference to the ODMain or "opendiscord" global variable */
|
|
600
|
-
|
|
600
|
+
protected main: ODMain|null = null
|
|
601
601
|
|
|
602
602
|
constructor(debug:ODDebugger,console:ODConsoleManager){
|
|
603
603
|
super(debug,"livestatus source")
|
|
@@ -606,18 +606,18 @@ export class ODLiveStatusManager<IdList extends ODLiveStatusManagerIdConstraint
|
|
|
606
606
|
|
|
607
607
|
/**Get the messages from all sources combined! */
|
|
608
608
|
async getAllMessages(): Promise<ODLiveStatusSourceData[]> {
|
|
609
|
-
if (!this
|
|
609
|
+
if (!this.main) throw new ODSystemError("ODLiveStatusManager:getAllMessages() --> Unable to get messages, 'opendiscord/ODMain' has not been connected!")
|
|
610
610
|
const messages: ODLiveStatusSourceData[] = []
|
|
611
611
|
for (const source of this.getAll()){
|
|
612
612
|
try {
|
|
613
|
-
messages.push(...(await source.getMessages(this
|
|
613
|
+
messages.push(...(await source.getMessages(this.main)))
|
|
614
614
|
}catch{}
|
|
615
615
|
}
|
|
616
616
|
return messages
|
|
617
617
|
}
|
|
618
618
|
/**Set the opendiscord `ODMain` class to use for fetching message filters. */
|
|
619
619
|
useMain(main:ODMain){
|
|
620
|
-
this
|
|
620
|
+
this.main = main
|
|
621
621
|
}
|
|
622
622
|
|
|
623
623
|
get<LiveStatusId extends keyof ODNoGeneric<IdList>>(id:LiveStatusId): IdList[LiveStatusId]
|
|
@@ -649,10 +649,10 @@ export class ODLiveStatusManager<IdList extends ODLiveStatusManagerIdConstraint
|
|
|
649
649
|
*/
|
|
650
650
|
export class ODLiveStatusRenderer {
|
|
651
651
|
/**A reference to the ODConsoleManager or "opendiscord.console" global variable */
|
|
652
|
-
|
|
652
|
+
protected console: ODConsoleManager
|
|
653
653
|
|
|
654
654
|
constructor(console:ODConsoleManager){
|
|
655
|
-
this
|
|
655
|
+
this.console = console
|
|
656
656
|
}
|
|
657
657
|
|
|
658
658
|
/**Render all messages */
|
|
@@ -688,7 +688,7 @@ export class ODLiveStatusRenderer {
|
|
|
688
688
|
//return all messages
|
|
689
689
|
return final.join("\n")
|
|
690
690
|
}catch{
|
|
691
|
-
this
|
|
691
|
+
this.console.log("Failed to render LiveStatus messages!","error")
|
|
692
692
|
return ""
|
|
693
693
|
}
|
|
694
694
|
}
|
|
@@ -75,7 +75,7 @@ export class ODCooldownData<Data extends object> extends ODManagerData {
|
|
|
75
75
|
*
|
|
76
76
|
* There are also premade cooldowns available in the bot!
|
|
77
77
|
*/
|
|
78
|
-
export class ODCooldown<Data extends object> extends ODManagerData {
|
|
78
|
+
export abstract class ODCooldown<Data extends object> extends ODManagerData {
|
|
79
79
|
data: ODManager<ODCooldownData<Data>> = new ODManager()
|
|
80
80
|
/**Is this cooldown already initialized? */
|
|
81
81
|
ready: boolean = false
|
|
@@ -85,21 +85,16 @@ export class ODCooldown<Data extends object> extends ODManagerData {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
/**Check this id and start cooldown when it exeeds the limit! Returns `true` when on cooldown! */
|
|
88
|
-
use(id:string): boolean
|
|
89
|
-
|
|
90
|
-
}
|
|
88
|
+
abstract use(id:string): boolean
|
|
89
|
+
|
|
91
90
|
/**Check this id without starting or updating the cooldown. Returns `true` when on cooldown! */
|
|
92
|
-
check(id:string): boolean
|
|
93
|
-
|
|
94
|
-
}
|
|
91
|
+
abstract check(id:string): boolean
|
|
92
|
+
|
|
95
93
|
/**Remove the cooldown for an id when available.*/
|
|
96
|
-
delete(id:string)
|
|
97
|
-
|
|
98
|
-
}
|
|
94
|
+
abstract delete(id:string): void
|
|
95
|
+
|
|
99
96
|
/**Initialize the internal systems of this cooldown. */
|
|
100
|
-
|
|
101
|
-
throw new ODSystemError("Tried to use an unimplemented ODCooldown!")
|
|
102
|
-
}
|
|
97
|
+
abstract init(): Promise<void>|void
|
|
103
98
|
}
|
|
104
99
|
|
|
105
100
|
/**## ODCounterCooldown `class`
|
|
@@ -70,50 +70,42 @@ export type ODDatabaseIdConstraint = Record<string,ODValidJsonType>
|
|
|
70
70
|
*
|
|
71
71
|
* You can use this class if you want to create your own database implementation (e.g. `mongodb`, `mysql`,...)!
|
|
72
72
|
*/
|
|
73
|
-
export class ODDatabase<IdList extends ODDatabaseIdConstraint = ODDatabaseIdConstraint> extends ODManagerData {
|
|
73
|
+
export abstract class ODDatabase<IdList extends ODDatabaseIdConstraint = ODDatabaseIdConstraint> extends ODManagerData {
|
|
74
74
|
/**The name of the file with extension. */
|
|
75
75
|
file: string = ""
|
|
76
76
|
/**The path to the file relative to the main directory. */
|
|
77
77
|
path: string = ""
|
|
78
78
|
|
|
79
79
|
/**Init the database. */
|
|
80
|
-
init(): ODPromiseVoid
|
|
81
|
-
|
|
82
|
-
}
|
|
80
|
+
abstract init(): ODPromiseVoid
|
|
81
|
+
|
|
83
82
|
/**Add/Overwrite a specific category & key in the database. Returns `true` when overwritten. */
|
|
84
|
-
set<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string, value:IdList[CategoryId]): ODOptionalPromise<boolean>
|
|
85
|
-
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
|
|
86
|
-
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
|
|
87
|
-
|
|
88
|
-
}
|
|
83
|
+
abstract set<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string, value:IdList[CategoryId]): ODOptionalPromise<boolean>
|
|
84
|
+
abstract set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
|
|
85
|
+
abstract set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
|
|
86
|
+
|
|
89
87
|
/**Get a specific category & key in the database */
|
|
90
|
-
get<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string): ODOptionalPromise<IdList[CategoryId]|undefined>
|
|
91
|
-
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
|
|
92
|
-
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
|
|
93
|
-
|
|
94
|
-
}
|
|
88
|
+
abstract get<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string): ODOptionalPromise<IdList[CategoryId]|undefined>
|
|
89
|
+
abstract get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
|
|
90
|
+
abstract get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
|
|
91
|
+
|
|
95
92
|
/**Delete a specific category & key in the database */
|
|
96
|
-
delete<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string): ODOptionalPromise<boolean>
|
|
97
|
-
delete(category:string, key:string): ODOptionalPromise<boolean>
|
|
98
|
-
delete(category:string, key:string): ODOptionalPromise<boolean>
|
|
99
|
-
|
|
100
|
-
}
|
|
93
|
+
abstract delete<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId, key:string): ODOptionalPromise<boolean>
|
|
94
|
+
abstract delete(category:string, key:string): ODOptionalPromise<boolean>
|
|
95
|
+
abstract delete(category:string, key:string): ODOptionalPromise<boolean>
|
|
96
|
+
|
|
101
97
|
/**Check if a specific category & key exists in the database */
|
|
102
|
-
exists(category:keyof ODNoGeneric<IdList>, key:string): ODOptionalPromise<boolean>
|
|
103
|
-
exists(category:string, key:string): ODOptionalPromise<boolean>
|
|
104
|
-
exists(category:string, key:string): ODOptionalPromise<boolean>
|
|
105
|
-
|
|
106
|
-
}
|
|
98
|
+
abstract exists(category:keyof ODNoGeneric<IdList>, key:string): ODOptionalPromise<boolean>
|
|
99
|
+
abstract exists(category:string, key:string): ODOptionalPromise<boolean>
|
|
100
|
+
abstract exists(category:string, key:string): ODOptionalPromise<boolean>
|
|
101
|
+
|
|
107
102
|
/**Get a specific category in the database */
|
|
108
|
-
getCategory<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId): ODOptionalPromise<{key:string, value:IdList[CategoryId]}[]|undefined>
|
|
109
|
-
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
|
|
110
|
-
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
|
|
111
|
-
|
|
112
|
-
}
|
|
103
|
+
abstract getCategory<CategoryId extends keyof ODNoGeneric<IdList>>(category:CategoryId): ODOptionalPromise<{key:string, value:IdList[CategoryId]}[]|undefined>
|
|
104
|
+
abstract getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
|
|
105
|
+
abstract getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
|
|
106
|
+
|
|
113
107
|
/**Get all values in the database */
|
|
114
|
-
getAll(): ODOptionalPromise<ODJsonDatabaseStructure>
|
|
115
|
-
return []
|
|
116
|
-
}
|
|
108
|
+
abstract getAll(): ODOptionalPromise<ODJsonDatabaseStructure>
|
|
117
109
|
}
|
|
118
110
|
|
|
119
111
|
/**## ODJsonDatabaseStructure `type`
|
package/src/api/modules/event.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type ODEventCallback = (...args:any) => ODPromiseVoid
|
|
|
17
17
|
*/
|
|
18
18
|
export class ODEvent<Callback extends ODEventCallback = ODEventCallback> extends ODManagerData {
|
|
19
19
|
/**Alias to Open Discord debugger. */
|
|
20
|
-
|
|
20
|
+
protected debug?: ODDebugger
|
|
21
21
|
/**The list of permanent listeners. */
|
|
22
22
|
listeners: Function[] = []
|
|
23
23
|
/**The list of one-time listeners. List is cleared every time the event is emitted. */
|
|
@@ -27,10 +27,10 @@ export class ODEvent<Callback extends ODEventCallback = ODEventCallback> extends
|
|
|
27
27
|
|
|
28
28
|
/**Use the Open Discord debugger in this manager for logs*/
|
|
29
29
|
useDebug(debug:ODDebugger|null){
|
|
30
|
-
this
|
|
30
|
+
this.debug = debug ?? undefined
|
|
31
31
|
}
|
|
32
32
|
/**Get a collection of listeners combined from both types. Also clears the one-time listeners array! */
|
|
33
|
-
|
|
33
|
+
protected getCurrentListeners(){
|
|
34
34
|
const final: Function[] = []
|
|
35
35
|
this.oncelisteners.forEach((l) => final.push(l))
|
|
36
36
|
this.listeners.forEach((l) => final.push(l))
|
|
@@ -47,7 +47,7 @@ export class ODEvent<Callback extends ODEventCallback = ODEventCallback> extends
|
|
|
47
47
|
this.listeners.push(callback)
|
|
48
48
|
|
|
49
49
|
if (this.listeners.length > this.listenerLimit){
|
|
50
|
-
if (this
|
|
50
|
+
if (this.debug) this.debug.console.log(new ODWarningConsoleMessage("Possible event memory leak detected!",[
|
|
51
51
|
{key:"event",value:this.id.value},
|
|
52
52
|
{key:"listeners",value:this.listeners.length.toString()}
|
|
53
53
|
]))
|
|
@@ -65,7 +65,7 @@ export class ODEvent<Callback extends ODEventCallback = ODEventCallback> extends
|
|
|
65
65
|
}
|
|
66
66
|
/**Emit this event to all listeners. You are required to provide all parameters of the event! */
|
|
67
67
|
async emit(params:Parameters<Callback>): Promise<void> {
|
|
68
|
-
for (const listener of this
|
|
68
|
+
for (const listener of this.getCurrentListeners()){
|
|
69
69
|
try{
|
|
70
70
|
await listener(...params)
|
|
71
71
|
}catch(err:any){
|
|
@@ -89,16 +89,12 @@ export type ODEventManagerIdConstraint = Record<string,ODEvent>
|
|
|
89
89
|
* All events are available in the `opendiscord.events` global!
|
|
90
90
|
*/
|
|
91
91
|
export class ODEventManager<IdList extends ODEventManagerIdConstraint = ODEventManagerIdConstraint> extends ODManager<ODEvent> {
|
|
92
|
-
/**Reference to the Open Discord debugger */
|
|
93
|
-
#debug: ODDebugger
|
|
94
|
-
|
|
95
92
|
constructor(debug:ODDebugger){
|
|
96
93
|
super(debug,"event")
|
|
97
|
-
this.#debug = debug
|
|
98
94
|
}
|
|
99
95
|
|
|
100
96
|
add(data:ODEvent, overwrite?:boolean): boolean {
|
|
101
|
-
data.useDebug(this
|
|
97
|
+
if (this.debug) data.useDebug(this.debug)
|
|
102
98
|
return super.add(data,overwrite)
|
|
103
99
|
}
|
|
104
100
|
|
package/src/api/modules/fuse.ts
CHANGED
|
@@ -44,7 +44,7 @@ export type ODFuseStringArray<FuseList extends object> = {
|
|
|
44
44
|
*/
|
|
45
45
|
export class ODFuseManager<FuseList extends object> {
|
|
46
46
|
/**A list of all the defaults */
|
|
47
|
-
|
|
47
|
+
protected fuses: FuseList
|
|
48
48
|
|
|
49
49
|
constructor(fuses:FuseList){
|
|
50
50
|
this.fuses = fuses
|
|
@@ -4,27 +4,22 @@
|
|
|
4
4
|
import { ODId, ODManager, ODManagerData, ODNoGeneric, ODSystemError, ODValidId } from "./base.js"
|
|
5
5
|
import { ODDebugger } from "./console.js"
|
|
6
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
7
|
/**## ODHelpMenuComponent `class`
|
|
13
8
|
* This is an Open Discord help menu component.
|
|
14
9
|
*
|
|
15
10
|
* It can render something on the Open Discord help menu.
|
|
16
11
|
*/
|
|
17
|
-
export class ODHelpMenuComponent extends ODManagerData {
|
|
12
|
+
export abstract class ODHelpMenuComponent extends ODManagerData {
|
|
18
13
|
/**The priority of this component. The higher, the earlier it will appear in the help menu. */
|
|
19
14
|
priority: number
|
|
20
|
-
/**The render function for this component. */
|
|
21
|
-
render: ODHelpMenuComponentRenderer
|
|
22
15
|
|
|
23
|
-
constructor(id:ODValidId, priority:number
|
|
16
|
+
constructor(id:ODValidId, priority:number){
|
|
24
17
|
super(id)
|
|
25
18
|
this.priority = priority
|
|
26
|
-
this.render = render
|
|
27
19
|
}
|
|
20
|
+
|
|
21
|
+
/**The render function for this component. */
|
|
22
|
+
abstract render(page:number, category:number, location:number, mode:"slash"|"text"): string|Promise<string>
|
|
28
23
|
}
|
|
29
24
|
|
|
30
25
|
/**## ODHelpMenuTextComponent `class`
|
|
@@ -33,10 +28,16 @@ export class ODHelpMenuComponent extends ODManagerData {
|
|
|
33
28
|
* It can render a static piece of text on the Open Discord help menu.
|
|
34
29
|
*/
|
|
35
30
|
export class ODHelpMenuTextComponent extends ODHelpMenuComponent {
|
|
31
|
+
/**The text of this help menu component. */
|
|
32
|
+
text: string
|
|
33
|
+
|
|
36
34
|
constructor(id:ODValidId, priority:number, text:string){
|
|
37
|
-
super(id,priority
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
super(id,priority)
|
|
36
|
+
this.text = text
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
render(page:number,category:number,location:number,mode:"slash"|"text"){
|
|
40
|
+
return this.text
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
|
|
@@ -74,20 +75,26 @@ export interface ODHelpMenuCommandComponentSettings {
|
|
|
74
75
|
* It contains a useful helper to render a command in the Open Discord help menu.
|
|
75
76
|
*/
|
|
76
77
|
export class ODHelpMenuCommandComponent extends ODHelpMenuComponent {
|
|
78
|
+
/**The settings for this help menu component. */
|
|
79
|
+
settings:ODHelpMenuCommandComponentSettings
|
|
80
|
+
|
|
77
81
|
constructor(id:ODValidId, priority:number, settings:ODHelpMenuCommandComponentSettings){
|
|
78
|
-
super(id,priority
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
})
|
|
82
|
+
super(id,priority)
|
|
83
|
+
this.settings = settings
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
render(page:number,category:number,location:number,mode:"slash"|"text"){
|
|
87
|
+
if (mode == "slash" && this.settings.slashName){
|
|
88
|
+
return `\`${this.settings.slashName}${(this.settings.slashOptions) ? this.renderOptions(this.settings.slashOptions) : ""}\` ➜ ${this.settings.slashDescription ?? ""}`
|
|
89
|
+
|
|
90
|
+
}else if (mode == "text" && this.settings.textName){
|
|
91
|
+
return `\`${this.settings.textName}${(this.settings.textOptions) ? this.renderOptions(this.settings.textOptions) : ""}\` ➜ ${this.settings.textDescription ?? ""}`
|
|
92
|
+
|
|
93
|
+
}else return ""
|
|
87
94
|
}
|
|
88
95
|
|
|
89
96
|
/**Utility function to render all command options. */
|
|
90
|
-
|
|
97
|
+
protected renderOptions(options:ODHelpMenuCommandComponentOption[]){
|
|
91
98
|
return " "+options.map((opt) => (opt.optional) ? `[${opt.name}]` : `<${opt.name}>`).join(" ")
|
|
92
99
|
}
|
|
93
100
|
}
|
|
@@ -188,18 +195,15 @@ export type ODHelpMenuManagerIdConstraint = Record<string,ODHelpMenuCategory>
|
|
|
188
195
|
* Fewer Categories == More Clean Menu
|
|
189
196
|
*/
|
|
190
197
|
export class ODHelpMenuManager<IdList extends ODHelpMenuManagerIdConstraint = ODHelpMenuManagerIdConstraint> extends ODManager<ODHelpMenuCategory> {
|
|
191
|
-
/**Alias to Open Discord debugger. */
|
|
192
|
-
#debug: ODDebugger
|
|
193
198
|
/**The amount of categories per-page. */
|
|
194
199
|
categoriesPerPage: number = 3
|
|
195
200
|
|
|
196
201
|
constructor(debug:ODDebugger){
|
|
197
202
|
super(debug,"help menu category")
|
|
198
|
-
this.#debug = debug
|
|
199
203
|
}
|
|
200
204
|
|
|
201
205
|
add(data:ODHelpMenuCategory, overwrite?:boolean): boolean {
|
|
202
|
-
data.useDebug(this
|
|
206
|
+
data.useDebug(this.debug,"help menu component")
|
|
203
207
|
return super.add(data,overwrite)
|
|
204
208
|
}
|
|
205
209
|
|
|
@@ -40,15 +40,12 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
40
40
|
current: ODLanguage|null = null
|
|
41
41
|
/**The currently selected backup language. (used when translation missing in current language) */
|
|
42
42
|
backup: ODLanguage|null = null
|
|
43
|
-
/**An alias to Open Discord debugger. */
|
|
44
|
-
#debug: ODDebugger
|
|
45
43
|
|
|
46
44
|
constructor(debug:ODDebugger, presets:boolean){
|
|
47
45
|
super(debug,"language")
|
|
48
|
-
if (presets) this.add(new
|
|
49
|
-
this.current = presets ? new
|
|
50
|
-
this.backup = presets ? new
|
|
51
|
-
this.#debug = debug
|
|
46
|
+
if (presets) this.add(new ODJsonLanguage("english","english.json"))
|
|
47
|
+
this.current = presets ? new ODJsonLanguage("english","english.json") : null
|
|
48
|
+
this.backup = presets ? new ODJsonLanguage("english","english.json") : null
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
/**Set the current language by providing the ID of a language which is registered in this manager. */
|
|
@@ -58,7 +55,7 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
58
55
|
this.current = this.get(id)
|
|
59
56
|
const languageId = this.current?.id.value ?? "<unknown-id>"
|
|
60
57
|
const languageAutomated = this.current?.metadata?.automated.toString() ?? "<unknown-metadata>"
|
|
61
|
-
this
|
|
58
|
+
this.debug?.debug("Selected current language",[
|
|
62
59
|
{key:"id",value:languageId},
|
|
63
60
|
{key:"automated",value:languageAutomated},
|
|
64
61
|
])
|
|
@@ -74,7 +71,7 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
74
71
|
this.backup = this.get(id)
|
|
75
72
|
const languageId = this.backup?.id.value ?? "<unknown-id>"
|
|
76
73
|
const languageAutomated = this.backup?.metadata?.automated.toString() ?? "<unknown-metadata>"
|
|
77
|
-
this
|
|
74
|
+
this.debug?.debug("Selected backup language",[
|
|
78
75
|
{key:"id",value:languageId},
|
|
79
76
|
{key:"automated",value:languageAutomated},
|
|
80
77
|
])
|
|
@@ -96,7 +93,7 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
96
93
|
getTranslation(id:TranslationIds): string
|
|
97
94
|
getTranslation(id:string): string|null
|
|
98
95
|
getTranslation(id:string): string|null {
|
|
99
|
-
if (!this.current) return this
|
|
96
|
+
if (!this.current) return this.getBackupTranslation(id)
|
|
100
97
|
|
|
101
98
|
const splitted = id.split(".")
|
|
102
99
|
let currentObject = this.current.data
|
|
@@ -110,10 +107,10 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
110
107
|
})
|
|
111
108
|
|
|
112
109
|
if (typeof result == "string") return result
|
|
113
|
-
else return this
|
|
110
|
+
else return this.getBackupTranslation(id)
|
|
114
111
|
}
|
|
115
|
-
/**Get a backup
|
|
116
|
-
|
|
112
|
+
/**Get a backup translation string by JSON location. (system only) */
|
|
113
|
+
protected getBackupTranslation(id:string): string|null {
|
|
117
114
|
if (!this.backup) return null
|
|
118
115
|
|
|
119
116
|
const splitted = id.split(".")
|
|
@@ -185,7 +182,7 @@ export class ODLanguageManager<IdList extends ODLanguageManagerIdConstraint = OD
|
|
|
185
182
|
*
|
|
186
183
|
* JSON languages should be created using the `ODJsonLanguage` class instead!
|
|
187
184
|
*/
|
|
188
|
-
export class ODLanguage extends ODManagerData {
|
|
185
|
+
export abstract class ODLanguage extends ODManagerData {
|
|
189
186
|
/**The name of the file with extension. */
|
|
190
187
|
file: string = ""
|
|
191
188
|
/**The path to the file relative to the main directory. */
|
|
@@ -201,9 +198,7 @@ export class ODLanguage extends ODManagerData {
|
|
|
201
198
|
}
|
|
202
199
|
|
|
203
200
|
/**Init the language. */
|
|
204
|
-
init(): ODPromiseVoid
|
|
205
|
-
//nothing
|
|
206
|
-
}
|
|
201
|
+
abstract init(): ODPromiseVoid
|
|
207
202
|
}
|
|
208
203
|
|
|
209
204
|
/**## ODJsonLanguage `class`
|