npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yuasharder

v1.1.2

Published

Sharder and Clusterer for Yua utilizing Discord Eris Library

Downloads

22

Readme

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)

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

Functions

Manager extends node:events.EventEmitter

launch() void

|Parameters|Type|Description| |:--- |:---|:--- |

Launch yuasharder

broadcast(start, message) void

|Parameters|Type|Description| |:--- |:---|:--- | |start|number|Cluster to start boradcasting from| |message|IPCEvents|Message to broadcast|

Broadcast message to all clusters

sendTo(cluster, message) void

|Parameters|Type|Description| |:--- |:---|:--- | |cluster|number|Cluster to send message to| |message|IPCEvents|Message to broadcast|

Send message to specific cluster

Events

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

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

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

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

|Parameters|Type|Description| |:--- |:---|:--- |

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

|Event|Callback |Description| |:--- | :--- |:--- | |stats|MasterStats|All stats|

Functions

register(event, callback) void

|Parameters|Type|Description| |:--- |:---|:--- | |event|string|Events Name| |callback|(msg: IPCEvents) => void|Callback function|

Register event listener for IPC event

unregister(event) void

|Parameters|Type|Description| |:--- |:---|:--- | |event|string|Events name|

Unregister event listener

broadcast(event, message) void

|Parameters|Type|Description| |:--- |:---|:--- | |event|string|Events name| |message|object|Message to send with event|

Broadcast message to all clusters

sendTo(clusterID, event, message) void

|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

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