Skip to content

Commit

Permalink
types(Events): rest events can be emitted on BaseClient (#6936)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Nov 5, 2021
1 parent dc64541 commit c297829
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/client/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ class BaseClient extends EventEmitter {
}

module.exports = BaseClient;

/**
* Emitted for general debugging information.
* @event BaseClient#debug
* @param {string} info The debug information
*/
6 changes: 0 additions & 6 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,6 @@ module.exports = Client;
* @param {string} info The warning
*/

/**
* Emitted for general debugging information.
* @event Client#debug
* @param {string} info The debug information
*/

/**
* @external Collection
* @see {@link https://discord.js.org/#/docs/collection/main/class/Collection}
Expand Down
8 changes: 4 additions & 4 deletions src/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class RequestHandler {
if (this.manager.client.listenerCount(RATE_LIMIT)) {
/**
* Emitted when the client hits a rate limit while making a request
* @event Client#rateLimit
* @event BaseClient#rateLimit
* @param {RateLimitData} rateLimitData Object containing the rate limit info
*/
this.manager.client.emit(RATE_LIMIT, {
Expand Down Expand Up @@ -178,7 +178,7 @@ class RequestHandler {
* This event can emit several times for the same request, e.g. when hitting a rate limit.
* <info>This is an informational event that is emitted quite frequently,
* it is highly recommended to check `request.path` to filter the data.</info>
* @event Client#apiRequest
* @event BaseClient#apiRequest
* @param {APIRequest} request The request that is about to be sent
*/
this.manager.client.emit(API_REQUEST, {
Expand Down Expand Up @@ -210,7 +210,7 @@ class RequestHandler {
* This event does not necessarily correlate to completion of the request, e.g. when hitting a rate limit.
* <info>This is an informational event that is emitted quite frequently,
* it is highly recommended to check `request.path` to filter the data.</info>
* @event Client#apiResponse
* @event BaseClient#apiResponse
* @param {APIRequest} request The request that triggered this response
* @param {Response} response The response received from the Discord API
*/
Expand Down Expand Up @@ -285,7 +285,7 @@ class RequestHandler {
/**
* Emitted periodically when the process sends invalid requests to let users avoid the
* 10k invalid requests in 10 minutes threshold that causes a ban
* @event Client#invalidRequestWarning
* @event BaseClient#invalidRequestWarning
* @param {InvalidRequestWarningData} invalidRequestWarningData Object containing the invalid request info
*/
this.manager.client.emit(INVALID_REQUEST_WARNING, {
Expand Down
44 changes: 40 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,39 @@ export class BaseClient extends EventEmitter {
private decrementMaxListeners(): void;
private incrementMaxListeners(): void;

public on<K extends keyof BaseClientEvents>(
event: K,
listener: (...args: BaseClientEvents[K]) => Awaitable<void>,
): this;
public on<S extends string | symbol>(
event: Exclude<S, keyof BaseClientEvents>,
listener: (...args: any[]) => Awaitable<void>,
): this;

public once<K extends keyof BaseClientEvents>(
event: K,
listener: (...args: BaseClientEvents[K]) => Awaitable<void>,
): this;
public once<S extends string | symbol>(
event: Exclude<S, keyof BaseClientEvents>,
listener: (...args: any[]) => Awaitable<void>,
): this;

public emit<K extends keyof BaseClientEvents>(event: K, ...args: BaseClientEvents[K]): boolean;
public emit<S extends string | symbol>(event: Exclude<S, keyof BaseClientEvents>, ...args: unknown[]): boolean;

public off<K extends keyof BaseClientEvents>(
event: K,
listener: (...args: BaseClientEvents[K]) => Awaitable<void>,
): this;
public off<S extends string | symbol>(
event: Exclude<S, keyof BaseClientEvents>,
listener: (...args: any[]) => Awaitable<void>,
): this;

public removeAllListeners<K extends keyof BaseClientEvents>(event?: K): this;
public removeAllListeners<S extends string | symbol>(event?: Exclude<S, keyof BaseClientEvents>): this;

public options: ClientOptions | WebhookClientOptions;
public destroy(): void;
public toJSON(...props: Record<string, boolean | string>[]): unknown;
Expand Down Expand Up @@ -3553,9 +3586,15 @@ export interface ChannelWebhookCreateOptions {
reason?: string;
}

export interface ClientEvents {
export interface BaseClientEvents {
apiResponse: [request: APIRequest, response: Response];
apiRequest: [request: APIRequest];
debug: [message: string];
rateLimit: [rateLimitData: RateLimitData];
invalidRequestWarning: [invalidRequestWarningData: InvalidRequestWarningData];
}

export interface ClientEvents extends BaseClientEvents {
/** @deprecated See [this issue](https://github.com/discord/discord-api-docs/issues/3690) for more information. */
applicationCommandCreate: [command: ApplicationCommand];
/** @deprecated See [this issue](https://github.com/discord/discord-api-docs/issues/3690) for more information. */
Expand All @@ -3566,7 +3605,6 @@ export interface ClientEvents {
channelDelete: [channel: DMChannel | GuildChannel];
channelPinsUpdate: [channel: TextBasedChannels, date: Date];
channelUpdate: [oldChannel: DMChannel | GuildChannel, newChannel: DMChannel | GuildChannel];
debug: [message: string];
warn: [message: string];
emojiCreate: [emoji: GuildEmoji];
emojiDelete: [emoji: GuildEmoji];
Expand Down Expand Up @@ -3604,8 +3642,6 @@ export interface ClientEvents {
messageReactionRemove: [reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser];
messageUpdate: [oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage];
presenceUpdate: [oldPresence: Presence | null, newPresence: Presence];
rateLimit: [rateLimitData: RateLimitData];
invalidRequestWarning: [invalidRequestWarningData: InvalidRequestWarningData];
ready: [client: Client<true>];
invalidated: [];
roleCreate: [role: Role];
Expand Down

0 comments on commit c297829

Please sign in to comment.