-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
181 lines (147 loc) · 3.91 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
const Account = require("./classes/account.js");
const CheckIn = require("./classes/check-in.js");
const Command = require("./classes/command.js");
const Config = require("./classes/config.js");
const Expedition = require("./classes/expedition.js");
const Stamina = require("./classes/stamina.js");
const Error = require("./objects/error.js");
const Got = require("./singletons/got.js");
const Logger = require("./singletons/logger.js");
const Utils = require("./singletons/utils.js");
/* eslint-disable no-fallthrough */
const handleFlags = async (argv) => {
const help = `
Usage: node index.js [OPTIONS] [ARGS]...
Honkai: Star Rail Helper
Options:
-h, --help
Show this help message and exit.
-c, --sign
Run auto check-in for all accounts.
-s, --stamina
Show all accounts current stamina and when does it cap.
-e, --expedition
Show all accounts current expedition status.
`;
const flags = argv.slice(2);
let shouldExit = false;
for (const flag of flags) {
switch (flag) {
case "-h":
case "--help": {
console.log(help);
process.exit(0);
}
case "-c":
case "--sign": {
const checkInResult = await sr.CheckIn.checkAndSign();
for (const data of checkInResult) {
const message = (data.result === "OK")
? `[Account ${data.uid}] Check-in successful: ${data.award.name} x${data.award.count}`
: `[Account ${data.uid}] ${data.result}`;
sr.Logger.info(message);
}
shouldExit = true;
break;
}
case "-s":
case "--stamina": {
const staminaResult = await sr.Stamina.checkAndRun({ checkOnly: true });
for (const message of staminaResult) {
const { uid, currentStamina, maxStamina, delta } = message;
sr.Logger.info(`[${uid}] Current stamina: ${currentStamina}/${maxStamina} (${delta})`);
}
shouldExit = true;
break;
}
case "-e":
case "--expedition": {
const expiditionResult = await sr.Expedition.checkAndRun({ checkOnly: true });
for (const data of expiditionResult) {
const { uid } = data;
if (!data?.expeditions) {
sr.Logger.info(`[${uid}] All expedition has been completed!`);
continue;
}
for (const expedition of data.expeditions) {
const { delta } = expedition;
sr.Logger.info(`[${uid}] Remaining time: ${delta}`);
}
}
shouldExit = true;
break;
}
}
}
if (shouldExit) {
process.exit(0);
}
};
let configData;
try {
configData = require("./config.json");
}
catch {
try {
configData = require("./default.config.json");
}
catch {
throw new Error({ message: "No default or custom config file found." });
}
}
(async () => {
globalThis.sr = {
Error,
Logger,
Command,
Config: await Config,
Got: await Got,
Utils: new Utils()
};
if (!configData.modules.commands.disableAll) {
const { loadCommands } = require("./commands/index.js");
const commands = await loadCommands();
await Command.importData(commands.definitions);
}
Config.load(configData.config);
globalThis.sr = {
...sr,
Account: await Account.initialize(configData.accounts)
};
const { initCrons } = require("./crons/index.js");
initCrons(configData.modules.crons);
globalThis.sr = {
...sr,
CheckIn: await CheckIn.initialize(),
Expedition: await Expedition.initialize(),
Stamina: await Stamina.initialize()
};
await handleFlags(process.argv);
const platforms = [
"Discord",
"Telegram"
];
for (const platform of platforms) {
let Controller = null;
try {
Controller = require(`./controllers/${platform.toLowerCase()}.js`);
}
catch (e) {
console.error(`Failed to load ${platform} controller:`, e);
continue;
}
try {
sr[platform] = new Controller();
}
catch (e) {
console.error(`Failed to initialize ${platform} controller:`, e);
continue;
}
}
process.on("unhandledRejection", async (reason) => {
if (!(reason instanceof Error)) {
return;
}
console.error("Rejected promise", reason);
});
})();