mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(Client): add conditional ready typings (#6073)
This commit is contained in:
@@ -18,7 +18,7 @@ const VoiceRegion = require('../structures/VoiceRegion');
|
||||
const Webhook = require('../structures/Webhook');
|
||||
const Widget = require('../structures/Widget');
|
||||
const Collection = require('../util/Collection');
|
||||
const { Events, InviteScopes } = require('../util/Constants');
|
||||
const { Events, InviteScopes, Status } = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const Intents = require('../util/Intents');
|
||||
const Options = require('../util/Options');
|
||||
@@ -227,6 +227,15 @@ class Client extends BaseClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the client has logged in, indicative of being able to access
|
||||
* properties such as `user` and `application`.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isReady() {
|
||||
return this.ws.status === Status.READY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs out, terminates the connection to Discord, and destroys the client.
|
||||
* @returns {void}
|
||||
|
||||
@@ -373,8 +373,9 @@ class WebSocketManager extends EventEmitter {
|
||||
/**
|
||||
* Emitted when the client becomes ready to start working.
|
||||
* @event Client#ready
|
||||
* @param {Client} client The client
|
||||
*/
|
||||
this.client.emit(Events.CLIENT_READY);
|
||||
this.client.emit(Events.CLIENT_READY, this.client);
|
||||
|
||||
this.handlePacket();
|
||||
}
|
||||
|
||||
19
typings/index.d.ts
vendored
19
typings/index.d.ts
vendored
@@ -278,23 +278,25 @@ export class Channel extends Base {
|
||||
public toString(): ChannelMention;
|
||||
}
|
||||
|
||||
export class Client extends BaseClient {
|
||||
type If<T extends boolean, A, B = null> = T extends true ? A : T extends false ? B : A | B;
|
||||
|
||||
export class Client<Ready extends boolean = boolean> extends BaseClient {
|
||||
public constructor(options: ClientOptions);
|
||||
private actions: unknown;
|
||||
private _eval(script: string): unknown;
|
||||
private _validateOptions(options: ClientOptions): void;
|
||||
|
||||
public application: ClientApplication | null;
|
||||
public application: If<Ready, ClientApplication>;
|
||||
public channels: ChannelManager;
|
||||
public readonly emojis: BaseGuildEmojiManager;
|
||||
public guilds: GuildManager;
|
||||
public options: ClientOptions;
|
||||
public readyAt: Date | null;
|
||||
public readonly readyTimestamp: number | null;
|
||||
public readyAt: If<Ready, Date>;
|
||||
public readonly readyTimestamp: If<Ready, number>;
|
||||
public shard: ShardClientUtil | null;
|
||||
public token: string | null;
|
||||
public readonly uptime: number | null;
|
||||
public user: ClientUser | null;
|
||||
public token: If<Ready, string, string | null>;
|
||||
public uptime: If<Ready, number>;
|
||||
public user: If<Ready, ClientUser>;
|
||||
public users: UserManager;
|
||||
public voice: ClientVoiceManager;
|
||||
public ws: WebSocketManager;
|
||||
@@ -307,6 +309,7 @@ export class Client extends BaseClient {
|
||||
public fetchWidget(guild: GuildResolvable): Promise<Widget>;
|
||||
public generateInvite(options?: InviteGenerationOptions): string;
|
||||
public login(token?: string): Promise<string>;
|
||||
public isReady(): this is Client<true>;
|
||||
public sweepMessages(lifetime?: number): number;
|
||||
public toJSON(): unknown;
|
||||
|
||||
@@ -2818,7 +2821,7 @@ export interface ClientEvents {
|
||||
presenceUpdate: [oldPresence: Presence | null, newPresence: Presence];
|
||||
rateLimit: [rateLimitData: RateLimitData];
|
||||
invalidRequestWarning: [invalidRequestWarningData: InvalidRequestWarningData];
|
||||
ready: [];
|
||||
ready: [client: Client<true>];
|
||||
invalidated: [];
|
||||
roleCreate: [role: Role];
|
||||
roleDelete: [role: Role];
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
ApplicationCommandResolvable,
|
||||
CategoryChannel,
|
||||
Client,
|
||||
ClientApplication,
|
||||
ClientUser,
|
||||
Collection,
|
||||
Constants,
|
||||
DMChannel,
|
||||
@@ -447,6 +449,27 @@ client.on('interaction', async interaction => {
|
||||
|
||||
client.login('absolutely-valid-token');
|
||||
|
||||
// Test client conditional types
|
||||
client.on('ready', client => {
|
||||
assertType<Client<true>>(client);
|
||||
});
|
||||
|
||||
declare const loggedInClient: Client<true>;
|
||||
assertType<ClientApplication>(loggedInClient.application);
|
||||
assertType<Date>(loggedInClient.readyAt);
|
||||
assertType<number>(loggedInClient.readyTimestamp);
|
||||
assertType<string>(loggedInClient.token);
|
||||
assertType<number>(loggedInClient.uptime);
|
||||
assertType<ClientUser>(loggedInClient.user);
|
||||
|
||||
declare const loggedOutClient: Client<false>;
|
||||
assertType<null>(loggedOutClient.application);
|
||||
assertType<null>(loggedOutClient.readyAt);
|
||||
assertType<null>(loggedOutClient.readyTimestamp);
|
||||
assertType<string | null>(loggedOutClient.token);
|
||||
assertType<null>(loggedOutClient.uptime);
|
||||
assertType<null>(loggedOutClient.user);
|
||||
|
||||
// Test type transformation:
|
||||
declare const assertType: <T>(value: T) => asserts value is T;
|
||||
declare const serialize: <T>(value: T) => Serialized<T>;
|
||||
|
||||
Reference in New Issue
Block a user