mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Add ShardManager
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -136,9 +136,8 @@ class WebSocketManager {
|
||||
const payload = this.client.options.ws;
|
||||
payload.token = this.client.token;
|
||||
if (this.client.options.shard_count > 0) {
|
||||
payload.shard = [this.client.options.shard_id, this.client.options.shard_count];
|
||||
payload.shard = [Number(this.client.options.shard_id), Number(this.client.options.shard_count)];
|
||||
}
|
||||
|
||||
this.send({
|
||||
op: Constants.OPCodes.IDENTIFY,
|
||||
d: payload,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const Client = require('./client/Client');
|
||||
const ShardingManager = require('./sharding/ShardingManager');
|
||||
|
||||
exports.Client = Client;
|
||||
exports.ShardingManager = ShardingManager;
|
||||
|
||||
34
src/sharding/ShardingManager.js
Normal file
34
src/sharding/ShardingManager.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const childProcess = require('child_process');
|
||||
const path = require('path');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
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.runningShards = 0;
|
||||
this.createShards();
|
||||
}
|
||||
|
||||
createShard(id) {
|
||||
const shard = childProcess.fork(path.resolve(this.file), [id, this.totalShards]);
|
||||
this.emit('launch', id, shard);
|
||||
}
|
||||
|
||||
createShards() {
|
||||
this.createShard(0);
|
||||
const interval = setInterval(() => {
|
||||
this.runningShards++;
|
||||
if (this.runningShards === this.totalShards) {
|
||||
return clearInterval(interval);
|
||||
}
|
||||
this.createShard(this.runningShards);
|
||||
}, 5500);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ShardingManager;
|
||||
24
test/shard.js
Normal file
24
test/shard.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const Discord = require('../');
|
||||
const { token } = require('./auth.json');
|
||||
|
||||
const client = new Discord.Client({
|
||||
shard_id: process.argv[2],
|
||||
shard_count: process.argv[3],
|
||||
});
|
||||
|
||||
client.on('message', msg => {
|
||||
if (msg.content.startsWith('?eval') && msg.author.id === '66564597481480192') {
|
||||
try {
|
||||
const com = eval(msg.content.split(' ').slice(1).join(' '));
|
||||
msg.channel.sendMessage('```\n' + com + '```');
|
||||
} catch (e) {
|
||||
msg.channel.sendMessage('```\n' + e + '```');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log('Ready');
|
||||
});
|
||||
|
||||
client.login(token).catch(console.log);
|
||||
5
test/sharder.js
Normal file
5
test/sharder.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const Discord = require('../');
|
||||
|
||||
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`, 5);
|
||||
|
||||
sharder.on('launch', id => console.log(`launched ${id}`));
|
||||
Reference in New Issue
Block a user