-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (30 loc) · 1.43 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
(async () => {
// Bot Stuff
const { ShardingManager } = require('discord.js');
const config = require('./config.js');
const axios = require('axios');
const gatewaySettings = await axios.get('https://discord.com/api/gateway/bot', {headers:{Authorization: "Bot "+config.token}}).then(res=>res.data).catch(res=>null);
let totalShards = 'auto';
const guildsPerShard = 950, multipleOf = 3;
if (gatewaySettings.shards) totalShards = Math.ceil((gatewaySettings.shards * (1_000 / guildsPerShard)) / multipleOf) * multipleOf;
if (gatewaySettings?.session_start_limit?.remaining && totalShards != 'auto') {
if(totalShards > gatewaySettings.session_start_limit.remaining)
await new Promise((resolve) => setTimeout(resolve, gatewaySettings.session_start_limit.reset_after));
}
let manager = new ShardingManager('./bot.js', { totalShards, token: config.token });
manager.on('shardCreate', (shard) => console.log(`Shard ${shard.id} launched`));
await manager.spawn();
for (let event of ['SIGINT', 'SIGTERM']) process.on(event, () => {
manager.broadcastEval((c)=>c.stop())
database.raw.close();
redis.disconnect();
process.exit(0);
});
// HTTP Services
for (let event of ['uncaughtException', 'unhandledRejection']) process.on(event, (err) => {
console.error(err);
})
const API = require('./structures/restapi/index');
const api = new API(config, manager);
api.listen(config.restapi.port);
})()