forked from stegripe/rawon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (115 loc) · 4.8 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
/* eslint-disable node/no-sync */
import { execSync } from "node:child_process";
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { Server } from "node:http";
import nodePath from "node:path";
import process from "node:process";
import got from "got";
import prism from "prism-media";
import { extract } from "zip-lib";
import { downloadExecutable } from "./yt-dlp-utils/index.js";
const ensureEnv = arr => arr.every(x => process.env[x] !== undefined);
const isGlitch = ensureEnv([
"PROJECT_DOMAIN",
"PROJECT_INVITE_TOKEN",
"API_SERVER_EXTERNAL",
"PROJECT_REMIX_CHAIN"
]);
const isReplit = ensureEnv([
"REPLIT_DB_URL",
"REPL_ID",
"REPL_IMAGE",
"REPL_LANGUAGE",
"REPL_OWNER",
"REPL_PUBKEYS",
"REPL_SLUG"
]);
const isGitHub = ensureEnv([
"GITHUB_ENV",
"GITHUB_REPOSITORY_OWNER",
"GITHUB_HEAD_REF",
"GITHUB_API_URL",
"GITHUB_REPOSITORY",
"GITHUB_SERVER_URL"
]);
function npmInstall(deleteDir = false, forceInstall = false, additionalArgs = []) {
if (deleteDir) {
const modulesPath = nodePath.resolve(process.cwd(), "node_modules");
if (existsSync(modulesPath)) {
rmSync(modulesPath, {
recursive: true,
force: true
});
}
}
execSync(`npm install${isGlitch ? " --only=prod" : ""}${forceInstall ? " --force" : ""} ${additionalArgs.join(" ")}`);
}
if (isGlitch) {
const gitIgnorePath = nodePath.resolve(process.cwd(), ".gitignore");
try {
const data = readFileSync(gitIgnorePath, "utf8").toString();
if (data.includes("dev.env")) {
writeFileSync(gitIgnorePath, data.replace("\ndev.env", ""));
console.info("Removed dev.env from .gitignore");
}
} catch {
console.error("Failed to remove dev.env from .gitignore");
}
try {
console.info("[INFO] Trying to re-install modules...");
npmInstall();
console.info("[INFO] Modules successfully re-installed.");
} catch {
console.info("[INFO] Failed to re-install modules, trying to delete node_modules and re-install...");
try {
npmInstall(true);
console.info("[INFO] Modules successfully re-installed.");
} catch {
console.info("[INFO] Failed to re-install modules, trying to delete node_modules and install modules forcefully...");
try {
npmInstall(true, true);
console.info("[INFO] Modules successfully re-installed.");
} catch {
console.warn("[WARN] Failed to re-install modules, please re-install manually.");
}
}
}
}
if (isGitHub) {
console.warn("[WARN] Running this bot using GitHub is not recommended.");
}
try {
prism.FFmpeg.getInfo(true);
} catch {
console.info("[INFO] Couldn't find FFmpeg/avconv, trying to install ffmpeg-static...");
npmInstall(false, false, ["--no-save", "ffmpeg-static"]);
console.info("[INFO] ffmpeg-static has been installed.");
}
if (isGlitch || isReplit) {
new Server((req, res) => {
const now = new Date().toLocaleString("en-US");
res.end(`OK (200) - ${now}`);
}).listen(Number(process.env.PORT || 3_000) || 3_000);
console.info(`[INFO] ${isGlitch ? "Glitch" : "Replit"} environment detected, trying to compile...`);
execSync(`npm run compile`);
console.info("[INFO] Compiled.");
}
const streamStrategy = process.env.STREAM_STRATEGY;
if (streamStrategy !== "play-dl") await downloadExecutable();
if (streamStrategy === "play-dl" && !existsSync(nodePath.resolve(process.cwd(), "play-dl-fix"))) {
console.log("[INFO] Downloading play-dl fix...");
writeFileSync(nodePath.resolve(process.cwd(), "temp.zip"), await got.get("https://github.com/YuzuZensai/play-dl-test/archive/2bfbfe6decd68261747ba55800319f9906f12b03.zip").buffer());
console.log("[INFO] Extracting play-dl fix...");
mkdirSync(nodePath.resolve(process.cwd(), "play-dl-fix"), { recursive: true });
await extract(nodePath.resolve(process.cwd(), "temp.zip"), nodePath.resolve(process.cwd(), "play-dl-fix"), { overwrite: true });
const dirs = readdirSync(nodePath.resolve(process.cwd(), "play-dl-fix"));
cpSync(nodePath.resolve(process.cwd(), "play-dl-fix", dirs[0]), nodePath.resolve(process.cwd(), "play-dl-fix"), { force: true, recursive: true });
rmSync(nodePath.resolve(process.cwd(), "play-dl-fix", dirs[0]), { force: true, recursive: true });
rmSync(nodePath.resolve(process.cwd(), "temp.zip"), { force: true });
console.log("[INFO] Installing packages for play-dl...");
execSync("cd play-dl-fix && npm install");
console.log("[INFO] Compiling play-dl...");
execSync("cd play-dl-fix && npm run build");
}
console.info("[INFO] Starting the bot...");
import("./dist/index.js");