-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (61 loc) · 3.18 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
import { XernerxShardClient } from 'xernerx';
import * as fs from 'fs';
import { exec } from 'child_process';
import { promisify } from 'util';
import config from './data/config.json' assert { type: 'json' };
import log from './models/log.js';
const shell = promisify(exec);
const bots = Object.entries(config.tokens);
const data = {};
const dataLog = JSON.parse(fs.readFileSync('./data/data.json', 'utf-8'));
(async () => {
await shell('mkdir bots').catch((e) => e);
for (const [bot, token] of bots) {
if (!token) {
log(`No token for ${bot} provided. Skipping...`, 'error');
continue;
}
log(`Starting ${bot}!`);
log(`Updating ${bot} code`);
fs.writeFileSync(
`./bots/${bot}.js`,
`import { Client } from '../main.js';\nimport config from '../data/config.json' assert {type:'json'};\n\nconst client = new Client();\nclient.login(config.tokens.${bot});`
);
let { shards, stats } = new XernerxShardClient(`./bots/${bot}.js`, { token, respawn: true }, { log: { info: true } });
shards = await shards;
const collector = setInterval(() => {
if (shards.get(shards.size - 1).ready) {
clearInterval(collector);
data[bot] = stats;
if (bots.filter((bot) => bot.at(1)).length == Object.keys(data).length) {
console.table(data);
dataLog[Number(Date.now())] = data;
// ! _______________________________________________________
// * Checks all pre-existing bots that have recorded data.
let botList = Object.keys(dataLog[Object.keys(dataLog)[Object.keys(data).length - 1]]);
// * Checks for new bots, if so, add it to the botList.
Object.keys(data).map((bot) => (!botList.includes(bot) ? botList.push(bot) : null));
// * Sorts the botList, as it's defaulted to the token order provided.
botList = botList.sort();
// * Make a new object that'll sort the data values correctly, otherwise I'd had to work with delete key (that's a good idea...)
Object.entries(dataLog).map(([timestamp, data]) => {
const botData = {};
botList.map((bot) => {
if (data[bot]) botData[bot] = data[bot];
else botData[bot] = {};
});
// * Assign the new sorted data object to the correct timestamp.
dataLog[timestamp] = botData;
});
// ! _______________________________________________________
fs.writeFileSync('./data/data.json', JSON.stringify(dataLog), console.error);
log('Ending processes...');
new Promise((resolve) => setTimeout(resolve, 1000)).then(() => {
log(`Processes ended.`);
process.exit();
});
} else log(`Done with ${bot}, loading next one...`);
}
}, 1000);
}
})();