/[sudobot]/branches/8.x/src/index.ts
ViewVC logotype

Annotation of /branches/8.x/src/index.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4744 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19     import "module-alias/register";
20     import "reflect-metadata";
21    
22     import axios from "axios";
23     import { spawn } from "child_process";
24     import { GatewayIntentBits, Partials } from "discord.js";
25     import "dotenv/config";
26     import { existsSync, readFileSync } from "fs";
27     import { createInterface } from "node:readline/promises";
28     import path from "path";
29     import Client from "./core/Client";
30     import { logError, logInfo, logSuccess } from "./utils/Logger";
31     import { sudoPrefix } from "./utils/utils";
32    
33     global.bootDate = Date.now();
34    
35     if (!Symbol.metadata) {
36     (Symbol as unknown as Record<string, symbol>).metadata ??= Symbol("metadata");
37     }
38    
39     const intents = [
40     GatewayIntentBits.Guilds,
41     GatewayIntentBits.MessageContent,
42     GatewayIntentBits.GuildMessages,
43     GatewayIntentBits.GuildMembers,
44     GatewayIntentBits.GuildMessageReactions,
45     GatewayIntentBits.GuildModeration,
46     GatewayIntentBits.GuildEmojisAndStickers,
47     GatewayIntentBits.GuildPresences,
48     GatewayIntentBits.GuildInvites,
49     GatewayIntentBits.GuildVoiceStates
50     ];
51    
52     const partials = [Partials.Channel];
53    
54     function spawnNativeProcess() {
55     const path = process.env.EXPERIMENTAL_NATIVE_EXECUTABLE_PATH;
56    
57     if (path) {
58     const child = spawn(path, {
59     stdio: "inherit",
60     env: process.env
61     });
62    
63     process.on("exit", () => void (child.killed ? null : child.kill()));
64     process.on("uncaughtException", () => void (child.killed ? null : child.kill()));
65     process.on("unhandledRejection", () => void (child.killed ? null : child.kill()));
66     }
67     }
68    
69     async function fetchCredentials() {
70     if (!process.env.CREDENTIAL_SERVER) {
71     return;
72     }
73    
74     const restartJsonFile = path.join(sudoPrefix("tmp", true), "restart.json");
75     let restartKey = null;
76    
77     if (existsSync(restartJsonFile)) {
78     logInfo("Found restart.json file: ", restartJsonFile);
79    
80     try {
81     const { key } = JSON.parse(readFileSync(restartJsonFile, { encoding: "utf-8" }));
82     restartKey = key;
83     } catch (error) {
84     logError(error);
85     }
86     }
87    
88     const index = process.argv.indexOf("--key");
89     let key = restartKey ?? (index !== -1 ? process.argv[index + 1] : null);
90    
91     if (!key) {
92     const readline = createInterface(process.stdin, process.stdout);
93     key = await readline.question("Enter the one-time 2FA code: ");
94     readline.close();
95     } else if (restartKey) {
96     logInfo("Accepted 2FA code during last restart command");
97     } else {
98     logInfo("Accepted 2FA code from command-line arguments");
99     }
100    
101     logInfo("Authenticating with the server...");
102    
103     const is2FACode = key.length === 6 && !isNaN(Number(key));
104    
105     try {
106     const response = await axios.get(process.env.CREDENTIAL_SERVER, {
107     headers: {
108     Authorization: is2FACode ? undefined : `Bearer ${key}`,
109     "X-2FA-code": is2FACode ? key : undefined
110     }
111     });
112    
113     if (
114     response.data?.success &&
115     response.data?.config &&
116     typeof response.data?.config === "object"
117     ) {
118     logSuccess(
119     "Successfully authenticated with the credentials server (Method: " +
120     (is2FACode ? "2FA" : "Key") +
121     ")"
122     );
123    
124     for (const key in response.data.config) {
125     process.env[key] = response.data.config[key];
126     }
127     } else {
128     throw new Error("Invalid response received");
129     }
130     } catch (error) {
131     logError(error);
132     process.exit(-1);
133     }
134     }
135    
136     const promise = (async () => {
137     await fetchCredentials();
138     const client = new Client({
139     intents,
140     partials
141     });
142    
143     spawnNativeProcess();
144    
145     await client.boot();
146    
147     if (process.env.SERVER_ONLY_MODE) {
148     await client.server.boot();
149     await client.server.start();
150     } else {
151     await client.login(process.env.TOKEN);
152     }
153     })();
154    
155     export default promise;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26