mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-02 17:10:08 +00:00
40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
const childProcess = require('child_process');
|
|
const path = require('path');
|
|
const EventEmitter = require('events').EventEmitter;
|
|
const Collection = require('../util/Collection');
|
|
const Shard = require('./Shard');
|
|
const crypto = require('crypto');
|
|
|
|
class ShardingManager extends EventEmitter {
|
|
constructor(file, totalShards) {
|
|
super();
|
|
this.file = file;
|
|
if (!path.isAbsolute(file)) {
|
|
this.file = path.resolve(`${process.cwd()}${file}`);
|
|
}
|
|
this.totalShards = totalShards;
|
|
this.shards = new Collection();
|
|
this.waiting = new Collection();
|
|
}
|
|
|
|
createShard() {
|
|
const id = this.shards.size;
|
|
const shard = new Shard(this, id);
|
|
this.shards.set(id, shard);
|
|
this.emit('launch', id, shard);
|
|
}
|
|
|
|
spawn(amount) {
|
|
this.totalShards = amount;
|
|
this.createShard();
|
|
const interval = setInterval(() => {
|
|
if (this.shards.size === this.totalShards) {
|
|
return clearInterval(interval);
|
|
}
|
|
this.createShard();
|
|
}, 5500);
|
|
}
|
|
}
|
|
|
|
module.exports = ShardingManager;
|