-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlavalink.js
72 lines (56 loc) · 1.97 KB
/
lavalink.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
const https = require('https')
const fs = require('fs')
let application = fs.readFileSync('./application.yml', 'utf8');
if (process.env.PORT) {
application = application.replace('DYNAMICPORT', process.env.PORT)
}
if (process.env.PASSWD) {
application = application.replace('youshallnotpass', process.env.PASSW)
}
fs.writeFileSync('./application.yml', application);
const download = function (url, dest, cb) {
const file = fs.createWriteStream(dest);
https.get(url, function (response) {
response.pipe(file);
console.log('Downloading Lavalink.jar')
file.on('finish', function () {
console.log('Downloaded Lavalink.jar')
file.close(cb);
});
}).on('error', function (err) {
fs.unlinkSync(dest);
console.error(err)
});
};
function startLavalink() {
const spawn = require('child_process').spawn;
const child = spawn('java', ['-jar', 'Lavalink.jar'])
child.stdout.setEncoding("utf8")
child.stderr.setEncoding("utf8")
child.stdout.on('data', (data) => {
console.log(data);
});
child.stderr.on('data', (data) => {
console.error(data);
});
child.on('error', (error) => {
console.error(error);
});
child.on('close', (code) => {
console.log(`Lavalink exited with code ${code}`);
});
if (process.env.APP_NAME)
keepAlive();
}
const cdn = 'https://ci.fredboat.com/repository/download/Lavalink_Build/.lastSuccessful/Lavalink.jar?guest=1&branch=refs/heads/dev'
download(cdn, './Lavalink.jar', startLavalink);
function keepAlive() {
console.log('Keeping alive.');
const fetch = require('node-fetch');
let count = 0;
setInterval(() =>
fetch(`http://${process.env.APP_NAME}/`, { headers: { Authorization: process.env.PASSW } })
.then(() => console.log(`[${++count}] Kept server alive.`))
.catch(() => console.log(`Failed to keep server alive.`))
, 20 * 60 * 1000);
}