mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
Improve sharding some more
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -39,6 +39,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
throw new TypeError('Amount of shards must be a number.');
|
throw new TypeError('Amount of shards must be a number.');
|
||||||
}
|
}
|
||||||
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
||||||
|
if (this.totalShards !== Math.floor(this.totalShards)) throw new RangeError('Amount of shards must be an integer.');
|
||||||
|
|
||||||
this.respawn = respawn;
|
this.respawn = respawn;
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Spawns a single shard.
|
* Spawns a single shard.
|
||||||
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
|
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
|
||||||
|
* @returns {Promise<Shard>}
|
||||||
*/
|
*/
|
||||||
createShard(id = this.shards.size) {
|
createShard(id = this.shards.size) {
|
||||||
const shard = new Shard(this, id);
|
const shard = new Shard(this, id);
|
||||||
@@ -62,20 +64,37 @@ class ShardingManager extends EventEmitter {
|
|||||||
* @param {Shard} shard Shard that was launched
|
* @param {Shard} shard Shard that was launched
|
||||||
*/
|
*/
|
||||||
this.emit('launch', shard);
|
this.emit('launch', shard);
|
||||||
|
return Promise.resolve(shard);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns multiple shards.
|
* Spawns multiple shards.
|
||||||
* @param {number} [amount=this.totalShards] Number of shards to spawn
|
* @param {number} [amount=this.totalShards] Number of shards to spawn
|
||||||
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
||||||
|
* @returns {Promise<Collection<number, Shard>>}
|
||||||
*/
|
*/
|
||||||
spawn(amount = this.totalShards, delay = 5500) {
|
spawn(amount = this.totalShards, delay = 5500) {
|
||||||
this.totalShards = amount;
|
if (typeof amount !== 'number' || isNaN(amount)) throw new TypeError('Amount of shards must be a number.');
|
||||||
this.createShard();
|
if (amount < 1) throw new RangeError('Amount of shards must be at least 1.');
|
||||||
const interval = setInterval(() => {
|
if (amount !== Math.floor(amount)) throw new RangeError('Amount of shards must be an integer.');
|
||||||
if (this.shards.size === this.totalShards) clearInterval(interval);
|
|
||||||
else this.createShard();
|
return new Promise(resolve => {
|
||||||
}, delay);
|
this.totalShards = amount;
|
||||||
|
this.createShard();
|
||||||
|
|
||||||
|
if (delay <= 0) {
|
||||||
|
while (this.shards.size < this.totalShards) this.createShard();
|
||||||
|
resolve(this.shards);
|
||||||
|
} else {
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
this.createShard();
|
||||||
|
if (this.shards.size >= this.totalShards) {
|
||||||
|
clearInterval(interval);
|
||||||
|
resolve(this.shards);
|
||||||
|
}
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,6 +114,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
* @returns {Promise<number>}
|
* @returns {Promise<number>}
|
||||||
*/
|
*/
|
||||||
fetchGuildCount(timeout = 3000) {
|
fetchGuildCount(timeout = 3000) {
|
||||||
|
if (this.shards.size === 0) return Promise.reject(new Error('No shards have been spawned.'));
|
||||||
if (this.shards.size !== this.totalShards) return Promise.reject(new Error('Still spawning shards.'));
|
if (this.shards.size !== this.totalShards) return Promise.reject(new Error('Still spawning shards.'));
|
||||||
if (this._guildCountPromise) return this._guildCountPromise;
|
if (this._guildCountPromise) return this._guildCountPromise;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user