YuaSharder is a clustering and sharding library for Discord Eris written in typescript with typings.
It was written explicitly for Yua Bot but I decided to open source it as there are not too many good working libraries for sharding and clustering.
Node Version: ᵗᵇʰ ᶦᵈᵏ ˡᵐᵃᵒ
npm install yuasharder
index.js
const { Manager } = require('yuasharder')
const manager = new Manager("Cool-Discord-Token", "/bot.js", {})
manager.launch()
bot.js
const { Base } = require('yuasharder')
class Bot extends Base {
constructor(props) {
super(props)
}
init() {
console.log("Logged in as", this.client.user.username)
}
}
module.exports = Bot
package.json
{
"name": "my-bot",
"version": "1.0.0",
"description": "My cool bot",
"main": "index.js",
"author": "You silly",
"license": "ISC",
"dependencies": {
"yuasharder": "^1.0.0"
}
}
Start with node .
parameter | Type | Default | Description |
---|---|---|---|
token |
string |
undefined |
Bot API Token |
file |
string |
undefined |
File path relative to root |
options |
ClusterManagerOptions |
null |
Options passed to manager |
options.totalShards |
number |
null |
Specifies amount of shards, checks gateway if null |
options.totalClusters |
number |
node:os.numCPUs() |
Amount fo clusters to spread shards across |
options.firstShardID |
number |
0 |
Shard that yuasharder should start on |
options.lastShardID |
number |
firstShardID - 1 |
Shard that yuasharder should end on |
options.clusterTimeout |
number |
5 |
Timeout between launching each cluster (seconds) |
options.statsInterval |
number |
60 |
Interval between each stats event emitted (seconds) |
options.guildsPerShard |
number |
1300 |
Amount of guilds per shard (totalShards must be null ) |
options.clientOptions |
Eris.ClientOptions |
{} |
Eris client options to pass to bot |
Manager
extends node:events.EventEmitter
Parameters | Type | Description |
---|
Launch yuasharder
Parameters | Type | Description |
---|---|---|
start |
number |
Cluster to start boradcasting from |
message |
IPCEvents |
Message to broadcast |
Broadcast message to all clusters
Parameters | Type | Description |
---|---|---|
cluster |
number |
Cluster to send message to |
message |
IPCEvents |
Message to broadcast |
Send message to specific cluster
Event | Callback | Emits on? |
---|---|---|
info |
string |
General info |
error |
any |
On error |
stats |
MasterStats |
Stats interval |
clusterInfo |
ClusterMessages |
General cluster info |
clusterWarn |
ClusterMessages |
Something semi dangerous |
clusterError |
ClusterMessages |
On cluster error |
shardConnect |
ShardMessages |
When shard connects |
shardDisconnect |
ShardMessages |
When shard disconnects |
shardReady |
ShardMessages |
When a shard becomes ready |
shardResume |
ShardMessages |
When shard resumes |
shardWarn |
ShardMessages |
Something semi dangerous occurs |
shardError |
ShardMessages |
When shard runs into error |
const { Manager } = require('yuasharder')
const manager = new Manager("Cool-Discord-Token", "/bot.js", {
totalShards: null, // Will Check Gateway For Recommended Shards
totalClusters: null, // Will Use Number Of Cpus
firstShardID: null, // Will Use Default
lastShardID: null, // Will Use Default
statsInterval: 30, // Every 30 seconds
guildsPerShard: 1000, // 1000 Guilds Per Shard
clientOptions: {
allowedMentions: {
everyone: false,
},
},
})
manager.on('stats', (stats) => {
console.log(stats)
})
manager.launch()
Base should not be constructed as it will not do anything, it should always be extended then exported
parameter | Type | Description |
---|---|---|
props |
BaseClassProps |
props given to contructor by yuasharder |
props.client |
Eris.Client |
Clusters eris client instance |
options.ipc |
IPCInterface |
Clusters IPC instance |
options.clusterID |
number |
Cluster instance is located on |
Eris client instance
IPC for cluster instance
Id of cluster current instance is on
Parameters | Type | Description |
---|
Used to start code
It looks like this because yuasharder will create an instance of your bot class extending Base then pass the props shown above to it. It will then run init to start your code
const { Base } = require('yuasharder')
class Bot extends Base {
constructor(props) {
super(props)
}
init() {
const bot = this.client
const ipc = this.ipc
const clusterID = this.clusterID
console.log("Logged in as", bot.user.username, "on cluster", clusterID)
}
}
module.exports = Bot
Event | Callback | Description |
---|---|---|
stats |
MasterStats |
All stats |
Parameters | Type | Description |
---|---|---|
event |
string |
Events Name |
callback |
(msg: IPCEvents) => void |
Callback function |
Register event listener for IPC event
Parameters | Type | Description |
---|---|---|
event |
string |
Events name |
Unregister event listener
Parameters | Type | Description |
---|---|---|
event |
string |
Events name |
message |
object |
Message to send with event |
Broadcast message to all clusters
Parameters | Type | Description |
---|---|---|
clusterID |
number |
Cluster to send to |
event |
string |
Events name |
message |
object |
Message to send with event |
Send message to specific cluster
const { Base } = require('yuasharder')
class Bot extends Base {
constructor(props) {
super(props)
}
init() {
this.ipc.register('stats', (stats) => {
console.log(stats)
})
}
}
module.exports = Bot
const { Base } = require('yuasharder')
class Bot extends Base {
constructor(props) {
super(props)
}
init() {
this.ipc.register('PING', (clusterID) => {
this.ipc.sendTo(clusterID, "PONG", {
clusterID: this.clusterID,
uptime: this.client.uptime
})
})
this.ipc.register('PONG', (msg) => {
console.log(msg.clusterID, "responded with", msg.uptime, "uptime")
})
this.ipc.broadcast("PING")
}
}
module.exports = Bot
Just some random extra stuff that may or may not be helpful
{
payload: "event",
msg: {
...
}
}
{
_eventName: "event",
msg: {
...
}
}
Please submit an issue in the issues section and I will try to get back asap
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make be sure to TEST all functionality before requesting