-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
146 lines (131 loc) · 3.81 KB
/
log.js
File metadata and controls
146 lines (131 loc) · 3.81 KB
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
const util = require("util"),
queue = [];
/**
* @type {typeof import("./discord")}
*/
let Discord;
// #
// #
// # ### ## #
// # # # # #
// # # # ##
// # # # #
// ##### ### ###
// # #
// ###
/**
* A class that handles logging.
*/
class Log {
// ##
// #
// # ## ###
// # # # # #
// # # # ##
// ### ## #
// ###
/**
* Logs a message.
* @param {object} obj The object to log.
* @returns {void}
*/
static log(obj) {
queue.push({
type: "log",
date: new Date(),
obj
});
Log.output();
}
// #
//
// # # ### ### ### ## ### ###
// # # # # # # # # # # # # #
// #### # ## # # # # # # ##
// #### # # # # # ### # # #
// ###
/**
* Logs a warning.
* @param {string} message The string to log.
* @returns {void}
*/
static warning(message) {
queue.push({
type: "warning",
date: new Date(),
message
});
Log.output();
}
// # #
// #
// ## # # ## ## ### ### ## ## ###
// # ## ## # # ## # # # # # # # #
// ## ## # ## # # # # # # # #
// ## # # ## ## ### ## ### ## # #
// #
/**
* Logs an exception.
* @param {string} message The message describing the error.
* @param {object} obj The object to log.
* @returns {void}
*/
static exception(message, obj) {
queue.push({
type: "exception",
date: new Date(),
message,
obj
});
Log.output();
}
// # #
// # #
// ## # # ### ### # # ###
// # # # # # # # # # #
// # # # # # # # # # #
// ## ### ## ### ### ##
// #
/**
* Outputs the log queue.
* @returns {void}
*/
static output() {
if (!Discord) {
Discord = require("./discord");
}
if (Discord.isConnected()) {
const logChannel = Discord.findChannelByName("fusionbot-log"),
errorChannel = Discord.findChannelByName("fusionbot-errors");
queue.forEach((log) => {
const message = {
embed: {
color: log.type === "log" ? 0x80FF80 : log.type === "warning" ? 0xFFFF00 : 0xFF0000,
footer: {"icon_url": Discord.icon, text: "DescentBot"},
fields: [],
timestamp: log.date
}
};
if (log.message) {
({message: message.embed.description} = log);
}
if (log.obj) {
const msg = util.inspect(log.obj);
if (msg.length > 1024) {
Discord.queue(msg, log.type === "exception" ? errorChannel : logChannel);
return;
}
message.embed.fields.push({
name: "Message",
value: msg
});
}
Discord.richQueue(message, log.type === "exception" ? errorChannel : logChannel);
});
queue.splice(0, queue.length);
} else {
console.log(queue[queue.length - 1]);
}
}
}
module.exports = Log;