This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from pyraxo/dev
0.2.0
- Loading branch information
Showing
27 changed files
with
508 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
engines: | ||
duplication: | ||
enabled: false | ||
eslint: | ||
enabled: false | ||
ratings: | ||
paths: | ||
- "**.js" | ||
exclude_paths: | ||
- test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
let Promise | ||
let EventEmitter | ||
try { | ||
Promise = require('bluebird') | ||
} catch (err) { | ||
Promise = global.Promise | ||
} | ||
try { | ||
EventEmitter = require('eventemitter3') | ||
} catch (err) { | ||
EventEmitter = require('events') | ||
} | ||
|
||
const path = require('path') | ||
|
||
const { Collection, delay } = require('../util') | ||
|
||
/** | ||
* Shard cluster manager | ||
* @prop {Collection} clusters Collection of clusters | ||
*/ | ||
class Crystal extends EventEmitter { | ||
/** | ||
* Creates a new Crystal instance | ||
* @arg {String} file Relative or absolute path to file to run | ||
* @arg {Number} [count] Number of clusters to create, defaults to number of CPU cores | ||
*/ | ||
constructor (file, count = require('os').cpus().length) { | ||
super() | ||
this.clusters = new Collection() | ||
this._count = count | ||
this._file = path.isAbsolute(file) ? file : path.join(process.cwd(), file) | ||
} | ||
|
||
/** Spawns new clusters */ | ||
async createClusters () { | ||
for (let i = 0; i < this._count; i++) { | ||
this.createCluster(i) | ||
await delay(6000) | ||
} | ||
} | ||
|
||
/** | ||
* Spawns a cluster | ||
* @arg {Number} id Cluster ID | ||
*/ | ||
createCluster (id) { | ||
const cluster = new (require('../structures')).Cluster(this._file, id) | ||
const worker = cluster.worker | ||
worker.on('exit', this.onExit.bind(this, worker)) | ||
worker.on('message', this.onMessage.bind(this, worker)) | ||
this.clusters.set(cluster.id, cluster) | ||
} | ||
|
||
/** | ||
* Fetches a cluster | ||
* @arg {Number} pid Process ID to find | ||
*/ | ||
getCluster (pid) { | ||
return this.clusters.find(s => s.pid === pid) | ||
} | ||
|
||
onExit (worker) { | ||
const cluster = this.getCluster(worker) | ||
this.emit('clusterExit', worker.pid, cluster.id) | ||
this.clusters.delete(cluster.id) | ||
this.createCluster(cluster.id) | ||
} | ||
|
||
onMessage (worker, message) { | ||
if (!message.op) return | ||
if (message.op === 'resp') return | ||
if (this[message.op]) { | ||
return this[message.op](message) | ||
} | ||
|
||
this.awaitResponse(worker, message) | ||
} | ||
|
||
awaitResponse (worker, message) { | ||
const promises = [] | ||
|
||
for (const cluster of this.clusters.values()) { | ||
promises.push(cluster.awaitResponse(message)) | ||
} | ||
|
||
Promise.all(promises) | ||
.then(results => worker.send({ op: 'resp', d: results, code: message.code })) | ||
.catch(err => worker.send({ op: 'error', d: err, code: message.code })) | ||
} | ||
|
||
broadcast (message) { | ||
if (message.op === 'broadcast') { | ||
message = message.d | ||
} | ||
|
||
for (const cluster of this.clusters.values()) { | ||
cluster.worker.send(message) | ||
} | ||
} | ||
|
||
/** | ||
* Restarts all clusters, or a specific one | ||
* @arg {Object} [message] The message sent | ||
* @arg {Number} [message.d] The cluster ID to restart | ||
*/ | ||
async restart (message = {}) { | ||
if (typeof message.d === 'number') { | ||
const cluster = this.clusters.get(message.d) | ||
if (!cluster) return | ||
cluster.worker.kill() | ||
} else { | ||
for (let cluster of this.clusters.values()) { | ||
cluster.worker.kill() | ||
await Promise.delay(6000) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = Crystal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
Resolver: require('./Resolver'), | ||
Transmitter: require('./Transmitter'), | ||
Responder: require('./Responder') | ||
Responder: require('./Responder'), | ||
Crystal: require('./Crystal') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
type: 'channel', | ||
resolve: (content, { text = true, voice = true } = {}, msg) => { | ||
const guild = msg.guild | ||
content = String(content).toLowerCase() | ||
let channel = content.match(/^<#?(\d{17,18})>$/) | ||
if (!channel) { | ||
let channels = guild.channels.filter(c => { | ||
if (text && c.type !== 0) return | ||
if (voice && c.type !== 2) return | ||
const name = c.name.toLowerCase() | ||
return name === content || name.includes(content) | ||
}) | ||
if (channels.length) { | ||
return Promise.resolve(channels) | ||
} else { | ||
return Promise.reject('channel.NOT_FOUND') | ||
} | ||
} else { | ||
let chan = guild.channels.get(channel[1]) | ||
if (!chan) return Promise.reject('channel.NOT_FOUND') | ||
return Promise.resolve([chan]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
type: 'command', | ||
resolve: (content, args, msg, { engine }) => { | ||
const command = engine.commands.get(content.toLowerCase()) | ||
return !command || | ||
((command.options || {}).adminOnly && !process.env.ADMIN_IDS.split(', ').includes(msg.author.id)) | ||
? Promise.reject('command.NOT_FOUND') | ||
: Promise.resolve(command) | ||
} | ||
} |
Oops, something went wrong.