-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (88 loc) · 2.33 KB
/
Copy pathindex.js
File metadata and controls
105 lines (88 loc) · 2.33 KB
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
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { setupMaster, fork } from 'cluster'
import { watchFile, unwatchFile } from 'fs'
import { createInterface } from 'readline'
import cfonts from 'cfonts'
import yargs from 'yargs'
import './config.js'
import checkUpdate from './lib/system.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const { say } = cfonts
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
/**
* Fungsi update
*/
async function runUpdater() {
if (!global.config?.cekupdate) return
console.log('Checking for updates...')
try {
await checkUpdate()
} catch (e) {
console.error(`[UPDATE ERROR] ${e.message}`)
}
}
/**
* UI
*/
function displayHeader() {
say(global.config.namebot || 'BOT', {
font: 'pallet',
align: 'center',
colors: ['white']
})
say(`⧻ ${global.config.namebot} by ${global.config.author}`, {
font: 'console',
align: 'center',
colors: ['cyan']
})
}
let isRunning = false
/**
* Fungsi Utama Bot
*/
async function start(file) {
if (isRunning) return
isRunning = true
const args = [join(__dirname, file), ...process.argv.slice(2)]
say('⸙ INITIALIZING SOURCE...', { font: 'console', align: 'center', colors: ['magenta'] })
setupMaster({
exec: args[0],
args: args.slice(1),
})
const p = fork()
// Event Handling
p.on('message', data => {
console.log(`[SYSTEM] Received: ${data}`)
switch (data) {
case 'reset':
p.kill()
isRunning = false
start(file)
break
case 'uptime':
p.send(process.uptime())
break
}
})
p.on('exit', (code) => {
isRunning = false
console.error(`[!] Process exited with code: ${code}`)
if (code === 0) return
watchFile(args[0], () => {
unwatchFile(args[0])
start(file)
})
})
const opts = yargs(process.argv.slice(2)).exitProcess(false).parse()
if (!opts.test && rl.listenerCount('line') === 0) {
rl.on('line', line => {
p.emit('message', line.trim())
})
}
}
displayHeader()
runUpdater().then(() => start('main.js'))