Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@brussell98/megane
Advanced tools
A sharding manager for Discord bots. Megane distributes your shards across (logical) CPU cores. Megane uses Eris to interface with the Discord API. Based on Kurasuta.
Features to add before 1.0:
Features to be added after 1.0:
Splitting your bot into multiple processes should not be done unless needed. Megane has some downsides compared to how a normal bot works. Make sure you consider these first:
Package name: @brussell98/megane
You can see working examples by browsing the examples
folder
You will have at least two main files:
ShardManager
. We will call this file "index.js"BaseClusterWorker
. We will call this file "bot.js"In your index.js
file you should have some code similar to this:
const { isMaster } = require('cluster');
const { ShardManager } = require('@brussell98/megane');
const manager = new ShardManager({
path: __dirname + '/bot.js',
token: 'Your bot token'
});
manager.spawn(); // You should await this
if (isMaster) {
// Master process code here
manager.on('error', error => console.error(error)); // Not handling these errors will kill everything when any error is emitted
manager.on('debug', message => console.log(message));
manager.on('clusterReady', cluster => console.log(`Cluster ${cluster.id} ready`));
manager.once('allClustersReady', () => console.log('All clusters ready'));
}
This will create a new ShardManager
which will run bot.js
on separate processes. The worker file (bot.js
) must implement a BaseClusterWorker
. This will be demonstrated next.
Note the isMaster
block. Your index.js file will be run each time a worker is created. Any code you only want to run on the master process must check if it's running on the master process.
Next, your bot.js
file should implement a ClusterWorker, like so:
const { BaseClusterWorker } = require('@brussell98/megane');
module.exports = class BotWorker extends BaseClusterWorker {
constructor(manager) {
super(manager);
}
async launch() {
// Anything you want to run when the worker starts
// This is run after the IPC is initialized
await this.client.connect(); // Connect eris to Discord
}
}
In many cases you will have tasks that are used by all clusters. One example is updating Twitch API data. You can create a service to handle this and it will run in its own process, accessible by all clusters. Add the following code to your index.js
file:
if (isMaster) {
// ...
manager.on('serviceReady', service => console.log(`Service ${service.name} ready`));
manager.registerService(__dirname + '/service.js', { name: 'example-service', timeout: 60e3 });
}
This will register a service named "example-service" and spawn a worker. The service worker is implemented similarly to a cluster worker.
Here is an example of what your service.js
file should look like:
const { BaseServiceWorker } = require('@brussell98/megane');
module.exports = class ServiceWorker extends BaseServiceWorker {
constructor(manager) {
super(manager);
}
async launch() {
// Anything you want to run when the worker starts
// This is run after the IPC is initialized
await this.sendReady(); // Required to be sent before `timeout`
}
// Handles SERVICE_COMMAND events
handleCommand(data, receptive) {
if (data.op === 'PING')
return receptive && this.asResponse('PONG');
if (receptive)
return this.asError(new Error('Unknown command'));
}
}
Your bot can then send commands like this:
const reply = await this.ipc.sendCommand('example-service', { op: 'PING' }, { receptive: true });
console.log(reply); // PONG
Master:
index.js -> ShardManager -> Clusters
-> Services
-> MasterIPC
ClusterWorker:
index.js -> ShardManager -> bot.js -> ClusterWorkerIPC
ServiceWorker:
index.js -> ShardManager -> service.js -> ServiceWorkerIPC
Refer to DOCUMENTATION.md
Refer to CHANGELOG.md
This was created to be used for Mirai Bot for Discord. The bot is named after the anime character Mirai Kuriyama (栗山 未来), who notably wears red glasses [Megane (めがね)].
FAQs
A cluster management system for Discord bots
We found that @brussell98/megane demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.