This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·151 lines (145 loc) · 5.52 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
143
144
145
146
147
148
149
150
151
/* eslint no-use-before-define: 0 */
if (parseInt(process.versions.node.split('.')[0]) < 10) {
console.error('ERROR: You are running on Node.js v%s, but not supported this version!', process.versions.node.split('.')[0])
process.exit(1)
}
require('./src/yaml')
const logger = require('./src/logger').getLogger('main', 'green')
const args = require('./src/argument_parser')(process.argv.slice(2))
const app = require('./config')
logger.info('Loaded core modules')
if (process.pid === 1 || args.debug.pid1) {
logger.warn('=============== WARNING ===============')
.warn('PID is 1, it may occur unexpected behavior! (And not supported)')
.warn('=======================================')
}
if (args.debugg) logger.debug('You enabled debug option, and you\'ll see debug messages.');
(async () => {
const util = require('./src/util')
const fs = require('fs').promises
if (await util.exists(__dirname + '/src/secret.yml')) {
logger.info('Converting config')
const data = await fs.readFile(__dirname + '/src/secret.yml')
const configdata = await fs.readFile(__dirname + '/src/config.yml')
await fs.appendFile(__dirname + '/src/config.yml', data)
await fs.writeFile(__dirname + '/src/config.old.yml', configdata)
await fs.rename(__dirname + '/src/secret.yml', __dirname + '/src/secret.old.yml')
logger.warn('Completed converting, but Is this ok?')
.warn('Please check "config.yml" carefully.')
.info('Please re-run this when you have checked config file.')
process.exit()
}
const config = require(__dirname + '/src/config.yml')
if (!config.patroninvite) {
let data = ''
data += '\n# Patron bot invite - Do not edit if you aren\'t self hosted patron bot.\n'
data += 'patroninvite: https://discordapp.com/oauth2/authorize?client_id=469114408501510166&permissions=8&redirect_uri=https%3A%2F%2Fapi.rht0910.tk%2Fpatronbot&scope=bot&response_type=code\n'
fs.appendFile(__dirname + '/src/config.yml', data)
}
if (!config.config_version) {
let data = ''
data += '\n# DO NOT EDIT CONFIG VERSION ANYWAY.\n'
data += 'config_version: \'1.0\'\n'
fs.appendFile(__dirname + '/src/config.yml', data)
}
if (config.config_version) {
logger.info(`Checking for config version [config:${config.config_version}, wanted: ${app.wanted_configversion}]`)
/**
* Actual version
*/
const i1 = parseInt(config.config_version.replace(/\./gm, ''))
/**
* Expected version
*/
const i2 = parseInt(app.wanted_configversion.replace(/\./gm, ''))
if (i1 < i2) {
const migrate = require(__dirname + '/src/config_migrate')
logger.warn(`Your config version is outdated! (${config.config_version} < ${app.wanted_configversion})`)
if (migrate.versions[`${config.config_version}-to-${app.wanted_configversion}`]) {
logger.info(`Available update script: ${config.config_version}-to-${app.wanted_configversion}`)
const result = await migrate.versions[`${config.config_version}-to-${app.wanted_configversion}`]()
if (result) {
logger.info('You need to review the your config.')
.info('Please edit your config and re-run this.')
.warn('IMPORTANT: Check your config.yml carefully(single/double quotations).')
process.exit()
} else {
logger.info('Fully upgraded, and no review(s) needed.')
}
} else {
logger.warn('No update scripts available.')
}
} else if (i1 > i2) {
logger.warn('Your config version is greater than expected version!')
.warn('Are you time traveller? (or bug?)')
} else {
logger.info('Config version is up to date.')
}
}
logger.info('Checking for version')
await require('./src/versioncheck')()
})()
logger.info('Starting')
const { fork } = require('child_process')
let spawned
let restart = false
const spawn = () => spawned = fork('src/client', process.argv.slice(2))
const register = () => {
spawned.on('error', e => {
clearInterval(timer)
logger.emerg('Failed to start Bot: ')
logger.emerg(e.stack)
process.exit(1)
})
spawned.on('close', (code) => {
if (!restart) clearInterval(timer)
if (code === 0) logger.info(`Bot exited: ${code}`)
else logger.emerg(`Bot exited with unexpected code: ${code}`)
if (!restart) process.exit(code)
spawned.kill('SIGKILL')
restart = false
})
const KILLINTHandler = () => {
clearInterval(timer)
logger.info('Caught SIGINT')
logger.info('Stopping bot')
setTimeout(() => {
spawned.kill('SIGKILL')
}, 5000)
if (spawned) spawned.send('stop')
else logger.error('Can\'t send message to client')
}
spawned.on('message', msg => {
if (msg !== 'ping' && msg !== 'stop') process.stdout.write(msg)
if (msg === 'stop') KILLINTHandler()
})
process.on('SIGINT', KILLINTHandler)
process.on('SIGTERM', KILLINTHandler)
process.on('SIGHUP', KILLINTHandler)
}
let times = 0
const heartbeat = async () => {
let received = false
const handler = (msg) => {
if (msg === 'ping') received = true
}
if (spawned) spawned.send('heartbeat')
else logger.error('Can\'t send heartbeat')
spawned.once('message', handler)
setTimeout(() => {
if (!received) {
if (times >= 1) {
logger.emerg('Looks like client is unusable(not responding), killing client, and attmpting restart')
restart = true
spawned.kill('SIGKILL')
} else {
times = times + 1
}
}
received = false
spawned.removeListener('message', handler)
}, 250)
}
const timer = setInterval(heartbeat, 10000)
spawn()
register()