-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
81 lines (71 loc) · 1.77 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
const start = require('./src/index.js');
const setupAndStart = function(key, args) {
let dev = false;
let config = {
key: key,
debug: false,
verboose: false
};
for (let i=0;i<args.length;i++) {
switch(args[i]) {
case '-d':
case '--debug':
config.debug = true;
break;
case '-v':
case '--verboose':
config.verboose = true;
break;
case '--dev':
dev = true;
break;
case '-p':
case '--prefix':
config.prefix = args[++i];
break;
case '--key':
config.key = args[++i];
break;
case '--owner':
config.owner = args[++i];
break;
case '--admin':
config.adminFlag = args[++i];
break;
}
}
if (!dev) return start(config).then(null).catch(console.error);
const fs = require('fs');
let stop, restarting = false;
fs.watch('src', { recursive: true }, async function() {
if (restarting) return;
console.log('\nRestarting\n==========');
restarting = true;
if (stop) await stop();
stop = undefined;
await devStart();
restarting = false;
});
const devStart = async function() {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);
let startFunc = require('./src/index.js'), client;
try {
client = await startFunc(config);
} catch(e) {
console.log(e);
return await devStart();
}
client.on('warn', console.warn);
client.on('error', console.error);
stop = client.destroy.bind(client);
}
process.on('beforeExit', async function() {
if (stop) await stop();
})
devStart().then(null).catch(console.error);
}
if (require.main === module) setupAndStart(process.env.DISCORD_API_KEY, process.argv);
module.exports = {
setupAndStart,
start
};