{"id":17347062,"url":"https://github.com/brussell98/megane","last_synced_at":"2025-04-14T20:46:04.166Z","repository":{"id":54491591,"uuid":"114804934","full_name":"brussell98/megane","owner":"brussell98","description":"A framework for large multi-process Discord bots","archived":false,"fork":false,"pushed_at":"2022-09-21T00:56:51.000Z","size":476,"stargazers_count":32,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T13:16:43.393Z","etag":null,"topics":["bot","clusters","discord","eris","ipc","sharding","worker"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brussell98.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":"FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"brussell98","open_collective":null,"ko_fi":"brussell98","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-12-19T19:47:29.000Z","updated_at":"2025-03-08T20:56:17.000Z","dependencies_parsed_at":"2023-01-18T17:16:29.526Z","dependency_job_id":null,"html_url":"https://github.com/brussell98/megane","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brussell98%2Fmegane","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brussell98%2Fmegane/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brussell98%2Fmegane/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brussell98%2Fmegane/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brussell98","download_url":"https://codeload.github.com/brussell98/megane/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248960709,"owners_count":21189987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","clusters","discord","eris","ipc","sharding","worker"],"created_at":"2024-10-15T16:47:28.426Z","updated_at":"2025-04-14T20:46:04.138Z","avatar_url":"https://github.com/brussell98.png","language":"TypeScript","funding_links":["https://patreon.com/brussell98","https://ko-fi.com/brussell98"],"categories":["Libraries"],"sub_categories":["JavaScript"],"readme":"# Megane\n\nA sharding manager for Discord bots. Megane distributes your shards across (logical) CPU cores. Megane uses [Eris](https://github.com/abalabahaha/eris) to interface with the Discord API. Based on [Kurasuta](https://github.com/DevYukine/Kurasuta).\n\n## Features\n\n- Automatic clustering and sharding of your bot across CPU cores\n- Central data store that all clusters can access and modify\n- Simple and extensible way to get users, channels, and guilds from other clusters\n- Run eval over IPC\n- Restart individual clusters instead of the whole bot at once\n- Create worker processes to interact with APIs or do other expensive or central tasks\n- Automatic and extensible collection of statistics\n\nFeatures to add before 1.0:\n\n- (IPC) Optional messages in text channels\n- Change cpuUsage recording to return diff since last check\n\nFeatures to be added after 1.0:\n\n- Rolling restart helper\n- Re-sharding\n- Improve IPC usability (need suggestions)\n\n## Considerations\n\nSplitting 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:\n\n- Developing will be more complicated due to the need to use ipc and lack of a complete local cache\n- Fetching users, channels, guilds, or other structures from other processes will remove the ability to use methods without manually recreating them. Certain properties may also be missing after converting to plain objects\n\n## Using\n\nPackage name: `@brussell98/megane`\n\n*You can see working examples by browsing the `examples` folder*\n\n### Getting Started\n\nYou will have at least two main files:\n1. A file for your \"master\" process, which creates a new `ShardManager`. We will call this file \"index.js\"\n2. A file for your \"cluster\" process (the worker), which extends `BaseClusterWorker`. We will call this file \"bot.js\"\n\nIn your `index.js` file you should have some code similar to this:\n```js\nconst { isMaster } = require('cluster');\nconst { ShardManager } = require('@brussell98/megane');\n\nconst manager = new ShardManager({\n\tpath: __dirname + '/bot.js',\n\ttoken: 'Your bot token'\n});\n\nmanager.spawn(); // You should await this\n\nif (isMaster) {\n\t// Master process code here\n\tmanager.on('error', error =\u003e console.error(error)); // Not handling these errors will kill everything when any error is emitted\n\tmanager.on('debug', message =\u003e console.log(message));\n\tmanager.on('clusterReady', cluster =\u003e console.log(`Cluster ${cluster.id} ready`));\n\tmanager.once('allClustersReady', () =\u003e console.log('All clusters ready'));\n}\n```\nThis 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.\n\nNote 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.\n\nNext, your `bot.js` file should implement a ClusterWorker, like so:\n```js\nconst { BaseClusterWorker } = require('@brussell98/megane');\n\nmodule.exports = class BotWorker extends BaseClusterWorker {\n\tconstructor(manager) {\n\t\tsuper(manager);\n\t}\n\n\tasync launch() {\n\t\t// Anything you want to run when the worker starts\n\t\t// This is run after the IPC is initialized\n\t\tawait this.client.connect(); // Connect eris to Discord\n\t}\n}\n```\n\n### Services\n\nIn 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:\n```js\nif (isMaster) {\n\t// ...\n\tmanager.on('serviceReady', service =\u003e console.log(`Service ${service.name} ready`));\n\n\tmanager.registerService(__dirname + '/service.js', { name: 'example-service', timeout: 60e3 });\n}\n```\nThis will register a service named \"example-service\" and spawn a worker. The service worker is implemented similarly to a cluster worker.\n\nHere is an example of what your `service.js` file should look like:\n```js\nconst { BaseServiceWorker } = require('@brussell98/megane');\n\nmodule.exports = class ServiceWorker extends BaseServiceWorker {\n\tconstructor(manager) {\n\t\tsuper(manager);\n\t}\n\n\tasync launch() {\n\t\t// Anything you want to run when the worker starts\n\t\t// This is run after the IPC is initialized\n\t\tawait this.sendReady(); // Required to be sent before `timeout`\n\t}\n\n\t// Handles SERVICE_COMMAND events\n\thandleCommand(data, receptive) {\n\t\tif (data.op === 'PING')\n\t\t\treturn receptive \u0026\u0026 this.asResponse('PONG');\n\n\t\tif (receptive)\n\t\t\treturn this.asError(new Error('Unknown command'));\n\t}\n}\n```\nYour bot can then send commands like this:\n```js\nconst reply = await this.ipc.sendCommand('example-service', { op: 'PING' }, { receptive: true });\nconsole.log(reply); // PONG\n```\n\n### Diagram\n\n```\nMaster:\nindex.js -\u003e ShardManager -\u003e Clusters\n\t\t\t\t\t\t -\u003e Services\n\t\t\t\t\t\t -\u003e MasterIPC\n\nClusterWorker:\nindex.js -\u003e ShardManager -\u003e bot.js -\u003e ClusterWorkerIPC\n\nServiceWorker:\nindex.js -\u003e ShardManager -\u003e service.js -\u003e ServiceWorkerIPC\n```\n\n### Documentation\n\nRefer to [DOCUMENTATION.md](DOCUMENTATION.md)\n\n### Changelog\n\nRefer to [CHANGELOG.md](CHANGELOG.md)\n\n## Naming\n\nThis was created to be used for [Mirai Bot for Discord](https://mirai.brussell.me). The bot is named after the anime character [Mirai Kuriyama (栗山 未来)](https://myanimelist.net/character/81751/Mirai_Kuriyama), who notably wears red glasses [[Megane (めがね)](https://jisho.org/word/眼鏡)].\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrussell98%2Fmegane","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrussell98%2Fmegane","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrussell98%2Fmegane/lists"}