1.1.2 • Published 3 years ago

yuasharder v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

YuaSharder

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.

Installation

Node Version: ᵗᵇʰ ᶦᵈᵏ ˡᵐᵃᵒ

npm install yuasharder

Simple Usage

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 .

More Info/Examples

yuasharder.Manager(token, file, options)

parameterTypeDefaultDescription
tokenstringundefinedBot API Token
filestringundefinedFile path relative to root
optionsClusterManagerOptionsnullOptions passed to manager
options.totalShardsnumbernullSpecifies amount of shards, checks gateway if null
options.totalClustersnumbernode:os.numCPUs()Amount fo clusters to spread shards across
options.firstShardIDnumber0Shard that yuasharder should start on
options.lastShardIDnumberfirstShardID - 1Shard that yuasharder should end on
options.clusterTimeoutnumber5Timeout between launching each cluster (seconds)
options.statsIntervalnumber60Interval between each stats event emitted (seconds)
options.guildsPerShardnumber1300Amount of guilds per shard (totalShards must be null)
options.clientOptionsEris.ClientOptions{}Eris client options to pass to bot

Functions

Manager extends node:events.EventEmitter

launch() void

ParametersTypeDescription

Launch yuasharder

broadcast(start, message) void

ParametersTypeDescription
startnumberCluster to start boradcasting from
messageIPCEventsMessage to broadcast

Broadcast message to all clusters

sendTo(cluster, message) void

ParametersTypeDescription
clusternumberCluster to send message to
messageIPCEventsMessage to broadcast

Send message to specific cluster

Events

EventCallbackEmits on?
infostringGeneral info
erroranyOn error
statsMasterStatsStats interval
clusterInfoClusterMessagesGeneral cluster info
clusterWarnClusterMessagesSomething semi dangerous
clusterErrorClusterMessagesOn cluster error
shardConnectShardMessagesWhen shard connects
shardDisconnectShardMessagesWhen shard disconnects
shardReadyShardMessagesWhen a shard becomes ready
shardResumeShardMessagesWhen shard resumes
shardWarnShardMessagesSomething semi dangerous occurs
shardErrorShardMessagesWhen shard runs into error

Example Usage

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()

yuasharder.Base(props)

Base should not be constructed as it will not do anything, it should always be extended then exported

parameterTypeDescription
propsBaseClassPropsprops given to contructor by yuasharder
props.clientEris.ClientClusters eris client instance
options.ipcIPCInterfaceClusters IPC instance
options.clusterIDnumberCluster instance is located on

Props

.client Eris.Client

Eris client instance

.ipc IPCInterface

IPC for cluster instance

.clusterID number

Id of cluster current instance is on

Functions

init() void

ParametersTypeDescription

Used to start code

Example Usage

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

IPC

Default Events

EventCallbackDescription
statsMasterStatsAll stats

Functions

register(event, callback) void

ParametersTypeDescription
eventstringEvents Name
callback(msg: IPCEvents) => voidCallback function

Register event listener for IPC event

unregister(event) void

ParametersTypeDescription
eventstringEvents name

Unregister event listener

broadcast(event, message) void

ParametersTypeDescription
eventstringEvents name
messageobjectMessage to send with event

Broadcast message to all clusters

sendTo(clusterID, event, message) void

ParametersTypeDescription
clusterIDnumberCluster to send to
eventstringEvents name
messageobjectMessage to send with event

Send message to specific cluster

Examples

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

Random Extra

Just some random extra stuff that may or may not be helpful

Process Event Object

{
  payload: "event",
  msg: {
    ...
  }
}

IPC Event Object

{
  _eventName: "event",
  msg: {
    ...
  }
}

Issues / Feature Request

Please submit an issue in the issues section and I will try to get back asap

Contributing

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

License

AGPL-3.0

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago