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 5 commits
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
174 changes: 174 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
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 ClusterMap {
workerID: number;
firstShardID: number;
lastShardID: number;
}

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: "connect" | "reconnect", clientOptions: Eris.ClientOptions): void;
loadCode(bot: C): void;
startsStats(bot: C): void;
}

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

declare class ClusterManager extends EventEmitter {
shardCount: number | "auto";
firstShardID: number;
lastShardID: number;
clusterCount: number;
clusterTimeout: number;
token: string | false;
clusters: Map<number, ClusterMap>;
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;
}
}
30 changes: 21 additions & 9 deletions src/sharding/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const { inspect } = require('util');
const IPC = require("../structures/IPC.js");

/**
*
*
* @class Cluster
*/
class Cluster {
Expand Down Expand Up @@ -53,11 +51,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 +173,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 +263,10 @@ class Cluster {
}
}

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

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