@open-discord-bots/framework 0.3.0 → 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/main.js +1 -1
- package/dist/api/modules/base.d.ts +27 -9
- package/dist/api/modules/base.js +78 -80
- package/dist/api/modules/builder.d.ts +0 -9
- package/dist/api/modules/checker.d.ts +26 -5
- package/dist/api/modules/checker.js +31 -31
- package/dist/api/modules/client.d.ts +66 -14
- package/dist/api/modules/client.js +146 -132
- package/dist/api/modules/component.d.ts +8 -2
- package/dist/api/modules/component.js +8 -6
- package/dist/api/modules/config.d.ts +0 -1
- package/dist/api/modules/config.js +9 -7
- package/dist/api/modules/console.d.ts +16 -4
- package/dist/api/modules/console.js +25 -25
- 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 +2 -2
- package/dist/api/modules/helpmenu.js +4 -7
- package/dist/api/modules/language.d.ts +2 -1
- package/dist/api/modules/language.js +6 -9
- 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 +16 -5
- package/dist/api/modules/progressbar.js +34 -34
- package/dist/api/modules/responder.d.ts +95 -26
- package/dist/api/modules/responder.js +213 -172
- package/dist/api/modules/session.d.ts +10 -1
- package/dist/api/modules/session.js +15 -15
- package/dist/api/modules/startscreen.d.ts +0 -1
- package/dist/api/modules/startscreen.js +3 -6
- 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 +1 -1
- package/src/api/main.ts +1 -1
- package/src/api/modules/base.ts +75 -77
- package/src/api/modules/builder.ts +0 -10
- package/src/api/modules/checker.ts +31 -31
- package/src/api/modules/client.ts +144 -136
- package/src/api/modules/component.ts +11 -7
- package/src/api/modules/config.ts +8 -6
- package/src/api/modules/console.ts +25 -25
- package/src/api/modules/event.ts +6 -10
- package/src/api/modules/fuse.ts +1 -1
- package/src/api/modules/helpmenu.ts +4 -7
- package/src/api/modules/language.ts +6 -9
- 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 +34 -34
- package/src/api/modules/responder.ts +232 -181
- package/src/api/modules/session.ts +14 -14
- package/src/api/modules/startscreen.ts +3 -6
- package/src/api/modules/statistic.ts +4 -7
- package/src/api/modules/worker.ts +3 -3
|
@@ -45,12 +45,21 @@ export type ODSessionTimeoutCallback = (id: string, timeout: "default" | "custom
|
|
|
45
45
|
* You can almost compare it to the PHP session system.
|
|
46
46
|
*/
|
|
47
47
|
export declare class ODSession extends ODManagerData {
|
|
48
|
-
|
|
48
|
+
/**The history of previously generated instance ids. Used to reduce the risk of generating the same id twice. */
|
|
49
|
+
protected idHistory: string[];
|
|
50
|
+
/**The max length of the instance id history. */
|
|
51
|
+
protected maxIdHistoryLength: number;
|
|
49
52
|
/**An array of all the currently active session instances. */
|
|
50
53
|
sessions: ODSessionInstance[];
|
|
51
54
|
/**The default amount of minutes before a session automatically stops. */
|
|
52
55
|
timeoutMinutes: number;
|
|
56
|
+
/**The id of the auto-timeout session checker interval */
|
|
57
|
+
protected intervalId: NodeJS.Timeout;
|
|
58
|
+
/**Listeners for when a session times-out. */
|
|
59
|
+
protected timeoutListeners: ODSessionTimeoutCallback[];
|
|
53
60
|
constructor(id: ODValidId, intervalSeconds?: number);
|
|
61
|
+
/**Create a unique hex id of 8 characters and add it to the instance id history */
|
|
62
|
+
protected createUniqueId(): string;
|
|
54
63
|
/**Stop the global interval that automatically deletes timed-out sessions. (This action can't be reverted!) */
|
|
55
64
|
stopAutoTimeout(): void;
|
|
56
65
|
/**Start a session instance with data. Returns the unique id required to access the session. */
|
|
@@ -33,21 +33,21 @@ export class ODSessionManager extends ODManager {
|
|
|
33
33
|
*/
|
|
34
34
|
export class ODSession extends ODManagerData {
|
|
35
35
|
/**The history of previously generated instance ids. Used to reduce the risk of generating the same id twice. */
|
|
36
|
-
|
|
36
|
+
idHistory = [];
|
|
37
37
|
/**The max length of the instance id history. */
|
|
38
|
-
|
|
38
|
+
maxIdHistoryLength = 500;
|
|
39
39
|
/**An array of all the currently active session instances. */
|
|
40
40
|
sessions = [];
|
|
41
41
|
/**The default amount of minutes before a session automatically stops. */
|
|
42
42
|
timeoutMinutes = 30;
|
|
43
43
|
/**The id of the auto-timeout session checker interval */
|
|
44
|
-
|
|
44
|
+
intervalId;
|
|
45
45
|
/**Listeners for when a session times-out. */
|
|
46
|
-
|
|
46
|
+
timeoutListeners = [];
|
|
47
47
|
constructor(id, intervalSeconds) {
|
|
48
48
|
super(id);
|
|
49
49
|
//create the auto-timeout session checker
|
|
50
|
-
this
|
|
50
|
+
this.intervalId = setInterval(() => {
|
|
51
51
|
const deletableSessions = [];
|
|
52
52
|
//collect all deletable sessions
|
|
53
53
|
this.sessions.forEach((session) => {
|
|
@@ -65,30 +65,30 @@ export class ODSession extends ODManagerData {
|
|
|
65
65
|
const index = this.sessions.findIndex((s) => s.id === session.instance.id);
|
|
66
66
|
this.sessions.splice(index, 1);
|
|
67
67
|
//emit timeout listeners
|
|
68
|
-
this
|
|
68
|
+
this.timeoutListeners.forEach((cb) => cb(session.instance.id, session.reason, session.instance.data, new Date(session.instance.creation)));
|
|
69
69
|
});
|
|
70
70
|
}, ((intervalSeconds) ? (intervalSeconds * 1000) : 60000));
|
|
71
71
|
}
|
|
72
72
|
/**Create a unique hex id of 8 characters and add it to the instance id history */
|
|
73
|
-
|
|
73
|
+
createUniqueId() {
|
|
74
74
|
const hex = crypto.randomBytes(4).toString("hex");
|
|
75
|
-
if (this
|
|
76
|
-
return this
|
|
75
|
+
if (this.idHistory.includes(hex)) {
|
|
76
|
+
return this.createUniqueId();
|
|
77
77
|
}
|
|
78
78
|
else {
|
|
79
|
-
this
|
|
80
|
-
if (this
|
|
81
|
-
this
|
|
79
|
+
this.idHistory.push(hex);
|
|
80
|
+
if (this.idHistory.length > this.maxIdHistoryLength)
|
|
81
|
+
this.idHistory.shift();
|
|
82
82
|
return hex;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
/**Stop the global interval that automatically deletes timed-out sessions. (This action can't be reverted!) */
|
|
86
86
|
stopAutoTimeout() {
|
|
87
|
-
clearInterval(this
|
|
87
|
+
clearInterval(this.intervalId);
|
|
88
88
|
}
|
|
89
89
|
/**Start a session instance with data. Returns the unique id required to access the session. */
|
|
90
90
|
start(data) {
|
|
91
|
-
const id = this
|
|
91
|
+
const id = this.createUniqueId();
|
|
92
92
|
this.sessions.push({
|
|
93
93
|
id, data,
|
|
94
94
|
creation: new Date().getTime(),
|
|
@@ -137,6 +137,6 @@ export class ODSession extends ODManagerData {
|
|
|
137
137
|
}
|
|
138
138
|
/**Listen for a session timeout (default or custom) */
|
|
139
139
|
onTimeout(callback) {
|
|
140
|
-
this
|
|
140
|
+
this.timeoutListeners.push(callback);
|
|
141
141
|
}
|
|
142
142
|
}
|
|
@@ -17,7 +17,6 @@ export type ODStartScreenManagerIdConstraint = Record<string, ODStartScreenCompo
|
|
|
17
17
|
* The startscreen is the part you see when the bot has started up successfully. (e.g. the Open Discord logo, logs, livestatus, flags, ...)
|
|
18
18
|
*/
|
|
19
19
|
export declare class ODStartScreenManager<IdList extends ODStartScreenManagerIdConstraint = ODStartScreenManagerIdConstraint, LiveStatus extends ODLiveStatusManager = ODLiveStatusManager> extends ODManager<ODStartScreenComponent> {
|
|
20
|
-
#private;
|
|
21
20
|
/**Alias to the livestatus manager. */
|
|
22
21
|
livestatus: LiveStatus;
|
|
23
22
|
constructor(debug: ODDebugger, livestatus: LiveStatus);
|
|
@@ -11,13 +11,10 @@ import ansis from "ansis";
|
|
|
11
11
|
* The startscreen is the part you see when the bot has started up successfully. (e.g. the Open Discord logo, logs, livestatus, flags, ...)
|
|
12
12
|
*/
|
|
13
13
|
export class ODStartScreenManager extends ODManager {
|
|
14
|
-
/**Alias to the Open Discord debugger. */
|
|
15
|
-
#debug;
|
|
16
14
|
/**Alias to the livestatus manager. */
|
|
17
15
|
livestatus;
|
|
18
16
|
constructor(debug, livestatus) {
|
|
19
17
|
super(debug, "startscreen component");
|
|
20
|
-
this.#debug = debug;
|
|
21
18
|
this.livestatus = livestatus;
|
|
22
19
|
}
|
|
23
20
|
/**Get all components in sorted order. */
|
|
@@ -37,11 +34,11 @@ export class ODStartScreenManager extends ODManager {
|
|
|
37
34
|
try {
|
|
38
35
|
const renderedText = await component.renderAll(location);
|
|
39
36
|
console.log(renderedText);
|
|
40
|
-
this
|
|
37
|
+
this.debug?.console.debugfile.writeText("[STARTSCREEN] Component: \"" + component.id + "\"\n" + ansis.strip(renderedText));
|
|
41
38
|
}
|
|
42
39
|
catch (e) {
|
|
43
|
-
this
|
|
44
|
-
this
|
|
40
|
+
this.debug?.console.log("Unable to render \"" + component.id + "\" startscreen component!", "error");
|
|
41
|
+
this.debug?.console.debugfile.writeErrorMessage(new ODError(e, "uncaughtException"));
|
|
45
42
|
}
|
|
46
43
|
location++;
|
|
47
44
|
}
|
|
@@ -29,9 +29,10 @@ export type ODStatisticManagerIdConstraint = Record<string, ODStatisticScope>;
|
|
|
29
29
|
* Statistic can be accessed in the individual scopes.
|
|
30
30
|
*/
|
|
31
31
|
export declare class ODStatisticManager<IdList extends ODStatisticManagerIdConstraint = ODStatisticManagerIdConstraint> extends ODManager<ODStatisticScope> {
|
|
32
|
-
#private;
|
|
33
32
|
/**Alias to Open Discord statistics database. */
|
|
34
33
|
database: ODDatabase<ODDatabaseIdConstraint> | null;
|
|
34
|
+
/**All the listeners for the init event. */
|
|
35
|
+
protected initListeners: ODStatisticManagerInitCallback[];
|
|
35
36
|
constructor(debug: ODDebugger);
|
|
36
37
|
/**Select the database to use to read/write all statistics from/to. */
|
|
37
38
|
useDatabase(database: ODDatabase<ODDatabaseIdConstraint>): void;
|
|
@@ -11,22 +11,19 @@ import { ODId, ODManager, ODManagerData, ODSystemError } from "./base.js";
|
|
|
11
11
|
* Statistic can be accessed in the individual scopes.
|
|
12
12
|
*/
|
|
13
13
|
export class ODStatisticManager extends ODManager {
|
|
14
|
-
/**Alias to Open Discord debugger. */
|
|
15
|
-
#debug;
|
|
16
14
|
/**Alias to Open Discord statistics database. */
|
|
17
15
|
database = null;
|
|
18
16
|
/**All the listeners for the init event. */
|
|
19
|
-
|
|
17
|
+
initListeners = [];
|
|
20
18
|
constructor(debug) {
|
|
21
19
|
super(debug, "statistic scope");
|
|
22
|
-
this.#debug = debug;
|
|
23
20
|
}
|
|
24
21
|
/**Select the database to use to read/write all statistics from/to. */
|
|
25
22
|
useDatabase(database) {
|
|
26
23
|
this.database = database;
|
|
27
24
|
}
|
|
28
25
|
add(data, overwrite) {
|
|
29
|
-
data.useDebug(this
|
|
26
|
+
data.useDebug(this.debug, "stat");
|
|
30
27
|
if (this.database)
|
|
31
28
|
data.useDatabase(this.database);
|
|
32
29
|
return super.add(data, overwrite);
|
|
@@ -48,7 +45,7 @@ export class ODStatisticManager extends ODManager {
|
|
|
48
45
|
deletableStats.push(data);
|
|
49
46
|
});
|
|
50
47
|
//do additional deletion
|
|
51
|
-
for (const cb of this
|
|
48
|
+
for (const cb of this.initListeners) {
|
|
52
49
|
await cb(data, deletableStats);
|
|
53
50
|
}
|
|
54
51
|
//delete all deletable statistics
|
|
@@ -69,7 +66,7 @@ export class ODStatisticManager extends ODManager {
|
|
|
69
66
|
}
|
|
70
67
|
/**Run a function when the statistics are initialized. This can be used to clear statistics from users that left the server or tickets which don't exist anymore. */
|
|
71
68
|
onInit(callback) {
|
|
72
|
-
this
|
|
69
|
+
this.initListeners.push(callback);
|
|
73
70
|
}
|
|
74
71
|
get(id) {
|
|
75
72
|
return super.get(id);
|
|
@@ -28,7 +28,8 @@ export declare class ODWorker<Instance, Origin extends string, Params> extends O
|
|
|
28
28
|
* You can register a custom worker in this class to create a message or button.
|
|
29
29
|
*/
|
|
30
30
|
export declare class ODWorkerManager<Instance, Origin extends string, Params, WorkerIds extends string = string> extends ODManager<ODWorker<Instance, Origin, Params>> {
|
|
31
|
-
|
|
31
|
+
/**The order of execution for workers inside this manager. */
|
|
32
|
+
protected priorityOrder: "ascending" | "descending";
|
|
32
33
|
/**The backup worker will be executed when one of the workers fails or cancels execution. */
|
|
33
34
|
backupWorker: ODWorker<{
|
|
34
35
|
reason: "error" | "cancel";
|
|
@@ -32,12 +32,12 @@ export class ODWorker extends ODManagerData {
|
|
|
32
32
|
*/
|
|
33
33
|
export class ODWorkerManager extends ODManager {
|
|
34
34
|
/**The order of execution for workers inside this manager. */
|
|
35
|
-
|
|
35
|
+
priorityOrder;
|
|
36
36
|
/**The backup worker will be executed when one of the workers fails or cancels execution. */
|
|
37
37
|
backupWorker = null;
|
|
38
38
|
constructor(priorityOrder) {
|
|
39
39
|
super();
|
|
40
|
-
this
|
|
40
|
+
this.priorityOrder = priorityOrder;
|
|
41
41
|
}
|
|
42
42
|
/**Get all workers in sorted order. */
|
|
43
43
|
getSortedWorkers(priority) {
|
|
@@ -52,7 +52,7 @@ export class ODWorkerManager extends ODManager {
|
|
|
52
52
|
/**Execute all workers on an instance using the given origin & parameters. */
|
|
53
53
|
async executeWorkers(instance, origin, params) {
|
|
54
54
|
const derefParams = { ...params };
|
|
55
|
-
const workers = this.getSortedWorkers(this
|
|
55
|
+
const workers = this.getSortedWorkers(this.priorityOrder);
|
|
56
56
|
let didCancel = false;
|
|
57
57
|
let didCrash = false;
|
|
58
58
|
for (const worker of workers) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-discord-bots/framework",
|
|
3
3
|
"author": "DJj123dj",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"description": "The core framework of the popular open-source discord bots: Open Ticket & Open Moderation.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
package/src/api/main.ts
CHANGED
|
@@ -150,7 +150,7 @@ export abstract class ODMain implements ODMainManagers {
|
|
|
150
150
|
constructor(managers:ODMainManagers,project:ODProjectType){
|
|
151
151
|
this.project = project
|
|
152
152
|
this.versions = managers.versions
|
|
153
|
-
this.versions.add(ODVersion.fromString("opendiscord:api","v0.3.
|
|
153
|
+
this.versions.add(ODVersion.fromString("opendiscord:api","v0.3.1"))
|
|
154
154
|
this.versions.add(ODVersion.fromString("opendiscord:livestatus","v2.0.0"))
|
|
155
155
|
|
|
156
156
|
this.debugfile = managers.debugfile
|
package/src/api/modules/base.ts
CHANGED
|
@@ -64,18 +64,10 @@ export type ODNoGeneric<T extends Record<string|number|symbol,any>> = {
|
|
|
64
64
|
* You can use this class to assign a unique id when creating configs, databases, languages & more!
|
|
65
65
|
*/
|
|
66
66
|
export class ODId {
|
|
67
|
-
/**The
|
|
68
|
-
|
|
69
|
-
/**The full value of this `ODId` as a `string`. */
|
|
70
|
-
set value(id:string){
|
|
71
|
-
this._change(this.#value,id)
|
|
72
|
-
this.#value = id
|
|
73
|
-
}
|
|
74
|
-
get value(){
|
|
75
|
-
return this.#value
|
|
76
|
-
}
|
|
67
|
+
/**The raw value of this `ODId` as a `string`. */
|
|
68
|
+
private rawValue: string
|
|
77
69
|
/**The change listener for the parent `ODManager` of this `ODId`. */
|
|
78
|
-
|
|
70
|
+
private changeListener: ((oldId:string,newId:string) => void)|null = null
|
|
79
71
|
|
|
80
72
|
constructor(id:ODValidId){
|
|
81
73
|
if (typeof id != "string" && !(id instanceof ODId)) throw new ODSystemError("Invalid constructor parameter => id:ODValidId")
|
|
@@ -91,37 +83,45 @@ export class ODId {
|
|
|
91
83
|
}
|
|
92
84
|
})
|
|
93
85
|
|
|
94
|
-
if (result.length > 0) this
|
|
86
|
+
if (result.length > 0) this.rawValue = result.join("")
|
|
95
87
|
else throw new ODSystemError("invalid ID at 'new ODID(id: "+id+")'")
|
|
96
88
|
}else{
|
|
97
89
|
//id is ODId
|
|
98
|
-
this
|
|
90
|
+
this.rawValue = id.rawValue
|
|
99
91
|
}
|
|
100
92
|
}
|
|
101
93
|
|
|
94
|
+
/**The full value of this `ODId` as a `string`. */
|
|
95
|
+
set value(id:string){
|
|
96
|
+
this._change(this.rawValue,id)
|
|
97
|
+
this.rawValue = id
|
|
98
|
+
}
|
|
99
|
+
get value(){
|
|
100
|
+
return this.rawValue
|
|
101
|
+
}
|
|
102
102
|
/**Returns a string representation of this id. (same as `this.value`) */
|
|
103
103
|
toString(){
|
|
104
|
-
return this
|
|
104
|
+
return this.rawValue
|
|
105
105
|
}
|
|
106
106
|
/**The namespace of the id before `:`. (e.g. `opendiscord` for `opendiscord:autoclose-enabled`) */
|
|
107
107
|
getNamespace(){
|
|
108
|
-
const splitted = this
|
|
108
|
+
const splitted = this.rawValue.split(":")
|
|
109
109
|
if (splitted.length > 1) return splitted[0]
|
|
110
110
|
else return ""
|
|
111
111
|
}
|
|
112
112
|
/**The identifier of the id after `:`. (e.g. `autoclose-enabled` for `opendiscord:autoclose-enabled`) */
|
|
113
113
|
getIdentifier(){
|
|
114
|
-
const splitted = this
|
|
114
|
+
const splitted = this.rawValue.split(":")
|
|
115
115
|
if (splitted.length > 1){
|
|
116
116
|
splitted.shift()
|
|
117
117
|
return splitted.join(":")
|
|
118
|
-
}else return this
|
|
118
|
+
}else return this.rawValue
|
|
119
119
|
}
|
|
120
120
|
/**Trigger an `onChange()` event in the parent `ODManager` of this class. */
|
|
121
121
|
protected _change(oldId:string,newId:string){
|
|
122
|
-
if (this
|
|
122
|
+
if (this.changeListener){
|
|
123
123
|
try{
|
|
124
|
-
this
|
|
124
|
+
this.changeListener(oldId,newId)
|
|
125
125
|
}catch(err){
|
|
126
126
|
process.emit("uncaughtException",err)
|
|
127
127
|
throw new ODSystemError("Failed to execute _change() callback!")
|
|
@@ -130,7 +130,7 @@ export class ODId {
|
|
|
130
130
|
}
|
|
131
131
|
/****(❌ SYSTEM ONLY!!)** Set the callback executed when a value inside this class changes. */
|
|
132
132
|
changed(callback:((oldId:string,newId:string) => void)|null){
|
|
133
|
-
this
|
|
133
|
+
this.changeListener = callback
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
@@ -141,13 +141,13 @@ export class ODId {
|
|
|
141
141
|
* You can use this class when extending your own `ODManager`
|
|
142
142
|
*/
|
|
143
143
|
export abstract class ODManagerChangeHelper {
|
|
144
|
-
|
|
144
|
+
private changeListener: (() => void)|null = null
|
|
145
145
|
|
|
146
146
|
/**Trigger an `onChange()` event in the parent `ODManager` of this class. */
|
|
147
147
|
protected _change(){
|
|
148
|
-
if (this
|
|
148
|
+
if (this.changeListener){
|
|
149
149
|
try{
|
|
150
|
-
this
|
|
150
|
+
this.changeListener()
|
|
151
151
|
}catch(err){
|
|
152
152
|
process.emit("uncaughtException",err)
|
|
153
153
|
throw new ODSystemError("Failed to execute _change() callback!")
|
|
@@ -156,7 +156,7 @@ export abstract class ODManagerChangeHelper {
|
|
|
156
156
|
}
|
|
157
157
|
/****(❌ SYSTEM ONLY!!)** Set the callback executed when a value inside this class changes. */
|
|
158
158
|
changed(callback:(() => void)|null){
|
|
159
|
-
this
|
|
159
|
+
this.changeListener = callback
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
@@ -198,22 +198,22 @@ export type ODManagerAddCallback<DataType extends ODManagerData> = (data:DataTyp
|
|
|
198
198
|
*/
|
|
199
199
|
export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHelper {
|
|
200
200
|
/**Alias to Open Discord debugger. */
|
|
201
|
-
|
|
201
|
+
protected debug?: ODDebugger
|
|
202
202
|
/**The message to send when debugging this manager. */
|
|
203
|
-
|
|
203
|
+
protected debugname?: string
|
|
204
204
|
/**The map storing all data classes in this manager. */
|
|
205
|
-
|
|
205
|
+
private data: Map<string,DataType> = new Map()
|
|
206
206
|
/**An array storing all listeners when data is added. */
|
|
207
|
-
|
|
207
|
+
private addListeners: ODManagerAddCallback<DataType>[] = []
|
|
208
208
|
/**An array storing all listeners when data has changed. */
|
|
209
|
-
|
|
209
|
+
private changeListeners: ODManagerCallback<DataType>[] = []
|
|
210
210
|
/**An array storing all listeners when data is removed. */
|
|
211
|
-
|
|
211
|
+
private removeListeners: ODManagerCallback<DataType>[] = []
|
|
212
212
|
|
|
213
213
|
constructor(debug?:ODDebugger, debugname?:string){
|
|
214
|
-
|
|
215
|
-
this
|
|
216
|
-
this
|
|
214
|
+
super()
|
|
215
|
+
this.debug = debug
|
|
216
|
+
this.debugname = debugname
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
/**Add data to the manager. The `ODId` in the data class will be used as identifier! You can optionally select to overwrite existing data!*/
|
|
@@ -228,22 +228,22 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
228
228
|
|
|
229
229
|
//add listener for data id change => transfer data within manager
|
|
230
230
|
data.id.changed((oldId,newId) => {
|
|
231
|
-
this
|
|
232
|
-
this
|
|
231
|
+
this.data.delete(oldId)
|
|
232
|
+
this.data.set(newId,data)
|
|
233
233
|
})
|
|
234
234
|
|
|
235
235
|
//add data
|
|
236
236
|
let didOverwrite: boolean
|
|
237
|
-
if (this
|
|
238
|
-
if (!overwrite) throw new ODSystemError("Id '"+data.id.value+"' already exists in "+this
|
|
239
|
-
this
|
|
237
|
+
if (this.data.has(data.id.value)){
|
|
238
|
+
if (!overwrite) throw new ODSystemError("Id '"+data.id.value+"' already exists in "+this.debugname+" manager. Use 'overwrite:true' to allow overwriting!")
|
|
239
|
+
this.data.set(data.id.value,data)
|
|
240
240
|
didOverwrite = true
|
|
241
|
-
if (this
|
|
241
|
+
if (this.debug) this.debug.debug("Added new "+this.debugname+" to manager",[{key:"id",value:data.id.value},{key:"overwrite",value:"true"}])
|
|
242
242
|
|
|
243
243
|
}else{
|
|
244
|
-
this
|
|
244
|
+
this.data.set(data.id.value,data)
|
|
245
245
|
didOverwrite = false
|
|
246
|
-
if (this
|
|
246
|
+
if (this.debug) this.debug.debug("Added new "+this.debugname+" to manager",[{key:"id",value:data.id.value},{key:"overwrite",value:"false"}])
|
|
247
247
|
|
|
248
248
|
}
|
|
249
249
|
|
|
@@ -251,7 +251,7 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
251
251
|
data.changed(() => {
|
|
252
252
|
//notify change in upper-manager (because data in this manager changed)
|
|
253
253
|
this._change()
|
|
254
|
-
this
|
|
254
|
+
this.changeListeners.forEach((cb) => {
|
|
255
255
|
try{
|
|
256
256
|
cb(data)
|
|
257
257
|
}catch(err){
|
|
@@ -261,7 +261,7 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
261
261
|
})
|
|
262
262
|
|
|
263
263
|
//emit add listeners
|
|
264
|
-
this
|
|
264
|
+
this.addListeners.forEach((cb) => {
|
|
265
265
|
try{
|
|
266
266
|
cb(data,didOverwrite)
|
|
267
267
|
}catch(err){
|
|
@@ -277,21 +277,21 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
277
277
|
/**Get data that matches the `ODId`. Returns the found data.*/
|
|
278
278
|
get(id:ODValidId): DataType|null {
|
|
279
279
|
const newId = new ODId(id)
|
|
280
|
-
const data = this
|
|
280
|
+
const data = this.data.get(newId.value)
|
|
281
281
|
if (data) return data
|
|
282
282
|
else return null
|
|
283
283
|
}
|
|
284
284
|
/**Remove data that matches the `ODId`. Returns the removed data. */
|
|
285
285
|
remove(id:ODValidId): DataType|null {
|
|
286
286
|
const newId = new ODId(id)
|
|
287
|
-
const data = this
|
|
287
|
+
const data = this.data.get(newId.value)
|
|
288
288
|
|
|
289
289
|
if (!data){
|
|
290
|
-
if (this
|
|
290
|
+
if (this.debug) this.debug.debug("Removed "+this.debugname+" from manager",[{key:"id",value:newId.value},{key:"found",value:"false"}])
|
|
291
291
|
return null
|
|
292
292
|
}else{
|
|
293
|
-
this
|
|
294
|
-
if (this
|
|
293
|
+
this.data.delete(newId.value)
|
|
294
|
+
if (this.debug) this.debug.debug("Removed "+this.debugname+" from manager",[{key:"id",value:newId.value},{key:"found",value:"true"}])
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
//remove all listeners
|
|
@@ -299,7 +299,7 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
299
299
|
data.changed(null)
|
|
300
300
|
|
|
301
301
|
//emit remove listeners
|
|
302
|
-
this
|
|
302
|
+
this.removeListeners.forEach((cb) => {
|
|
303
303
|
try{
|
|
304
304
|
cb(data)
|
|
305
305
|
}catch(err){
|
|
@@ -315,28 +315,28 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
315
315
|
/**Check if data that matches the `ODId` exists. Returns a boolean. */
|
|
316
316
|
exists(id:ODValidId): boolean {
|
|
317
317
|
const newId = new ODId(id)
|
|
318
|
-
if (this
|
|
318
|
+
if (this.data.has(newId.value)) return true
|
|
319
319
|
else return false
|
|
320
320
|
}
|
|
321
321
|
/**Get all data inside this manager*/
|
|
322
322
|
getAll(): DataType[] {
|
|
323
|
-
return Array.from(this
|
|
323
|
+
return Array.from(this.data.values())
|
|
324
324
|
}
|
|
325
325
|
/**Get all data that matches inside the filter function*/
|
|
326
326
|
getFiltered(predicate:(value:DataType, index:number, array:DataType[]) => unknown): DataType[] {
|
|
327
|
-
return Array.from(this
|
|
327
|
+
return Array.from(this.data.values()).filter(predicate)
|
|
328
328
|
}
|
|
329
329
|
/**Get all data where the `ODId` matches the provided RegExp. */
|
|
330
330
|
getRegex(regex:RegExp): DataType[] {
|
|
331
|
-
return Array.from(this
|
|
331
|
+
return Array.from(this.data.values()).filter((data) => regex.test(data.id.value))
|
|
332
332
|
}
|
|
333
333
|
/**Get the length/size/amount of the data inside this manager. */
|
|
334
334
|
getLength(){
|
|
335
|
-
return this
|
|
335
|
+
return this.data.size
|
|
336
336
|
}
|
|
337
337
|
/**Get a list of all the ids inside this manager*/
|
|
338
338
|
getIds(): ODId[] {
|
|
339
|
-
const ids = Array.from(this
|
|
339
|
+
const ids = Array.from(this.data.keys())
|
|
340
340
|
return ids.map((id) => new ODId(id))
|
|
341
341
|
}
|
|
342
342
|
/**Run an iterator over all data in this manager. This method also supports async-await behaviour!*/
|
|
@@ -347,20 +347,20 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
347
347
|
}
|
|
348
348
|
/**Use the Open Discord debugger in this manager for logs*/
|
|
349
349
|
useDebug(debug?:ODDebugger, debugname?:string){
|
|
350
|
-
this
|
|
351
|
-
this
|
|
350
|
+
this.debug = debug
|
|
351
|
+
this.debugname = debugname
|
|
352
352
|
}
|
|
353
353
|
/**Listen for when data is added to this manager. */
|
|
354
354
|
onAdd(callback:ODManagerAddCallback<DataType>){
|
|
355
|
-
this
|
|
355
|
+
this.addListeners.push(callback)
|
|
356
356
|
}
|
|
357
357
|
/**Listen for when data is changed in this manager. */
|
|
358
358
|
onChange(callback:ODManagerCallback<DataType>){
|
|
359
|
-
this
|
|
359
|
+
this.changeListeners.push(callback)
|
|
360
360
|
}
|
|
361
361
|
/**Listen for when data is removed from this manager. */
|
|
362
362
|
onRemove(callback:ODManagerCallback<DataType>){
|
|
363
|
-
this
|
|
363
|
+
this.removeListeners.push(callback)
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
366
|
|
|
@@ -372,14 +372,11 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
|
|
|
372
372
|
*/
|
|
373
373
|
export class ODManagerWithSafety<DataType extends ODManagerData> extends ODManager<DataType> {
|
|
374
374
|
/**The function that creates backup data returned in `getSafe()` when an id is missing in this manager. */
|
|
375
|
-
|
|
376
|
-
/** Temporary storage for manager debug name. */
|
|
377
|
-
#debugname: string
|
|
375
|
+
protected backupGenerator: () => DataType
|
|
378
376
|
|
|
379
|
-
constructor(
|
|
377
|
+
constructor(backupGenerator:() => DataType, debug?:ODDebugger, debugname?:string){
|
|
380
378
|
super(debug,debugname)
|
|
381
|
-
this
|
|
382
|
-
this.#debugname = debugname ?? "unknown"
|
|
379
|
+
this.backupGenerator = backupGenerator
|
|
383
380
|
}
|
|
384
381
|
|
|
385
382
|
/**Get data that matches the `ODId`. Returns the backup data when not found.
|
|
@@ -390,8 +387,8 @@ export class ODManagerWithSafety<DataType extends ODManagerData> extends ODManag
|
|
|
390
387
|
const newId = new ODId(id)
|
|
391
388
|
const data = super.get(id)
|
|
392
389
|
if (!data){
|
|
393
|
-
process.emit("uncaughtException",new ODSystemError("ODManagerWithSafety:getSafe(\""+newId.value+"\") => Unknown Id => Used backup data ("+this
|
|
394
|
-
return this
|
|
390
|
+
process.emit("uncaughtException",new ODSystemError("ODManagerWithSafety:getSafe(\""+newId.value+"\") => Unknown Id => Used backup data ("+this.debugname+" manager)"))
|
|
391
|
+
return this.backupGenerator()
|
|
395
392
|
}
|
|
396
393
|
else return data
|
|
397
394
|
}
|
|
@@ -578,19 +575,19 @@ export class ODVersionMigration {
|
|
|
578
575
|
/**The version to migrate data to */
|
|
579
576
|
version: ODVersion
|
|
580
577
|
/**The migration function */
|
|
581
|
-
|
|
578
|
+
private migrateFunc: () => void|Promise<void>
|
|
582
579
|
/**The migration function */
|
|
583
|
-
|
|
580
|
+
private migrateAfterInitFunc: () => void|Promise<void>
|
|
584
581
|
|
|
585
|
-
constructor(version:ODVersion,
|
|
582
|
+
constructor(version:ODVersion,migrateFunc:() => void|Promise<void>,migrateAfterInitFunc:() => void|Promise<void>){
|
|
586
583
|
this.version = version
|
|
587
|
-
this
|
|
588
|
-
this
|
|
584
|
+
this.migrateFunc = migrateFunc
|
|
585
|
+
this.migrateAfterInitFunc = migrateAfterInitFunc
|
|
589
586
|
}
|
|
590
587
|
/**Run this version migration as a plugin. Returns `false` when something goes wrong. */
|
|
591
588
|
async migrate(): Promise<boolean> {
|
|
592
589
|
try{
|
|
593
|
-
await this
|
|
590
|
+
await this.migrateFunc()
|
|
594
591
|
return true
|
|
595
592
|
}catch(err){
|
|
596
593
|
process.emit("uncaughtException",err)
|
|
@@ -600,7 +597,7 @@ export class ODVersionMigration {
|
|
|
600
597
|
/**Run this version migration as a plugin (after other plugins have loaded). Returns `false` when something goes wrong. */
|
|
601
598
|
async migrateAfterInit(): Promise<boolean> {
|
|
602
599
|
try{
|
|
603
|
-
await this
|
|
600
|
+
await this.migrateAfterInitFunc()
|
|
604
601
|
return true
|
|
605
602
|
}catch(err){
|
|
606
603
|
process.emit("uncaughtException",err)
|
|
@@ -742,7 +739,7 @@ export class ODEnvHelper {
|
|
|
742
739
|
if (typeof customEnvPath != "undefined" && typeof customEnvPath != "string") throw new ODSystemError("Invalid constructor parameter => customEnvPath?:string")
|
|
743
740
|
|
|
744
741
|
const path = customEnvPath ? customEnvPath : ".env"
|
|
745
|
-
this.dotenv = fs.existsSync(path) ? this
|
|
742
|
+
this.dotenv = fs.existsSync(path) ? this.readDotEnv(fs.readFileSync(path)) : {}
|
|
746
743
|
this.env = process.env
|
|
747
744
|
}
|
|
748
745
|
|
|
@@ -765,7 +762,8 @@ export class ODEnvHelper {
|
|
|
765
762
|
//THIS CODE IS COPIED FROM THE DODENV-LIB
|
|
766
763
|
//Repo: https://github.com/motdotla/dotenv
|
|
767
764
|
//Source: https://github.com/motdotla/dotenv/blob/master/lib/main.js#L12
|
|
768
|
-
|
|
765
|
+
//All rights go to the original authors of the dotenv library.
|
|
766
|
+
protected readDotEnv(src:Buffer){
|
|
769
767
|
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
|
|
770
768
|
const obj: Record<string,any> = {}
|
|
771
769
|
|
|
@@ -1295,16 +1295,6 @@ export interface ODMessageBuildResult {
|
|
|
1295
1295
|
ephemeral:boolean
|
|
1296
1296
|
}
|
|
1297
1297
|
|
|
1298
|
-
/**## ODMessageBuildSentResult `interface`
|
|
1299
|
-
* This interface contains the result from a sent built message. This can be used to edit, view & save the message that got created.
|
|
1300
|
-
*/
|
|
1301
|
-
export interface ODMessageBuildSentResult<InGuild extends boolean> {
|
|
1302
|
-
/**Did the message get sent successfully? */
|
|
1303
|
-
success:boolean,
|
|
1304
|
-
/**The message that got sent. */
|
|
1305
|
-
message:discord.Message<InGuild>|null
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
1298
|
/**## ODMessageInstance `class`
|
|
1309
1299
|
* This is an Open Discord message instance.
|
|
1310
1300
|
*
|