-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
337 lines (329 loc) · 12.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const { Console } = require('console');
const { Transform } = require('stream');
function getTimestamp() {
var timeNow = new Date();
var timestamp = "" + timeNow.toLocaleTimeString() + " " + timeNow.toLocaleDateString()
return timestamp;
};
function log(message, logchannel, client) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
var content = "```md\n " + getTimestamp() + " " + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
function logStatus(message, logchannel, client, status) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
if (status == undefined) return;
var content = "```md\n #" + getTimestamp() + " [" + status + "]: " + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
function logError(message, logchannel, client, code) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
if (code == undefined) return;
var content = "```md\n [" + getTimestamp() + "]: [ERROR] " + code + ": " + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
function logWarning(message, logchannel, client) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
var content = "```md\n [" + getTimestamp() + "]: [WARNING] " + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
function logOperation(message, logchannel, client, status, type) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
if (status == undefined) return;
if (type == undefined) return;
var content = "```md\n #" + getTimestamp() + " [OPERATION] " + status + " |" + type + ": " + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
function logTable(message, logchannel, client) {
try {
if (client == undefined) return;
if (logchannel == undefined) return;
var content = "```md\n" + message + "```";
client.channels.cache.get(logchannel).send({ content: content });
} catch (e) { return }
};
module.exports.console = class {
/**
* Richte das Consolen System ein
* @param {boolean} channellog Gebe an, ob das Consolen modul über einen Discord Bot mit loggen soll
* @param {discord.CategoryChannel.id} logchannel Gebe die Channel ID deines Discord Log Channel an
* @param {boolean} time Gebe an, ob eine Zeitangabe mit geloggt werden soll
* @param {boolean} color Gebe an, ob die Ausgabe Farbig sein soll
* @param {discord.Client} client Gebe den Discord Client ggf. an
*
* @example const { console } = require('./modules/console')
* console.consolebuilder(false, 0, true, true, client)
*
* @since v1.0.0
*/
static consolebuilder(
channellog = false,
logchannel = 0,
time = true,
color = true,
client
) {
this.channellog = channellog;
this.logchannel = logchannel;
this.time = time;
this.color = color;
this.client = client;
}
/**
* Logge eine einfache Nachricht
* @param {string} message Nachricht
*
* @example console.log("Moin")
*
* @since v1.0.0
*/
static log(message) {
if (this.channellog == true) {
log(message, this.logchannel, this.client)
}
if (this.time == true && this.color == true) {
console.log("\u001b[30;1m" + getTimestamp() + " \u001b[0m" + message);
}
if (this.time == false && this.color == true) {
console.log("\u001b[0m" + message);
}
if (this.time == true && this.color == false) {
console.log(getTimestamp() + " " + message);
}
if (this.time == false && this.color == false) {
console.log(message);
}
};
/**
* Logge einen Fehler
* @param {string} code Fehlercode
* @param {string} message Nachricht dazu
*
* @exaple console.logError(error, "Alles Ok")
*
* @since v1.0.0
*/
static logError(code, message) {
if (this.channellog == true) {
logError(message, this.logchannel, this.client, code)
}
if (this.time == true && this.color == true) {
console.log("\u001b[30;1m" + getTimestamp() + " \u001b[0m[\u001b[31;1m ERROR \u001b[0m] " + code + ": " + message);
}
if (this.time == false && this.color == true) {
console.log("\u001b[0m[\u001b[31;1m ERROR \u001b[0m] " + code + ": " + message);
}
if (this.time == true && this.color == false) {
console.log(getTimestamp() + " [ ERROR ] " + code + ": " + message);
}
if (this.time == false && this.color == false) {
console.log("[ ERROR ] " + code + ": " + message);
}
};
/**
* Logge eine Warnung
* @param {string} message Warnung
*
* @example console.logWarning("Moin")
*
* @since v1.0.0
*/
static logWarning(message) {
if (this.channellog == true) {
logWarning(message, this.logchannel, this.client)
}
if (this.time == true && this.color == true) {
console.log("\u001b[30;1m" + getTimestamp() + " \u001b[0m[\u001b[33m WARNING \u001b[0m]: " + message);
}
if (this.time == false && this.color == true) {
console.log("\u001b[0m[\u001b[33m WARNING \u001b[0m]: " + message);
}
if (this.time == true && this.color == false) {
console.log(getTimestamp() + " [ WARNING ]: " + message);
}
if (this.time == false && this.color == false) {
console.log("[ WARNING ]: " + message);
}
};
/**
* Logge eine Operation
* @param {string} type Operationstyp
* @param {string} status Status der Operation, z.B. STILL, COMPLEATE, ERROR
* @param {string} message Operationsnachricht
*
* @example console.logOperation("Login","COMLPLEATE","erfolgreich")
*
* @since v1.0.0
*/
static logOperation(type, status, message) {
if (this.channellog == true) {
logOperation(message, this.logchannel, this.client, status, type)
}
if (this.time == true && this.color == true) {
console.log("\u001b[30;1m" + getTimestamp() + " \u001b[0m[\u001b[36;1m OPERATION \u001b[0m] \u001b[35m" + status + "\u001b[0m|" + type + ": " + message);
}
if (this.time == false && this.color == true) {
console.log("\u001b[0m[\u001b[36;1m OPERATION \u001b[0m] \u001b[35m" + status + "\u001b[0m|" + type + ": " + message);
}
if (this.time == true && this.color == false) {
console.log(getTimestamp() + " [ OPERATION ] " + status + "|" + type + ": " + message);
}
if (this.time == false && this.color == false) {
console.log("[ OPERATION ] " + status + "|" + type + ": " + message);
}
};
/**
* Logge einen Status
* @param {string} status der Status
* @param {string} message Nachricht zum Status
*
* @example console.logStatus("Online", "System erfolgreich gestartet")
*
* @since v1.0.0
*/
static logStatus(status, message) {
if (this.channellog == true) {
logStatus(message, this.logchannel, this.client, status)
}
if (this.time == true && this.color == true) {
console.log("\u001b[30;1m" + getTimestamp() + " \u001b[0m[ \u001b[32m" + status + " \u001b[0m]: " + message);
}
if (this.time == false && this.color == true) {
console.log("\u001b[0m[ \u001b[32m" + status + " \u001b[0m]: " + message);
}
if (this.time == true && this.color == false) {
console.log(getTimestamp() + " [ " + status + " ]: " + message);
}
if (this.time == false && this.color == false) {
console.log("[ " + status + " ]: " + message);
}
};
/**
* Leere die Console
*
* @example console.logclear()
*
* @since v2.0.0
*/
static logClear() {
if (this.channellog == true) {
log("console cleared", this.logchannel, this.client)
}
console.clear()
}
/**
* Stelle daten in der Console in einer Tabelle dar
* @param {Array} input Array der Daten die in der Tabelle angezeigt werden sollen
* @param {Array} properties Welche Argumente sollen angezeigt werden - Weck lassen oder leres Array lässt alles anzeigen - Index ausgeschlossen!
* @param {boolean} index Wenn true wird die Spalte (index) nicht angezeigt
*
* @example console.logTable([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], [], false)
* @example console.logTable([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ["a"], false)
* @example console.logTable([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ["a"], true)
*
* @since v2.0.0
*/
static logTable(input, properties, index) {
if(!properties || !properties[0]){
properties = []
for (let i = 0; i < input.length; i++) {
for (const key in input[i]) {
properties.push(key)
}
}
}
if(!index){
index = false
}
const ts = new Transform({ transform(chunk, enc, cb) { cb(null, chunk) } })
const logger = new Console({ stdout: ts })
logger.table(input, properties)
const table = (ts.read() || '').toString()
let result = '';
if (this.channellog == true) {
for (let row of table.split(/[\r\n]+/)) {
let r = row
if(index == false){
r = r.replace(/[^┬]*┬/, '┌');
r = r.replace(/^├─*┼/, '├');
r = r.replace(/│[^│]*/, '');
r = r.replace(/^└─*┴/, '└');
r = r.replace(/'/g, ' ');
}
result += `${getTimestamp()} ${r}\n`;
}
logTable(result, this.logchannel, this.client)
}
if (this.time == true && this.color == true) {
for (let row of table.split(/[\r\n]+/)) {
let r = row
if(index == false){
r = r.replace(/[^┬]*┬/, '┌');
r = r.replace(/^├─*┼/, '├');
r = r.replace(/│[^│]*/, '');
r = r.replace(/^└─*┴/, '└');
r = r.replace(/'/g, ' ');
}
result += `\u001b[30;1m${getTimestamp()} \u001b[32m ${r}\n`;
}
console.log(result + "\u001b[0m");
}
if (this.time == false && this.color == true) {
for (let row of table.split(/[\r\n]+/)) {
let r = row
if(index == false){
r = r.replace(/[^┬]*┬/, '┌');
r = r.replace(/^├─*┼/, '├');
r = r.replace(/│[^│]*/, '');
r = r.replace(/^└─*┴/, '└');
r = r.replace(/'/g, ' ');
}
result += `\u001b[32m ${r}\n`;
}
console.log(result + "\u001b[0m");
}
if (this.time == true && this.color == false) {
for (let row of table.split(/[\r\n]+/)) {
let r = row
if(index == false){
r = r.replace(/[^┬]*┬/, '┌');
r = r.replace(/^├─*┼/, '├');
r = r.replace(/│[^│]*/, '');
r = r.replace(/^└─*┴/, '└');
r = r.replace(/'/g, ' ');
}
result += `${getTimestamp()} ${r}\n`;
}
console.log(result);
}
if (this.time == false && this.color == false) {
for (let row of table.split(/[\r\n]+/)) {
let r = row
if(index == false){
r = r.replace(/[^┬]*┬/, '┌');
r = r.replace(/^├─*┼/, '├');
r = r.replace(/│[^│]*/, '');
r = r.replace(/^└─*┴/, '└');
r = r.replace(/'/g, ' ');
}
result += `${r}\n`;
}
console.log(result);
}
}
};