Skip to content

Commit

Permalink
fix(Sharding): strict type context and return
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Jun 28, 2021
1 parent 03d3a5c commit 669b170
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
43 changes: 31 additions & 12 deletions typings/index.d.ts
Expand Up @@ -137,6 +137,7 @@ type Awaited<T> = T | Promise<T>;

declare module 'discord.js' {
import BaseCollection from '@discordjs/collection';
import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '@discordjs/voice';
import { ChildProcess } from 'child_process';
import {
APIActionRowComponent,
Expand All @@ -152,11 +153,11 @@ declare module 'discord.js' {
Snowflake as APISnowflake,
ApplicationCommandOptionType as ApplicationCommandOptionTypes,
ApplicationCommandPermissionType as ApplicationCommandPermissionTypes,
Snowflake as APISnowflake,
} from 'discord-api-types/v8';
import { EventEmitter } from 'events';
import { PathLike } from 'fs';
import { Readable, Stream, Writable } from 'stream';
import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '@discordjs/voice';
import { Stream } from 'stream';
import * as WebSocket from 'ws';

export const version: string;
Expand Down Expand Up @@ -1756,13 +1757,16 @@ declare module 'discord.js' {
public readonly ids: number[];
public mode: ShardingManagerMode;
public parentPort: any | null;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: MultipleShardRespawnOptions): Promise<void>;
Expand All @@ -1785,13 +1789,16 @@ declare module 'discord.js' {
public totalShards: number | 'auto';
public shardList: number[] | 'auto';
public broadcast(message: any): Promise<Shard[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => T,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public createShard(id: number): Shard;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
Expand Down Expand Up @@ -4321,5 +4328,17 @@ declare module 'discord.js' {
| 'STAGE_INSTANCE_UPDATE'
| 'STAGE_INSTANCE_DELETE';

type Serialized<T> = T extends symbol | bigint | (() => unknown)
? never
: T extends number | string | boolean | undefined
? T
: T extends { toJSON(): infer R }
? R
: T extends ReadonlyArray<infer V>
? Serialized<V>[]
: T extends ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
? {}
: { [K in keyof T]: Serialized<T[K]> };

//#endregion
}
40 changes: 39 additions & 1 deletion typings/index.ts
@@ -1,6 +1,15 @@
/// <reference path="index.d.ts" />

import { Client, Intents, Message, MessageAttachment, MessageEmbed } from 'discord.js';
import {
Client,
Collection,
Intents,
Message,
MessageAttachment,
MessageEmbed,
Permissions,
Serialized,
} from 'discord.js';

const client: Client = new Client({
intents: Intents.NON_PRIVILEGED,
Expand Down Expand Up @@ -48,3 +57,32 @@ client.on('message', ({ channel }) => {
});

client.login('absolutely-valid-token');

declare const assertType: <T>(value: T) => asserts value is T;
declare const serialize: <T>(value: T) => Serialized<T>;

assertType<undefined>(serialize(undefined));
assertType<null>(serialize(null));
assertType<number[]>(serialize([1, 2, 3]));
assertType<{}>(serialize(new Set([1, 2, 3])));
assertType<{}>(
serialize(
new Map([
[1, '2'],
[2, '4'],
]),
),
);
assertType<string>(serialize(new Permissions(Permissions.FLAGS.ATTACH_FILES)));
assertType<number>(serialize(new Intents(Intents.FLAGS.GUILDS)));
assertType<unknown>(
serialize(
new Collection([
[1, '2'],
[2, '4'],
]),
),
);
assertType<never>(serialize(Symbol('a')));
assertType<never>(serialize(() => {}));
assertType<never>(serialize(BigInt(42)));

0 comments on commit 669b170

Please sign in to comment.