1 |
import { MessageEmbed, WebhookClient } from "discord.js"; |
2 |
import { appendFile } from "fs/promises"; |
3 |
import Service from "../utils/structures/Service"; |
4 |
import { splitMessage } from "../utils/util"; |
5 |
|
6 |
export enum LogLevel { |
7 |
LOG = 'log', |
8 |
INFO = 'info', |
9 |
WARN = 'warn', |
10 |
CRITICAL = 'critical', |
11 |
ERROR = 'error' |
12 |
} |
13 |
|
14 |
export default class DebugLogger extends Service { |
15 |
private joinLeaveLogFile = __dirname + '/../../logs/join-leave.log'; |
16 |
private appLogFile = __dirname + '/../../logs/app.log'; |
17 |
|
18 |
async logApp(level: LogLevel, message: string) { |
19 |
await this.log(this.appLogFile, level, message); |
20 |
} |
21 |
|
22 |
async logLeaveJoin(level: LogLevel, message: string) { |
23 |
await this.log(this.joinLeaveLogFile, level, message); |
24 |
} |
25 |
|
26 |
async log(stream: string, level: LogLevel, message: string) { |
27 |
await appendFile(stream, `[${new Date().toISOString()}] [${level}] ${message}\n`); |
28 |
} |
29 |
|
30 |
async logToHomeServer(message: string, logLevel: LogLevel = LogLevel.ERROR) { |
31 |
if (!process.env.DEBUG_WEKHOOK_URL) |
32 |
return; |
33 |
|
34 |
const webhookClient = new WebhookClient({ url: process.env.DEBUG_WEKHOOK_URL! }); |
35 |
const splitted = splitMessage(message); |
36 |
const embed = new MessageEmbed({ |
37 |
color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60, |
38 |
title: logLevel === LogLevel.WARN ? 'Core Warning' : 'Fatal Error', |
39 |
description: splitted.shift(), |
40 |
}); |
41 |
|
42 |
if (splitted.length === 0) { |
43 |
embed.setTimestamp(); |
44 |
} |
45 |
|
46 |
try { |
47 |
await webhookClient.send({ |
48 |
embeds: [ |
49 |
embed |
50 |
] |
51 |
}); |
52 |
|
53 |
for (const index in splitted) { |
54 |
const embed = new MessageEmbed({ |
55 |
color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60, |
56 |
description: splitted[index], |
57 |
}); |
58 |
|
59 |
if (parseInt(index) === (splitted.length - 1)) { |
60 |
embed.setTimestamp(); |
61 |
} |
62 |
|
63 |
await webhookClient.send({ |
64 |
embeds: [ |
65 |
embed |
66 |
] |
67 |
}); |
68 |
} |
69 |
} |
70 |
catch (e) { |
71 |
console.log(e); |
72 |
} |
73 |
} |
74 |
} |