@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,665 @@
|
|
|
1
|
+
///////////////////////////////////////
|
|
2
|
+
//CONSOLE MODULE
|
|
3
|
+
///////////////////////////////////////
|
|
4
|
+
import { ODHTTPGetRequest, ODVersion, ODSystemError, ODPluginError, ODManager, ODManagerData, ODValidId } from "./base"
|
|
5
|
+
import { ODMain } from "../main"
|
|
6
|
+
import nodepath from "path"
|
|
7
|
+
import fs from "fs"
|
|
8
|
+
import ansis from "ansis"
|
|
9
|
+
|
|
10
|
+
/**## ODValidConsoleColor `type`
|
|
11
|
+
* This is a collection of all the supported console colors within Open Discord.
|
|
12
|
+
*/
|
|
13
|
+
export type ODValidConsoleColor = "white"|"red"|"yellow"|"green"|"blue"|"gray"|"cyan"|"magenta"
|
|
14
|
+
|
|
15
|
+
/**## ODConsoleMessageParam `type`
|
|
16
|
+
* This interface contains all data required for a console log parameter within Open Discord.
|
|
17
|
+
*/
|
|
18
|
+
export interface ODConsoleMessageParam {
|
|
19
|
+
/**The key of this parameter. */
|
|
20
|
+
key:string,
|
|
21
|
+
/**The value of this parameter. */
|
|
22
|
+
value:string,
|
|
23
|
+
/**When enabled, this parameter will only be shown in the debug file. */
|
|
24
|
+
hidden?:boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**## ODConsoleMessage `class`
|
|
28
|
+
* This is an Open Discord console message.
|
|
29
|
+
*
|
|
30
|
+
* It is used to create beautiful & styled logs in the console with a prefix, message & parameters.
|
|
31
|
+
* It also has full color support using `ansis` and parameters are parsed for you!
|
|
32
|
+
*/
|
|
33
|
+
export class ODConsoleMessage {
|
|
34
|
+
/**The main message sent in the console */
|
|
35
|
+
message: string
|
|
36
|
+
/**An array of all the parameters in this message */
|
|
37
|
+
params: ODConsoleMessageParam[]
|
|
38
|
+
/**The prefix of this message (!uppercase recommended!) */
|
|
39
|
+
prefix: string
|
|
40
|
+
/**The color of the prefix of this message */
|
|
41
|
+
color: ODValidConsoleColor
|
|
42
|
+
|
|
43
|
+
constructor(message:string, prefix:string, color:ODValidConsoleColor, params?:ODConsoleMessageParam[]){
|
|
44
|
+
this.message = message
|
|
45
|
+
this.params = params ? params : []
|
|
46
|
+
this.prefix = prefix
|
|
47
|
+
|
|
48
|
+
if (["white","red","yellow","green","blue","gray","cyan","magenta"].includes(color)){
|
|
49
|
+
this.color = color
|
|
50
|
+
}else{
|
|
51
|
+
this.color = "white"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**Render this message to the console using `console.log`! Returns `false` when something went wrong. */
|
|
55
|
+
render(){
|
|
56
|
+
try {
|
|
57
|
+
const prefixcolor = ansis[this.color]
|
|
58
|
+
|
|
59
|
+
const paramsstring = " "+this.createParamsString("gray")
|
|
60
|
+
const message = prefixcolor("["+this.prefix+"] ")+this.message
|
|
61
|
+
|
|
62
|
+
console.log(message+paramsstring)
|
|
63
|
+
return true
|
|
64
|
+
}catch{
|
|
65
|
+
return false
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**Create a more-detailed, non-colored version of this message to store it in the `debug.txt` file! */
|
|
69
|
+
toDebugString(){
|
|
70
|
+
const pstrings: string[] = []
|
|
71
|
+
this.params.forEach((p) => {
|
|
72
|
+
pstrings.push(p.key+": "+p.value)
|
|
73
|
+
})
|
|
74
|
+
const pstring = (pstrings.length > 0) ? " ("+pstrings.join(", ")+")" : ""
|
|
75
|
+
const date = new Date()
|
|
76
|
+
const dstring = `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
|
|
77
|
+
return `[${dstring} ${this.prefix}] ${this.message}${pstring}`
|
|
78
|
+
}
|
|
79
|
+
/**Render the parameters of this message in a specific color. */
|
|
80
|
+
createParamsString(color:ODValidConsoleColor){
|
|
81
|
+
let validcolor: ODValidConsoleColor = "white"
|
|
82
|
+
if (["white","red","yellow","green","blue","gray","cyan","magenta"].includes(color)){
|
|
83
|
+
validcolor = color
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const pstrings: string[] = []
|
|
87
|
+
this.params.forEach((p) => {
|
|
88
|
+
if (!p.hidden) pstrings.push(p.key+": "+p.value)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return (pstrings.length > 0) ? ansis[validcolor](" ("+pstrings.join(", ")+")") : ""
|
|
92
|
+
}
|
|
93
|
+
/**Set the message */
|
|
94
|
+
setMessage(message:string){
|
|
95
|
+
this.message = message
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
/**Set the params */
|
|
99
|
+
setParams(params:ODConsoleMessageParam[]){
|
|
100
|
+
this.params = params
|
|
101
|
+
return this
|
|
102
|
+
}
|
|
103
|
+
/**Set the prefix */
|
|
104
|
+
setPrefix(prefix:string){
|
|
105
|
+
this.prefix = prefix
|
|
106
|
+
return this
|
|
107
|
+
}
|
|
108
|
+
/**Set the prefix color */
|
|
109
|
+
setColor(color:ODValidConsoleColor){
|
|
110
|
+
if (["white","red","yellow","green","blue","gray","cyan","magenta"].includes(color)){
|
|
111
|
+
this.color = color
|
|
112
|
+
}else{
|
|
113
|
+
this.color = "white"
|
|
114
|
+
}
|
|
115
|
+
return this
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**## ODConsoleInfoMessage `class`
|
|
120
|
+
* This is an Open Discord console info message.
|
|
121
|
+
*
|
|
122
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "INFO" messages!
|
|
123
|
+
*/
|
|
124
|
+
export class ODConsoleInfoMessage extends ODConsoleMessage {
|
|
125
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
126
|
+
super(message,"INFO","blue",params)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**## ODConsoleSystemMessage `class`
|
|
131
|
+
* This is an Open Discord console system message.
|
|
132
|
+
*
|
|
133
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "SYSTEM" messages!
|
|
134
|
+
*/
|
|
135
|
+
export class ODConsoleSystemMessage extends ODConsoleMessage {
|
|
136
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
137
|
+
super(message,"SYSTEM","green",params)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**## ODConsolePluginMessage `class`
|
|
142
|
+
* This is an Open Discord console plugin message.
|
|
143
|
+
*
|
|
144
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "PLUGIN" messages!
|
|
145
|
+
*/
|
|
146
|
+
export class ODConsolePluginMessage extends ODConsoleMessage {
|
|
147
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
148
|
+
super(message,"PLUGIN","magenta",params)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**## ODConsoleDebugMessage `class`
|
|
153
|
+
* This is an Open Discord console debug message.
|
|
154
|
+
*
|
|
155
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "DEBUG" messages!
|
|
156
|
+
*/
|
|
157
|
+
export class ODConsoleDebugMessage extends ODConsoleMessage {
|
|
158
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
159
|
+
super(message,"DEBUG","cyan",params)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**## ODConsoleWarningMessage `class`
|
|
164
|
+
* This is an Open Discord console warning message.
|
|
165
|
+
*
|
|
166
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "WARNING" messages!
|
|
167
|
+
*/
|
|
168
|
+
export class ODConsoleWarningMessage extends ODConsoleMessage {
|
|
169
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
170
|
+
super(message,"WARNING","yellow",params)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**## ODConsoleErrorMessage `class`
|
|
175
|
+
* This is an Open Discord console error message.
|
|
176
|
+
*
|
|
177
|
+
* It is the same as a normal `ODConsoleMessage`, but it has a predefined prefix & color scheme for the "ERROR" messages!
|
|
178
|
+
*/
|
|
179
|
+
export class ODConsoleErrorMessage extends ODConsoleMessage {
|
|
180
|
+
constructor(message:string,params?:ODConsoleMessageParam[]){
|
|
181
|
+
super(message,"ERROR","red",params)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**## ODError `class`
|
|
186
|
+
* This is an Open Discord error.
|
|
187
|
+
*
|
|
188
|
+
* It is used to render and log Node.js errors & crashes in a styled way to the console & `debug.txt` file!
|
|
189
|
+
*/
|
|
190
|
+
export class ODError {
|
|
191
|
+
/**The original error that this class wraps around */
|
|
192
|
+
error: Error|ODSystemError|ODPluginError
|
|
193
|
+
/**The origin of the original error */
|
|
194
|
+
origin: NodeJS.UncaughtExceptionOrigin
|
|
195
|
+
|
|
196
|
+
constructor(error:Error|ODSystemError|ODPluginError, origin:NodeJS.UncaughtExceptionOrigin){
|
|
197
|
+
this.error = error
|
|
198
|
+
this.origin = origin
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**Render this error to the console using `console.log`! Returns `false` when something went wrong. */
|
|
202
|
+
render(){
|
|
203
|
+
try {
|
|
204
|
+
let prefix = (this.error["_ODErrorType"] == "plugin") ? "PLUGIN ERROR" : ((this.error["_ODErrorType"] == "system") ? "OPENTICKET ERROR" : "UNKNOWN ERROR")
|
|
205
|
+
//title
|
|
206
|
+
console.log(ansis.red("["+prefix+"]: ")+this.error.message+" | origin: "+this.origin)
|
|
207
|
+
//stack trace
|
|
208
|
+
if (this.error.stack) console.log(ansis.gray(this.error.stack))
|
|
209
|
+
//additional message
|
|
210
|
+
if (this.error["_ODErrorType"] == "plugin") console.log(ansis.red.bold("\nPlease report this error to the plugin developer and help us create a more stable plugin!"))
|
|
211
|
+
else console.log(ansis.red.bold("\nPlease report this error to our discord server and help us create a more stable bot!"))
|
|
212
|
+
console.log(ansis.red("Also send the "+ansis.cyan.bold("debug.txt")+" file! It would help a lot!\n"))
|
|
213
|
+
return true
|
|
214
|
+
}catch{
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**Create a more-detailed, non-colored version of this error to store it in the `debug.txt` file! */
|
|
219
|
+
toDebugString(){
|
|
220
|
+
return "[UNKNOWN OD ERROR]: "+this.error.message+" | origin: "+this.origin+"\n"+this.error.stack
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**## ODConsoleMessageTypes `type`
|
|
225
|
+
* This is a collection of all the default console message types within Open Discord.
|
|
226
|
+
*/
|
|
227
|
+
export type ODConsoleMessageTypes = "info"|"system"|"plugin"|"debug"|"warning"|"error"
|
|
228
|
+
|
|
229
|
+
/**## ODConsoleManager `class`
|
|
230
|
+
* This is the Open Discord console manager.
|
|
231
|
+
*
|
|
232
|
+
* It handles the entire console system of Open Discord. It's also the place where you need to log `ODConsoleMessage`'s.
|
|
233
|
+
* This manager keeps a short history of messages sent to the console which is configurable by plugins.
|
|
234
|
+
*
|
|
235
|
+
* The debug file (`debug.txt`) is handled in a sub-manager!
|
|
236
|
+
*/
|
|
237
|
+
export class ODConsoleManager {
|
|
238
|
+
/**The history of `ODConsoleMessage`'s and `ODError`'s since startup */
|
|
239
|
+
history: (ODConsoleMessage|ODError)[] = []
|
|
240
|
+
/**The max length of the history. The oldest messages will be removed when over the limit */
|
|
241
|
+
historylength = 100
|
|
242
|
+
/**An alias to the debugfile manager. (`debug.txt`) */
|
|
243
|
+
debugfile: ODDebugFileManager
|
|
244
|
+
/**Is silent mode enabled? */
|
|
245
|
+
silent: boolean = false
|
|
246
|
+
|
|
247
|
+
constructor(historylength:number, debugfile:ODDebugFileManager){
|
|
248
|
+
this.historylength = historylength
|
|
249
|
+
this.debugfile = debugfile
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**Log a message to the console ... But in the Open Discord way :) */
|
|
253
|
+
log(message:ODConsoleMessage): void
|
|
254
|
+
log(message:ODError): void
|
|
255
|
+
log(message:string, type?:ODConsoleMessageTypes, params?:ODConsoleMessageParam[]): void
|
|
256
|
+
log(message:ODConsoleMessage|ODError|string, type?:ODConsoleMessageTypes, params?:ODConsoleMessageParam[]){
|
|
257
|
+
if (message instanceof ODConsoleMessage){
|
|
258
|
+
if (!this.silent) message.render()
|
|
259
|
+
if (this.debugfile) this.debugfile.writeConsoleMessage(message)
|
|
260
|
+
this.history.push(message)
|
|
261
|
+
|
|
262
|
+
}else if (message instanceof ODError){
|
|
263
|
+
if (!this.silent) message.render()
|
|
264
|
+
if (this.debugfile) this.debugfile.writeErrorMessage(message)
|
|
265
|
+
this.history.push(message)
|
|
266
|
+
|
|
267
|
+
}else if (["string","number","boolean","object"].includes(typeof message)){
|
|
268
|
+
let newMessage: ODConsoleMessage
|
|
269
|
+
if (type == "info") newMessage = new ODConsoleInfoMessage(message,params)
|
|
270
|
+
else if (type == "system") newMessage = new ODConsoleSystemMessage(message,params)
|
|
271
|
+
else if (type == "plugin") newMessage = new ODConsolePluginMessage(message,params)
|
|
272
|
+
else if (type == "debug") newMessage = new ODConsoleDebugMessage(message,params)
|
|
273
|
+
else if (type == "warning") newMessage = new ODConsoleWarningMessage(message,params)
|
|
274
|
+
else if (type == "error") newMessage = new ODConsoleErrorMessage(message,params)
|
|
275
|
+
else newMessage = new ODConsoleSystemMessage(message,params)
|
|
276
|
+
|
|
277
|
+
if (!this.silent) newMessage.render()
|
|
278
|
+
if (this.debugfile) this.debugfile.writeConsoleMessage(newMessage)
|
|
279
|
+
this.history.push(newMessage)
|
|
280
|
+
}
|
|
281
|
+
this.#purgeHistory()
|
|
282
|
+
}
|
|
283
|
+
/**Shorten the history when it exceeds the max history length! */
|
|
284
|
+
#purgeHistory(){
|
|
285
|
+
if (this.history.length > this.historylength) this.history.shift()
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**## ODDebugFileManager `class`
|
|
290
|
+
* This is the Open Discord debug file manager.
|
|
291
|
+
*
|
|
292
|
+
* It manages the Open Discord debug file (`debug.txt`) which keeps a history of all system logs.
|
|
293
|
+
* There are even internal logs that aren't logged to the console which are available in this file!
|
|
294
|
+
*
|
|
295
|
+
* Using this class, you can change the max length of this file and some other cool things!
|
|
296
|
+
*/
|
|
297
|
+
export class ODDebugFileManager {
|
|
298
|
+
/**The path to the debugfile (`./debug.txt` by default) */
|
|
299
|
+
path: string
|
|
300
|
+
/**The filename of the debugfile (`debug.txt` by default) */
|
|
301
|
+
filename: string
|
|
302
|
+
/**The current version of the bot used in the debug file. */
|
|
303
|
+
version: ODVersion
|
|
304
|
+
/**The max length of the debug file. */
|
|
305
|
+
maxlines: number
|
|
306
|
+
|
|
307
|
+
constructor(path:string, filename:string, maxlines:number, version:ODVersion){
|
|
308
|
+
this.path = nodepath.join(path,filename)
|
|
309
|
+
this.filename = filename
|
|
310
|
+
this.version = version
|
|
311
|
+
this.maxlines = maxlines
|
|
312
|
+
|
|
313
|
+
this.#writeStartupStats()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**Check if the debug file exists */
|
|
317
|
+
#existsDebugFile(){
|
|
318
|
+
return fs.existsSync(this.path)
|
|
319
|
+
}
|
|
320
|
+
/**Read from the debug file */
|
|
321
|
+
#readDebugFile(){
|
|
322
|
+
if (this.#existsDebugFile()){
|
|
323
|
+
try {
|
|
324
|
+
return fs.readFileSync(this.path).toString()
|
|
325
|
+
}catch{
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
328
|
+
}else{
|
|
329
|
+
return false
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**Write to the debug file and shorten it when needed. */
|
|
333
|
+
#writeDebugFile(text:string){
|
|
334
|
+
const currenttext = this.#readDebugFile()
|
|
335
|
+
if (currenttext){
|
|
336
|
+
const splitted = currenttext.split("\n")
|
|
337
|
+
|
|
338
|
+
if (splitted.length+text.split("\n").length > this.maxlines){
|
|
339
|
+
splitted.splice(7,(text.split("\n").length))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
splitted.push(text)
|
|
343
|
+
fs.writeFileSync(this.path,splitted.join("\n"))
|
|
344
|
+
}else{
|
|
345
|
+
//write new file:
|
|
346
|
+
const newtext = this.#createStatsText()+text
|
|
347
|
+
fs.writeFileSync(this.path,newtext)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**Generate the stats/header of the debug file (containing the version) */
|
|
351
|
+
#createStatsText(){
|
|
352
|
+
const date = new Date()
|
|
353
|
+
const dstring = `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
|
|
354
|
+
return [
|
|
355
|
+
"=========================",
|
|
356
|
+
"OPEN DISCORD DEBUG FILE:",
|
|
357
|
+
"version: "+this.version.toString(),
|
|
358
|
+
"last startup: "+dstring,
|
|
359
|
+
"=========================\n\n"
|
|
360
|
+
].join("\n")
|
|
361
|
+
}
|
|
362
|
+
/**Write the stats/header to the debug file on startup */
|
|
363
|
+
#writeStartupStats(){
|
|
364
|
+
const currenttext = this.#readDebugFile()
|
|
365
|
+
if (currenttext){
|
|
366
|
+
//edit previous file:
|
|
367
|
+
const splitted = currenttext.split("\n")
|
|
368
|
+
splitted.splice(0,7)
|
|
369
|
+
|
|
370
|
+
if (splitted.length+11 > this.maxlines){
|
|
371
|
+
splitted.splice(0,((splitted.length+11) - this.maxlines))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
splitted.unshift(this.#createStatsText())
|
|
375
|
+
splitted.push("\n---------------------------------------------------------------------\n---------------------------------------------------------------------\n")
|
|
376
|
+
|
|
377
|
+
fs.writeFileSync(this.path,splitted.join("\n"))
|
|
378
|
+
}else{
|
|
379
|
+
//write new file:
|
|
380
|
+
const newtext = this.#createStatsText()
|
|
381
|
+
fs.writeFileSync(this.path,newtext)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**Write an `ODConsoleMessage` to the debug file */
|
|
385
|
+
writeConsoleMessage(message:ODConsoleMessage){
|
|
386
|
+
this.#writeDebugFile(message.toDebugString())
|
|
387
|
+
}
|
|
388
|
+
/**Write an `ODError` to the debug file */
|
|
389
|
+
writeErrorMessage(error:ODError){
|
|
390
|
+
this.#writeDebugFile(error.toDebugString())
|
|
391
|
+
}
|
|
392
|
+
/**Write custom text to the debug file */
|
|
393
|
+
writeText(text:string){
|
|
394
|
+
this.#writeDebugFile(text)
|
|
395
|
+
}
|
|
396
|
+
/**Write a custom note to the debug file (starting with `[NOTE]:`) */
|
|
397
|
+
writeNote(text:string){
|
|
398
|
+
this.#writeDebugFile("[NOTE]: "+text)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**## ODDebugger `class`
|
|
403
|
+
* This is the Open Discord debugger.
|
|
404
|
+
*
|
|
405
|
+
* It is a simple wrapper around the `ODConsoleManager` to handle debugging (primarily for `ODManagers`).
|
|
406
|
+
* Messages created using this debugger are only logged to the debug file unless specified otherwise.
|
|
407
|
+
*
|
|
408
|
+
* You will probably notice this class being used in the `ODManager` constructor.
|
|
409
|
+
*
|
|
410
|
+
* Using this system, all additions & removals inside a manager are logged to the debug file. This makes searching for errors a lot easier!
|
|
411
|
+
*/
|
|
412
|
+
export class ODDebugger {
|
|
413
|
+
/**An alias to the Open Discord console manager. */
|
|
414
|
+
console: ODConsoleManager
|
|
415
|
+
/**When enabled, debug logs are also shown in the console. */
|
|
416
|
+
visible: boolean = false
|
|
417
|
+
|
|
418
|
+
constructor(console:ODConsoleManager){
|
|
419
|
+
this.console = console
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**Create a debug message. This will always be logged to `debug.txt` & sometimes to the console (when enabled). Returns `true` when visible */
|
|
423
|
+
debug(message:string, params?:{key:string,value:string}[]): boolean {
|
|
424
|
+
if (this.visible){
|
|
425
|
+
this.console.log(new ODConsoleDebugMessage(message,params))
|
|
426
|
+
return true
|
|
427
|
+
}else{
|
|
428
|
+
this.console.debugfile.writeConsoleMessage(new ODConsoleDebugMessage(message,params))
|
|
429
|
+
return false
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**## ODLivestatusColor `type`
|
|
435
|
+
* This is a collection of all the colors available within the LiveStatus system.
|
|
436
|
+
*/
|
|
437
|
+
export type ODLiveStatusColor = "normal"|"red"|"green"|"blue"|"yellow"|"white"|"gray"|"magenta"|"cyan"
|
|
438
|
+
|
|
439
|
+
/**## ODLiveStatusSourceData `interface`
|
|
440
|
+
* This is an interface containing all raw data received from the LiveStatus system.
|
|
441
|
+
*/
|
|
442
|
+
export interface ODLiveStatusSourceData {
|
|
443
|
+
/**The message to display */
|
|
444
|
+
message:{
|
|
445
|
+
/**The title of the message to display */
|
|
446
|
+
title:string,
|
|
447
|
+
/**The title color of the message to display */
|
|
448
|
+
titleColor:ODLiveStatusColor,
|
|
449
|
+
/**The description of the message to display */
|
|
450
|
+
description:string,
|
|
451
|
+
/**The description color of the message to display */
|
|
452
|
+
descriptionColor:ODLiveStatusColor
|
|
453
|
+
},
|
|
454
|
+
/**The message will only be shown when the bot matches all statements */
|
|
455
|
+
active:{
|
|
456
|
+
/**A list of versions to match */
|
|
457
|
+
versions:string[],
|
|
458
|
+
/**A list of languages to match */
|
|
459
|
+
languages:string[],
|
|
460
|
+
/**All languages should match */
|
|
461
|
+
allLanguages:boolean,
|
|
462
|
+
/**Match when the bot is using plugins */
|
|
463
|
+
usingPlugins:boolean,
|
|
464
|
+
/**Match when the bot is not using plugins */
|
|
465
|
+
notUsingPlugins:boolean,
|
|
466
|
+
/**Match when the bot is using slash commands */
|
|
467
|
+
usingSlashCommands:boolean,
|
|
468
|
+
/**Match when the bot is not using slash commands */
|
|
469
|
+
notUsingSlashCommands:boolean,
|
|
470
|
+
/**Match when the bot is not using transcripts */
|
|
471
|
+
notUsingTranscripts:boolean,
|
|
472
|
+
/**Match when the bot is using text transcripts */
|
|
473
|
+
usingTextTranscripts:boolean,
|
|
474
|
+
/**Match when the bot is using html transcripts */
|
|
475
|
+
usingHtmlTranscripts:boolean
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**## ODLiveStatusSource `class`
|
|
480
|
+
* This is the Open Discord livestatus source.
|
|
481
|
+
*
|
|
482
|
+
* It is an empty template for a livestatus source.
|
|
483
|
+
* By default, you should use `ODLiveStatusUrlSource` or `ODLiveStatusFileSource`,
|
|
484
|
+
* unless you want to create one on your own!
|
|
485
|
+
*
|
|
486
|
+
* This class doesn't do anything on it's own! It's just a template!
|
|
487
|
+
*/
|
|
488
|
+
export class ODLiveStatusSource extends ODManagerData {
|
|
489
|
+
/**The raw data of this source */
|
|
490
|
+
data: ODLiveStatusSourceData[]
|
|
491
|
+
|
|
492
|
+
constructor(id:ODValidId, data:ODLiveStatusSourceData[]){
|
|
493
|
+
super(id)
|
|
494
|
+
this.data = data
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**Change the current data using this method! */
|
|
498
|
+
setData(data:ODLiveStatusSourceData[]){
|
|
499
|
+
this.data = data
|
|
500
|
+
}
|
|
501
|
+
/**Get all messages relevant to the bot based on some parameters. */
|
|
502
|
+
async getMessages(main:ODMain): Promise<ODLiveStatusSourceData[]> {
|
|
503
|
+
const validMessages: ODLiveStatusSourceData[] = []
|
|
504
|
+
|
|
505
|
+
//parse data from ODMain
|
|
506
|
+
const currentVersion: string = main.versions.get("opendiscord:version")?.toString(true) ?? "<OD:UNKNOWN_VERSION>"
|
|
507
|
+
const usingSlashCommands: boolean = main.configs.get("opendiscord:general")?.data.slashCommands ?? false
|
|
508
|
+
const usingTranscripts: false|"text"|"html" = false as false|"text"|"html" //TODO
|
|
509
|
+
const currentLanguage: string = main.languages.getCurrentLanguageId()
|
|
510
|
+
const usingPlugins: boolean = (main.plugins.getLength() > 0)
|
|
511
|
+
|
|
512
|
+
//check data for each message
|
|
513
|
+
this.data.forEach((msg) => {
|
|
514
|
+
const {active} = msg
|
|
515
|
+
|
|
516
|
+
const correctVersion = active.versions.includes(currentVersion)
|
|
517
|
+
const correctSlashMode = (usingSlashCommands && active.usingSlashCommands) || (!usingSlashCommands && active.notUsingSlashCommands)
|
|
518
|
+
const correctTranscriptMode = (usingTranscripts == "text" && active.usingTextTranscripts) || (usingTranscripts == "html" && active.usingHtmlTranscripts) || (!usingTranscripts && active.notUsingTranscripts)
|
|
519
|
+
const correctLanguage = active.languages.includes(currentLanguage) || active.allLanguages
|
|
520
|
+
const correctPlugins = (usingPlugins && active.usingPlugins) || (!usingPlugins && active.notUsingPlugins)
|
|
521
|
+
|
|
522
|
+
if (correctVersion && correctLanguage && correctPlugins && correctSlashMode && correctTranscriptMode) validMessages.push(msg)
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
//return the valid messages
|
|
526
|
+
return validMessages
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**## ODLiveStatusFileSource `class`
|
|
531
|
+
* This is the Open Discord livestatus file source.
|
|
532
|
+
*
|
|
533
|
+
* It is a LiveStatus source that will read the data from a local file.
|
|
534
|
+
*
|
|
535
|
+
* This can be used for testing/extending the LiveStatus system!
|
|
536
|
+
*/
|
|
537
|
+
export class ODLiveStatusFileSource extends ODLiveStatusSource {
|
|
538
|
+
/**The path to the source file */
|
|
539
|
+
path: string
|
|
540
|
+
|
|
541
|
+
constructor(id:ODValidId, path:string){
|
|
542
|
+
if (fs.existsSync(path)){
|
|
543
|
+
super(id,JSON.parse(fs.readFileSync(path).toString()))
|
|
544
|
+
}else throw new ODSystemError("LiveStatus source file doesn't exist!")
|
|
545
|
+
this.path = path
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**## ODLiveStatusUrlSource `class`
|
|
550
|
+
* This is the Open Discord livestatus url source.
|
|
551
|
+
*
|
|
552
|
+
* It is a LiveStatus source that will read the data from a http URL (json file).
|
|
553
|
+
*
|
|
554
|
+
* This is the default way of receiving LiveStatus messages!
|
|
555
|
+
*/
|
|
556
|
+
export class ODLiveStatusUrlSource extends ODLiveStatusSource {
|
|
557
|
+
/**The url used in the request */
|
|
558
|
+
url: string
|
|
559
|
+
/**The `ODHTTPGetRequest` helper to fetch the url! */
|
|
560
|
+
request: ODHTTPGetRequest
|
|
561
|
+
|
|
562
|
+
constructor(main:ODMain,id:ODValidId, url:string){
|
|
563
|
+
super(id,[])
|
|
564
|
+
this.url = url
|
|
565
|
+
this.request = new ODHTTPGetRequest(main,url,false)
|
|
566
|
+
}
|
|
567
|
+
async getMessages(main:ODMain): Promise<ODLiveStatusSourceData[]> {
|
|
568
|
+
//additional setup
|
|
569
|
+
this.request.url = this.url
|
|
570
|
+
const rawRes = await this.request.run()
|
|
571
|
+
if (rawRes.status != 200) throw new ODSystemError("ODLiveStatusUrlSource => Request Failed!")
|
|
572
|
+
try{
|
|
573
|
+
this.setData(JSON.parse(rawRes.body))
|
|
574
|
+
}catch{
|
|
575
|
+
throw new ODSystemError("ODLiveStatusUrlSource => Request Failed!")
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
//default
|
|
579
|
+
return super.getMessages(main)
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**## ODLiveStatusManager `class`
|
|
584
|
+
* This is the Open Discord livestatus manager.
|
|
585
|
+
*
|
|
586
|
+
* It manages all LiveStatus sources and has the renderer for all LiveStatus messages.
|
|
587
|
+
*
|
|
588
|
+
* You can use this to customise or add stuff to the LiveStatus system.
|
|
589
|
+
* Access it in the global `opendiscord.startscreen.livestatus` variable!
|
|
590
|
+
*/
|
|
591
|
+
export class ODLiveStatusManager extends ODManager<ODLiveStatusSource> {
|
|
592
|
+
/**The class responsible for rendering the livestatus messages. */
|
|
593
|
+
renderer: ODLiveStatusRenderer
|
|
594
|
+
/**A reference to the ODMain or "opendiscord" global variable */
|
|
595
|
+
#main: ODMain
|
|
596
|
+
|
|
597
|
+
constructor(debug:ODDebugger, main:ODMain){
|
|
598
|
+
super(debug,"livestatus source")
|
|
599
|
+
this.renderer = new ODLiveStatusRenderer(main.console)
|
|
600
|
+
this.#main = main
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**Get the messages from all sources combined! */
|
|
604
|
+
async getAllMessages(): Promise<ODLiveStatusSourceData[]> {
|
|
605
|
+
const messages: ODLiveStatusSourceData[] = []
|
|
606
|
+
for (const source of this.getAll()){
|
|
607
|
+
try {
|
|
608
|
+
messages.push(...(await source.getMessages(this.#main)))
|
|
609
|
+
}catch{}
|
|
610
|
+
}
|
|
611
|
+
return messages
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**## ODLiveStatusRenderer `class`
|
|
616
|
+
* This is the Open Discord livestatus renderer.
|
|
617
|
+
*
|
|
618
|
+
* It's responsible for rendering all LiveStatus messages to the console.
|
|
619
|
+
*/
|
|
620
|
+
export class ODLiveStatusRenderer {
|
|
621
|
+
/**A reference to the ODConsoleManager or "opendiscord.console" global variable */
|
|
622
|
+
#console: ODConsoleManager
|
|
623
|
+
|
|
624
|
+
constructor(console:ODConsoleManager){
|
|
625
|
+
this.#console = console
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**Render all messages */
|
|
629
|
+
render(messages:ODLiveStatusSourceData[]): string {
|
|
630
|
+
try {
|
|
631
|
+
//process data
|
|
632
|
+
const final: string[] = []
|
|
633
|
+
messages.forEach((msg) => {
|
|
634
|
+
const titleColor = msg.message.titleColor
|
|
635
|
+
const title = "["+msg.message.title+"] "
|
|
636
|
+
|
|
637
|
+
const descriptionColor = msg.message.descriptionColor
|
|
638
|
+
const description = msg.message.description.split("\n").map((text,row) => {
|
|
639
|
+
//first row row doesn't need prefix
|
|
640
|
+
if (row < 1) return text
|
|
641
|
+
//other rows do need a prefix
|
|
642
|
+
let text2 = text
|
|
643
|
+
for (const i of title){
|
|
644
|
+
text2 = " "+text2
|
|
645
|
+
}
|
|
646
|
+
return text2
|
|
647
|
+
}).join("\n")
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
if (!["red","yellow","green","blue","gray","magenta","cyan"].includes(titleColor)) var finalTitle = ansis.white(title)
|
|
651
|
+
else var finalTitle = ansis[titleColor](title)
|
|
652
|
+
if (!["red","yellow","green","blue","gray","magenta","cyan"].includes(descriptionColor)) var finalDescription = ansis.white(description)
|
|
653
|
+
else var finalDescription = ansis[descriptionColor](description)
|
|
654
|
+
|
|
655
|
+
final.push(finalTitle+finalDescription)
|
|
656
|
+
})
|
|
657
|
+
|
|
658
|
+
//return all messages
|
|
659
|
+
return final.join("\n")
|
|
660
|
+
}catch{
|
|
661
|
+
this.#console.log("Failed to render LiveStatus messages!","error")
|
|
662
|
+
return ""
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|