-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
131 lines (103 loc) · 3.31 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
const file = require("node:fs");
const JSON5 = require("json5");
const Command = require("./classes/command.js");
const Config = require("./classes/config.js");
const Got = require("./classes/got.js");
const Cache = require("./singleton/cache.js");
const Logger = require("./singleton/logger.js");
const Utils = require("./singleton/utils.js");
const HoyoLab = require("./hoyolab-modules/template.js");
const Platform = require("./platforms/template.js");
const Date = require("./object/date.js");
const Error = require("./object/error.js");
const RegionalTaskManager = require("./object/regional-task-manager.js");
let config;
try {
config = JSON5.parse(file.readFileSync("./config.json5"));
}
catch (e) {
if (file.existsSync("./config.json5") === false) {
throw new Error({
message: `No config file (config.json5) was found. Please follow the setup instructions on https://github.com/torikushiii/hoyolab-auto?tab=readme-ov-file#installation \n${e}`
});
}
throw new Error({
message: `An error occurred when reading your configuration file. Please check and fix the following error:\n${e}`
});
}
(async () => {
const start = process.hrtime.bigint();
const platformsConfig = config.platforms;
if (!platformsConfig || platformsConfig.length === 0) {
console.warn("No platforms configured! Exiting.");
process.exit(0);
}
globalThis.app = {
Date,
Error,
RegionalTaskManager,
Config,
Command,
Got: await Got.initialize(),
Cache: new Cache(),
Logger: new Logger(config.loglevel),
Utils: new Utils()
};
app.Logger.info("Client", "Loading configuration data");
Config.load(config);
app.Logger.info("Client", `Loaded ${Config.data.size} configuration entries`);
const { loadCommands } = require("./commands/index.js");
const commands = await loadCommands();
await Command.importData(commands.definitions);
const { initCrons } = require("./crons/index.js");
initCrons();
const accountsConfig = config.accounts;
if (!accountsConfig || accountsConfig.length === 0) {
app.Logger.warn("Client", "No accounts configured! Exiting.");
process.exit(0);
}
const accounts = new Set();
for (const definition of accountsConfig) {
if (!definition.active) {
app.Logger.warn("Client", `Skipping ${definition.type} account (inactive)`);
continue;
}
accounts.add(HoyoLab.create(definition.type, definition));
}
const definitions = require("./gots/index.js");
await app.Got.importData(definitions);
globalThis.app = {
...app,
Platform,
HoyoLab
};
const hoyoPromises = [];
for (const account of accounts) {
hoyoPromises.push(account.login());
}
await Promise.all(hoyoPromises);
const platforms = new Set();
for (const definition of platformsConfig) {
if (!definition.active) {
app.Logger.warn("Client", `Skipping ${definition.type} platform (inactive)`);
continue;
}
platforms.add(Platform.create(definition.type, definition));
}
const promises = [];
for (const platform of platforms) {
promises.push(platform.connect());
}
await Promise.all(promises);
const end = process.hrtime.bigint();
app.Logger.info("Client", `Initialize completed (${Number(end - start) / 1e6}ms)`);
process.on("unhandledRejection", (reason) => {
if (!(reason instanceof Error)) {
return;
}
app.Logger.log("Client", {
message: "Unhandled promise rejection",
args: { reason }
});
});
})();