Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDoc tidy up and TS docs #69

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import Eris from "eris";
import EventEmitter from "events"

type Callback = (...args: any[]) => void;

interface ShardStats {
id: number;
ready: boolean;
latency: number;
status: "disconnected" | "connecting" | "handshaking" | "ready" | "resuming";
}

interface ClusterStatsInfo {
guilds: number;
users: number;
totalRam: number;
voice: number;
exclusiveGuilds: number;
largeGuilds: number;
clusters: ClusterDataStats[];
}

interface ClusterDataStats extends Exclude<ClusterStatsInfo, "totalRam" | "clusters"> {
cluster: number;
shards: number;
ram: number;
uptime: number;
shardStats: ShardStats;
}

interface ClusterStats {
stats: ClusterStatsInfo;
clustersCounted: number;
}

interface ClusterData {
item: number;
value: {
id: number;
clusterCount: number;
name: string;
firstShardID: number;
lastShardID: number;
maxShards: number;
token: string;
file: string;
clientOptions: Eris.ClientOptions;
}
}

interface Webhook {
id: string;
token: string;
}

interface Webhooks {
shard?: Webhook;
cluster?: Webhook;
}

interface ClusterManagerOptions {
shards?: number;
firstShardID?: number;
lastShardID?: number;
clusters?: number;
clusterTimeout?: number;
stats?: boolean;
debug?: boolean;
statsInterval?: number;
name?: string;
guildsPerShard?: number;
webhooks?: Webhooks;
clientOptions: Eris.ClientOptions;
}

declare class IPC extends EventEmitter {
events: Map<string, { fn: Callback }>;
register(event: string, callback: Callback): void;
unregister(name: string): void;
broadcast(name: string, message?: any): void;
sendTo(cluster: number, name: string, message?: any): void;
fetchUser(id: string): Promise<Eris.User>;
fetchGuild(id: string): Promise<Eris.Guild>;
fetchChannel(id: string): Promise<Eris.AnyChannel>;
fetchMember(guildID: string, memberID: string): Promise<Eris.Member>;
}

declare class Cluster<T extends Base, C extends Eris.Client> {
shards: number;
maxShards: number;
firstShardID: number;
lastShardID: number;
mainFile: string | null;
clusterID: number;
clusterCount: number;
guilds: number;
users: number;
uptime: number;
exclusiveGuilds: number;
largeGuilds: number;
voiceChannels: number;
shardsStats: ShardStats[];
app: T | null;
bot: C;
test: boolean;
ipc: IPC;
logOverride(message: unknown): string;
spawn(): void;
connect(firstShardID: number, lastShardID: number, maxShards: number, token: string, type: unknown, clientOptions: Eris.ClientOptions): void;
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
loadCode(bot: C): void;
startsStats(bot: C): void;
}

class Queue extends EventEmitter {
queue: ClusterData[];
executeQueue(): void;
queueItem(item: ClusterData): void;
}

declare class ClusterManager<T extends Base, C extends Eris.Client> extends EventEmitter {
shardCount: number | "auto";
firstShardID: number;
lastShardID: number;
clusterCount: number;
clusterTimeout: number;
token: string | false;
clusters: Map<number, Cluster<T, C>>;
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
workers: Map<number, number>;
queue: Queue;
callbacks: Map<string, string>;
options: { stats: boolean; debug: boolean };
statsInterval: number;
mainFile: string;
name: string;
guildsPerShard: number;
webhooks: Webhooks;
clientOptions: Eris.ClientOptions;
status: ClusterStats;
eris: Eris.Client;
constructor(token: string, mainFile: string, options: ClusterManagerOptions);
isMaster(): boolean;
startStats(): void;
executeStats(clusters: [string, Worker][], start: number): void;
start(clusterID: number): void;
launch(): void;
chunk(shards: number[], clusterCount: number): number[][];
connectShards(): void;
sendWebhook(type: "shard" | "cluster", embed: Eris.EmbedOptions): void;
printLogo(): void;
restartCluster(worker: Worker, code: number): void;
calculateShards(): Promise<number>;
fetchInfo(start: number, type: string, value: any): void;
broadcast(start: number, message: any): void;
sendTo(cluster: number, message: any): void;
}

declare module "eris-sharder" {
export const Master: typeof ClusterManager;

class Base {
bot: Eris.Client;
clusterID: number;
ipc: IPC;
constructor(setup: { bot: Eris.Client; clusterID: number; ipc: IPC });
restartCluster(clusterID: number): void;
abstract launch(): any;
}
}
31 changes: 22 additions & 9 deletions src/sharding/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const Base = require("../structures/Base.js");
const SyncedRequestHandler = require('../structures/SyncedRequestHandler.js');
const { inspect } = require('util');
const IPC = require("../structures/IPC.js");
const Eris = require("eris");

/**
*
*
* @class Cluster
*/
class Cluster {
Expand Down Expand Up @@ -53,11 +52,18 @@ class Cluster {

}

/**
* @returns {String}
* @memberof Cluster
*/
logOverride(message) {
if (typeof message == 'object') return inspect(message);
else return message;
}

/**
* @memberof Cluster
*/
spawn() {
process.on('uncaughtException', (err) => {
process.send({ name: "error", msg: err.stack });
Expand Down Expand Up @@ -168,13 +174,12 @@ class Cluster {
}

/**
*
*
* @param {any} firstShardID
* @param {any} lastShardID
* @param {any} maxShards
* @param {any} token
* @param {any} type
* @param {Number} firstShardID
* @param {Number} lastShardID
* @param {Number} maxShards
* @param {String} token
* @param {unknown} type
* @param {Eris.ClientOptions} clientOptions
* @memberof Cluster
*/
connect(firstShardID, lastShardID, maxShards, token, type, clientOptions) {
Expand Down Expand Up @@ -259,6 +264,10 @@ class Cluster {
}
}

/**
* @param {Eris.Client} bot
* @memberof Cluster
*/
loadCode(bot) {
let rootPath = process.cwd();
rootPath = rootPath.replace(`\\`, "/");
Expand All @@ -275,6 +284,10 @@ class Cluster {
}
}

/**
* @param {Eris.Client} bot
* @memberof Cluster
*/
startStats(bot) {
setInterval(() => {
this.guilds = bot.guilds.size;
Expand Down
Loading