/
startup.ts
144 lines (128 loc) · 4.14 KB
/
startup.ts
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
import dotenv from "dotenv";
import { readFileSync, existsSync, renameSync } from "node:fs";
import { join } from "node:path";
import { inspect } from "util";
import chalk from "chalk";
import { Console, File } from "winston/lib/winston/transports";
import winston from "winston";
import leapYear from "dayjs/plugin/isLeapYear";
import duration from "dayjs/plugin/duration";
import dayjs from "dayjs";
import { Tune } from "./src/Tune";
import { Sentry } from "./src/structures/logger/Sentry";
const logger = winston.createLogger({
handleExceptions: true,
handleRejections: true,
exitOnError: false,
});
console.log(
readFileSync(join(__dirname, "..", "banner.txt"), { encoding: "utf-8" })
);
logger.add(
new Console({
level: "silly",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf((info) => {
const levelPrefix =
info.level.includes("info") || info.level.includes("warn")
? `${info.level} `
: info.level;
const tagsPrefix =
info.tags && info.tags.length > 0
? ` --- [${info.tags
.map((t: string) => chalk.cyan(t))
.join(", ")}]:`
: " --- ";
const message =
(info.message as any) instanceof Error ||
typeof info.message === "object"
? inspect(info.message, { depth: 0 })
: String(info.message);
return `${info.timestamp} ${levelPrefix} ${process.pid}${tagsPrefix} ${message}`;
})
),
})
);
if (existsSync(join(__dirname, "..", "logs", "latest.log"))) {
logger.debug("Old log file found, renaming...", {
tags: ["Logger"],
});
renameSync(
join(__dirname, "..", "logs", "latest.log"),
join(
__dirname,
"..",
"logs",
`${new Date().toISOString().replaceAll(":", "-")}.log`
)
);
}
logger.add(
new File({
level: "silly",
dirname: join(__dirname, "..", "logs"),
filename: "latest.log",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.uncolorize(),
winston.format.printf((info) => {
const levelPrefix =
info.level.includes("info") || info.level.includes("warn")
? `${info.level} `
: info.level;
const tagsPrefix =
info.tags && info.tags.length > 0
? ` --- [${info.tags.join(", ")}]:`
: " --- ";
const message =
(info.message as any) instanceof Error ||
typeof info.message === "object"
? inspect(info.message, { depth: 0 })
: String(info.message);
return `${info.timestamp} ${levelPrefix} ${process.pid}${tagsPrefix} ${message}`;
})
),
})
);
Object.defineProperty(global, "CUSTOM_COLORS", { value: Object.create(null) });
const result = dotenv.config();
if (result.error) {
logger.error(result.error as any, { tags: ["DotEnv"] });
process.exit(1);
}
if (process.env.SENTRY_DSN && process.env.SENTRY_DSN.length > 0) {
logger.add(new Sentry(process.env.SENTRY_DSN));
} else {
logger.warn("Sentry DSN not defined. Skipping Sentry configuration.", {
tags: ["Logger", "Sentry"],
});
}
logger.info("Logger ready.", { tags: ["Logger"] });
dayjs.extend(leapYear);
dayjs.extend(duration);
logger.info("Day.JS loaded.", { tags: ["DayJS"] });
const tune = new Tune(logger);
/* if (process.env.DISCORD_CLIENT_ID === Tune.bots[0]) {
const path = join(__dirname, "..", "theme", "active");
logger.debug("Oh main bot! Applying theme config...", {
tags: ["Theme"],
});
const promise = import(join(path, "info.json"));
promise.then(async (theme) => {
tune.options.presence = theme.presence ?? tune.options.presence;
await tune.loadTheme(path);
});
} */
process.on("uncaughtException", (error: any) => {
tune.logger.error(error as any, { tags: ["Process"] });
});
process.on("unhandledRejection", (error: any, promise) => {
if (typeof error === "object") Object.assign(error, { promise });
tune.logger.error(error, { tags: ["Process"] });
});
process.on("warning", (warning) =>
tune.logger.warn(warning.message, { tags: ["Process"] })
);
tune.init();