mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
types: make event emitters use mapped event types (#7019)
This commit is contained in:
70
typings/index.d.ts
vendored
70
typings/index.d.ts
vendored
@@ -609,6 +609,12 @@ export class ClientVoiceManager {
|
|||||||
|
|
||||||
export { Collection } from '@discordjs/collection';
|
export { Collection } from '@discordjs/collection';
|
||||||
|
|
||||||
|
export interface CollectorEventTypes<K, V, F extends unknown[] = []> {
|
||||||
|
collect: [V, ...F];
|
||||||
|
dispose: [V, ...F];
|
||||||
|
end: [collected: Collection<K, V>, reason: string];
|
||||||
|
}
|
||||||
|
|
||||||
export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmitter {
|
export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmitter {
|
||||||
protected constructor(client: Client, options?: CollectorOptions<[V, ...F]>);
|
protected constructor(client: Client, options?: CollectorOptions<[V, ...F]>);
|
||||||
private _timeout: NodeJS.Timeout | null;
|
private _timeout: NodeJS.Timeout | null;
|
||||||
@@ -633,11 +639,15 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
|
|||||||
public abstract collect(...args: unknown[]): K | null | Promise<K | null>;
|
public abstract collect(...args: unknown[]): K | null | Promise<K | null>;
|
||||||
public abstract dispose(...args: unknown[]): K | null;
|
public abstract dispose(...args: unknown[]): K | null;
|
||||||
|
|
||||||
public on(event: 'collect' | 'dispose', listener: (...args: [V, ...F]) => Awaitable<void>): this;
|
public on<EventKey extends keyof CollectorEventTypes<K, V, F>>(
|
||||||
public on(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
|
event: EventKey,
|
||||||
|
listener: (...args: CollectorEventTypes<K, V, F>[EventKey]) => Awaitable<void>,
|
||||||
|
): this;
|
||||||
|
|
||||||
public once(event: 'collect' | 'dispose', listener: (...args: [V, ...F]) => Awaitable<void>): this;
|
public once<EventKey extends keyof CollectorEventTypes<K, V, F>>(
|
||||||
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
|
event: EventKey,
|
||||||
|
listener: (...args: CollectorEventTypes<K, V, F>[EventKey]) => Awaitable<void>,
|
||||||
|
): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandInteractionOptionResolver<Cached extends CacheType = CacheType>
|
export interface ApplicationCommandInteractionOptionResolver<Cached extends CacheType = CacheType>
|
||||||
@@ -1829,6 +1839,16 @@ export class SelectMenuInteraction<Cached extends CacheType = CacheType> extends
|
|||||||
public inRawGuild(): this is SelectMenuInteraction<'raw'>;
|
public inRawGuild(): this is SelectMenuInteraction<'raw'>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ShardEventTypes {
|
||||||
|
spawn: [child: ChildProcess];
|
||||||
|
death: [child: ChildProcess];
|
||||||
|
disconnect: [];
|
||||||
|
ready: [];
|
||||||
|
reconnection: [];
|
||||||
|
error: [error: Error];
|
||||||
|
message: [message: any];
|
||||||
|
}
|
||||||
|
|
||||||
export class Shard extends EventEmitter {
|
export class Shard extends EventEmitter {
|
||||||
private constructor(manager: ShardingManager, id: number);
|
private constructor(manager: ShardingManager, id: number);
|
||||||
private _evals: Map<string, Promise<unknown>>;
|
private _evals: Map<string, Promise<unknown>>;
|
||||||
@@ -1853,17 +1873,15 @@ export class Shard extends EventEmitter {
|
|||||||
public send(message: unknown): Promise<Shard>;
|
public send(message: unknown): Promise<Shard>;
|
||||||
public spawn(timeout?: number): Promise<ChildProcess>;
|
public spawn(timeout?: number): Promise<ChildProcess>;
|
||||||
|
|
||||||
public on(event: 'spawn' | 'death', listener: (child: ChildProcess) => Awaitable<void>): this;
|
public on<K extends keyof ShardEventTypes>(
|
||||||
public on(event: 'disconnect' | 'ready' | 'reconnecting', listener: () => Awaitable<void>): this;
|
event: K,
|
||||||
public on(event: 'error', listener: (error: Error) => Awaitable<void>): this;
|
listener: (...args: ShardEventTypes[K]) => Awaitable<void>,
|
||||||
public on(event: 'message', listener: (message: any) => Awaitable<void>): this;
|
): this;
|
||||||
public on(event: string, listener: (...args: any[]) => Awaitable<void>): this;
|
|
||||||
|
|
||||||
public once(event: 'spawn' | 'death', listener: (child: ChildProcess) => Awaitable<void>): this;
|
public once<K extends keyof ShardEventTypes>(
|
||||||
public once(event: 'disconnect' | 'ready' | 'reconnecting', listener: () => Awaitable<void>): this;
|
event: K,
|
||||||
public once(event: 'error', listener: (error: Error) => Awaitable<void>): this;
|
listener: (...args: ShardEventTypes[K]) => Awaitable<void>,
|
||||||
public once(event: 'message', listener: (message: any) => Awaitable<void>): this;
|
): this;
|
||||||
public once(event: string, listener: (...args: any[]) => Awaitable<void>): this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ShardClientUtil {
|
export class ShardClientUtil {
|
||||||
@@ -2352,6 +2370,14 @@ export class WebSocketManager extends EventEmitter {
|
|||||||
private triggerClientReady(): void;
|
private triggerClientReady(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface WebSocketShardEvents {
|
||||||
|
ready: [];
|
||||||
|
resumed: [];
|
||||||
|
invalidSession: [];
|
||||||
|
close: [event: CloseEvent];
|
||||||
|
allReady: [unavailableGuilds?: Set<Snowflake>];
|
||||||
|
}
|
||||||
|
|
||||||
export class WebSocketShard extends EventEmitter {
|
export class WebSocketShard extends EventEmitter {
|
||||||
private constructor(manager: WebSocketManager, id: number);
|
private constructor(manager: WebSocketManager, id: number);
|
||||||
private sequence: number;
|
private sequence: number;
|
||||||
@@ -2394,15 +2420,15 @@ export class WebSocketShard extends EventEmitter {
|
|||||||
|
|
||||||
public send(data: unknown, important?: boolean): void;
|
public send(data: unknown, important?: boolean): void;
|
||||||
|
|
||||||
public on(event: 'ready' | 'resumed' | 'invalidSession', listener: () => Awaitable<void>): this;
|
public on<K extends keyof WebSocketShardEvents>(
|
||||||
public on(event: 'close', listener: (event: CloseEvent) => Awaitable<void>): this;
|
event: K,
|
||||||
public on(event: 'allReady', listener: (unavailableGuilds?: Set<Snowflake>) => Awaitable<void>): this;
|
listener: (...args: WebSocketShardEvents[K]) => Awaitable<void>,
|
||||||
public on(event: string, listener: (...args: any[]) => Awaitable<void>): this;
|
): this;
|
||||||
|
|
||||||
public once(event: 'ready' | 'resumed' | 'invalidSession', listener: () => Awaitable<void>): this;
|
public once<K extends keyof WebSocketShardEvents>(
|
||||||
public once(event: 'close', listener: (event: CloseEvent) => Awaitable<void>): this;
|
event: K,
|
||||||
public once(event: 'allReady', listener: (unavailableGuilds?: Set<Snowflake>) => Awaitable<void>): this;
|
listener: (...args: WebSocketShardEvents[K]) => Awaitable<void>,
|
||||||
public once(event: string, listener: (...args: any[]) => Awaitable<void>): this;
|
): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Widget extends Base {
|
export class Widget extends Base {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { ChildProcess } from 'child_process';
|
||||||
import type {
|
import type {
|
||||||
APIGuildMember,
|
APIGuildMember,
|
||||||
APIInteractionGuildMember,
|
APIInteractionGuildMember,
|
||||||
@@ -26,6 +27,7 @@ import {
|
|||||||
Client,
|
Client,
|
||||||
ClientApplication,
|
ClientApplication,
|
||||||
ClientUser,
|
ClientUser,
|
||||||
|
CloseEvent,
|
||||||
Collection,
|
Collection,
|
||||||
CommandInteraction,
|
CommandInteraction,
|
||||||
CommandInteractionOption,
|
CommandInteractionOption,
|
||||||
@@ -81,6 +83,8 @@ import {
|
|||||||
Shard,
|
Shard,
|
||||||
ApplicationCommandAutocompleteOption,
|
ApplicationCommandAutocompleteOption,
|
||||||
ApplicationCommandNumericOptionData,
|
ApplicationCommandNumericOptionData,
|
||||||
|
WebSocketShard,
|
||||||
|
Collector,
|
||||||
} from '.';
|
} from '.';
|
||||||
import type { ApplicationCommandOptionTypes } from './enums';
|
import type { ApplicationCommandOptionTypes } from './enums';
|
||||||
|
|
||||||
@@ -1124,4 +1128,31 @@ client.on('interactionCreate', async interaction => {
|
|||||||
|
|
||||||
declare const shard: Shard;
|
declare const shard: Shard;
|
||||||
|
|
||||||
|
shard.on('death', process => {
|
||||||
|
assertType<ChildProcess>(process);
|
||||||
|
});
|
||||||
|
|
||||||
|
declare const webSocketShard: WebSocketShard;
|
||||||
|
|
||||||
|
webSocketShard.on('close', event => {
|
||||||
|
assertType<CloseEvent>(event);
|
||||||
|
});
|
||||||
|
|
||||||
|
declare const collector: Collector<string, Interaction, string[]>;
|
||||||
|
|
||||||
|
collector.on('collect', (collected, ...other) => {
|
||||||
|
assertType<Interaction>(collected);
|
||||||
|
assertType<string[]>(other);
|
||||||
|
});
|
||||||
|
|
||||||
|
collector.on('dispose', (vals, ...other) => {
|
||||||
|
assertType<Interaction>(vals);
|
||||||
|
assertType<string[]>(other);
|
||||||
|
});
|
||||||
|
|
||||||
|
collector.on('end', (collection, reason) => {
|
||||||
|
assertType<Collection<string, Interaction>>(collection);
|
||||||
|
assertType<string>(reason);
|
||||||
|
});
|
||||||
|
|
||||||
assertType<Promise<number | null>>(shard.eval(c => c.readyTimestamp));
|
assertType<Promise<number | null>>(shard.eval(c => c.readyTimestamp));
|
||||||
|
|||||||
Reference in New Issue
Block a user