-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.ts
More file actions
97 lines (81 loc) · 3.03 KB
/
index.ts
File metadata and controls
97 lines (81 loc) · 3.03 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
import logger from "./utils/logger";
import { loadConfig } from "./utils/config";
import Got, { type GotRequestOptions } from "./classes/got";
import { Game } from "./skport/template";
import Endfield from "./skport/endfield";
import initializeCrons from "./crons";
import Platform from "./platform/template";
import Commands from "./classes/command";
import { Template } from "./classes/template";
async function initializeAk(): Promise<void> {
const config = loadConfig();
const gotInstance = new Got();
const commandInstance = new Commands();
const endfieldInstance = new Endfield();
const gotCallable = Object.assign(
async <T = unknown>(moduleName: string, options: GotRequestOptions, ...args: unknown[]): Promise<T> => {
return gotInstance.request<T>(moduleName, options, ...args);
},
{ instance: gotInstance }
);
const platforms = new Map<string, Platform>();
for (const pConfig of config.platforms) {
if (!pConfig.active) continue;
const platform = await Platform.create(pConfig);
platforms.set(pConfig.id, platform);
}
globalThis.ak = {
Logger: logger,
Config: config,
Got: gotCallable,
Platforms: platforms,
Commands: commandInstance,
SKPort: Game,
};
const MODULE_INITIALIZE_ORDER: Template[][] = [
[gotInstance, commandInstance],
[endfieldInstance]
];
ak.Logger.info("Arknights: Endfield Auto | Initialization");
const modulesToDestroy: Template[] = [gotInstance, commandInstance, endfieldInstance];
for (const batch of MODULE_INITIALIZE_ORDER) {
const promises = batch.map(async (mod: Template) => {
const start = Date.now();
await mod.initialize();
const duration = Date.now() - start;
ak.Logger.debug(` Initialized module: ${mod.constructor.name} (${duration}ms)`);
});
await Promise.all(promises);
}
process.on("SIGINT", async () => {
ak.Logger.info("Arknights: Endfield Auto | Shutdown");
for (const mod of modulesToDestroy) {
try {
mod.destroy();
} catch (e) {
ak.Logger.error(`Error destroying module ${mod.constructor.name}:`, { e });
}
}
process.exit(0);
});
ak.Logger.info(`Initialized ${platforms.size} active platform(s)`);
}
async function main(): Promise<void> {
await initializeAk();
initializeCrons();
const botStarts = Array.from(ak.Platforms.values())
.filter(platform => platform.isConfigured())
.map(platform => platform.startBot());
await Promise.all(botStarts);
}
process.on("unhandledRejection", (reason) => {
if (reason instanceof Error) {
ak.Logger.error(`Unhandled Rejection: ${reason.message}`, { stack: reason.stack });
} else {
ak.Logger.error(`Unhandled Rejection: ${reason}`);
}
});
main().catch((error) => {
ak.Logger.error("Fatal error during initialization", { error });
process.exit(1);
});