Skip to content

Instantly share code, notes, and snippets.

@meister03
Last active December 27, 2022 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meister03/882ba6f6d805384f27336dd5ba389a54 to your computer and use it in GitHub Desktop.
Save meister03/882ba6f6d805384f27336dd5ba389a54 to your computer and use it in GitHub Desktop.
Hybrid Sharding - Typescript Rewrite - v2.0.0

The whole package has been rewritten to typescript to allow more type saftey and find some bugs, which will also lead to the rewrite of discord-cross-hosting in typescript. Naturally a major overhaul also requires some breaking changes, which have been kept small.

There could be some missing types, please report those in the Discord Server or create a issue in the repo.

Upgrade to latest:

npm uninstall discord-hybrid-sharding
npm install github:meister03/discord-hybrid-sharding

It is not recommended using the rewrite in production, without testing all functionalites

Changes:

Imports have changed, support for cjs as well for esm is available

// Manager
- import { Manager } from 'discord-hybrid-sharding'; //esm
- const { Manager } = require('discord-hybrid-sharding'); //cjs
+ import { ClusterManager } from 'discord-hybrid-sharding'; //esm
+ const { ClusterManager } = require('discord-hybrid-sharding'); //cjs

// Cluster Client
- import { Client, data } from 'discord-hybrid-sharding';
- const { Client, data } = require('discord-hybrid-sharding');
+ import { ClusterClient, getInfo } from 'discord-hybrid-sharding';
+ const { ClusterClient, getInfo } = require('discord-hybrid-sharding');

Retreiving Cluster Data

- Cluster.Client.getInfo().TOTAL_SHARDS
- Cluster.data.TOTAL_SHARDS
+ getInfo().TOTAL_SHARDS

IPC Message System, returnType on message, clientRequest event

- _sCustom, _sReply, _sRequest has been removed on BaseMessage
+ _type === messageType.CUSTOM_REQUEST | messageType.CUSTOM_REPLY

Cluster Death Event additional parameter

- cluster.on('death', Child | Worker)
+ cluster.on('death', Cluster ,Child | Worker)

Cluster Spawn Event additional parameter

- cluster.on('spawn', Child | Worker)
+ cluster.on('spawn', Cluster ,Child | Worker)

Remove evalOnCluster in favor of broadcastEval , as they do the same thing

- evalOnCluster
+ broadcastEval('string'|fn, {cluster})
@Tomato6966
Copy link

instead of doing

- if(msg._sRequest) {}
+ if(msg._type === messageType.CUSTOM_REQUEST) {}

Ways of replacing evalOnCluster

// by having the clusterId already.
client.cluster.broadcastEval(c => {
   return c.guilds.cache.size;
}, { cluster: clusterId })
// by without any options
const clusterId = await client.cluster.broadcastEval((c, context) => {
   if(!c.guilds.cache.has(context.guildId)) return "invalidCluster";
   else return c.cluster.id;
}, { context: { guildId: "id" } }).then(x => x.filter(res => res !== "invalidCluster"));
// in addition this exact example returns the clsuterId of the provided Guild Id

@Tomato6966
Copy link

instead of returning a specific string inside of the Broadcasteval, you could return "null", too and then just do
res.filter(Boolean)

This covers most usecases well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment