-
Notifications
You must be signed in to change notification settings - Fork 147
/
index.js
142 lines (126 loc) · 3.79 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
import { readFileSync, writeFileSync, existsSync, statSync } from "fs";
import { spawn, execSync } from "child_process";
import semver from "semver";
import axios from "axios";
import {} from "dotenv/config";
import logger from "./core/var/modules/logger.js";
import { loadPlugins } from "./core/var/modules/installDep.js";
import {
isGlitch,
isReplit,
isGitHub,
} from "./core/var/modules/environments.get.js";
console.clear();
// Install newer node version on some old Repls
function upNodeReplit() {
return new Promise((resolve) => {
execSync(
"npm i --save-dev node@16 && npm config set prefix=$(pwd)/node_modules/node && export PATH=$(pwd)/node_modules/node/bin:$PATH"
);
resolve();
});
}
(async () => {
if (process.version.slice(1).split(".")[0] < 16) {
if (isReplit) {
try {
logger.warn("Installing Node.js v16 for Repl.it...");
await upNodeReplit();
if (process.version.slice(1).split(".")[0] < 16)
throw new Error("Failed to install Node.js v16.");
} catch (err) {
logger.error(err);
process.exit(0);
}
}
logger.error(
"Xavia requires Node 16 or higher. Please update your version of Node."
);
process.exit(0);
}
if (isGlitch) {
const WATCH_FILE = {
restart: {
include: ["\\.json"],
},
throttle: 3000,
};
if (
!existsSync(process.cwd() + "/watch.json") ||
!statSync(process.cwd() + "/watch.json").isFile()
) {
logger.warn("Glitch environment detected. Creating watch.json...");
writeFileSync(
process.cwd() + "/watch.json",
JSON.stringify(WATCH_FILE, null, 2)
);
execSync("refresh");
}
}
if (isGitHub) {
logger.warn("Running on GitHub is not recommended.");
}
})();
// End
// CHECK UPDATE
async function checkUpdate() {
logger.custom("Checking for updates...", "UPDATE");
try {
const res = await axios.get(
"https://github.com/raw/XaviaTeam/XaviaBot/main/package.json"
);
const { version } = res.data;
const currentVersion = JSON.parse(
readFileSync("./package.json")
).version;
if (semver.lt(currentVersion, version)) {
logger.warn(`New version available: ${version}`);
logger.warn(`Current version: ${currentVersion}`);
} else {
logger.custom("No updates available.", "UPDATE");
}
} catch (err) {
logger.error("Failed to check for updates.");
}
}
// Child handler
const _1_MINUTE = 60000;
let restartCount = 0;
async function main() {
await checkUpdate();
await loadPlugins();
const child = spawn(
"node",
[
"--trace-warnings",
"--experimental-import-meta-resolve",
"--expose-gc",
"core/_build.js",
],
{
cwd: process.cwd(),
stdio: "inherit",
env: process.env,
}
);
child.on("close", async (code) => {
handleRestartCount();
if (code !== 0 && restartCount < 5) {
console.log();
logger.error(`An error occurred with exit code ${code}`);
logger.warn("Restarting...");
await new Promise((resolve) => setTimeout(resolve, 2000));
main();
} else {
console.log();
logger.error("XaviaBot has stopped, press Ctrl + C to exit.");
}
});
}
function handleRestartCount() {
restartCount++;
setTimeout(() => {
restartCount--;
}, _1_MINUTE);
}
main();