From 69d69f25b964b6a2bb13bd086696abec17782ca3 Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Sun, 15 Mar 2020 02:50:43 +1100 Subject: [PATCH] =?UTF-8?q?feat(Types):=20support=20type-guarding=20using?= =?UTF-8?q?=20Channel#type=20string=20li=E2=80=A6=20(#3918)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(types): union type for Channels * feat(Types): union for TextBasedChannelTypes * fix(Types): use new union types in return values * Update typings/index.d.ts Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * Update typings/index.d.ts Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * fix(Types): various minor improvements for Partials * fix(GuildChannel): partial should return false Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: Crawl --- src/structures/GuildChannel.js | 9 +++++ typings/index.d.ts | 72 +++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 2e80eca71222..2471f9e34094 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -576,6 +576,15 @@ class GuildChannel extends Channel { return this.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false); } + /** + * Whether this GuildChannel is a partial + * @type {boolean} + * @readonly + */ + get partial() { + return false; + } + /** * Whether the channel is viewable by the client user * @type {boolean} diff --git a/typings/index.d.ts b/typings/index.d.ts index 863e05ff0293..6d5285473090 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -132,7 +132,7 @@ declare module 'discord.js' { } export class CategoryChannel extends GuildChannel { - public readonly children: Collection; + public readonly children: Collection>; public type: 'category'; } @@ -143,8 +143,8 @@ declare module 'discord.js' { public deleted: boolean; public id: Snowflake; public type: keyof typeof ChannelType; - public delete(reason?: string): Promise; - public fetch(): Promise; + public delete(reason?: string): Promise; + public fetch(): Promise; public toString(): string; } @@ -176,11 +176,17 @@ declare module 'discord.js' { public sweepMessages(lifetime?: number): number; public toJSON(): object; - public on(event: 'channelCreate' | 'channelDelete', listener: (channel: Channel | PartialChannel) => void): this; - public on(event: 'channelPinsUpdate', listener: (channel: Channel | PartialChannel, time: Date) => void): this; + public on( + event: 'channelCreate' | 'channelDelete', + listener: (channel: ChannelTypes | PartialChannel) => void, + ): this; + public on( + event: 'channelPinsUpdate', + listener: (channel: TextBasedChannelTypes | PartialChannel, time: Date) => void, + ): this; public on( event: 'channelUpdate', - listener: (oldChannel: Channel | PartialChannel, newChannel: Channel | PartialChannel) => void, + listener: (oldChannel: ChannelTypes | PartialChannel, newChannel: ChannelTypes | PartialChannel) => void, ): this; public on(event: 'debug' | 'warn', listener: (info: string) => void): this; public on(event: 'disconnect', listener: (event: any, shardID: number) => void): this; @@ -240,7 +246,7 @@ declare module 'discord.js' { public on(event: 'roleUpdate', listener: (oldRole: Role, newRole: Role) => void): this; public on( event: 'typingStart', - listener: (channel: Channel | PartialChannel, user: User | PartialUser) => void, + listener: (channel: TextBasedChannelTypes | PartialChannel, user: User | PartialUser) => void, ): this; public on(event: 'userUpdate', listener: (oldUser: User | PartialUser, newUser: User | PartialUser) => void): this; public on(event: 'voiceStateUpdate', listener: (oldState: VoiceState, newState: VoiceState) => void): this; @@ -251,11 +257,17 @@ declare module 'discord.js' { public on(event: 'shardResume', listener: (id: number, replayed: number) => void): this; public on(event: string, listener: (...args: any[]) => void): this; - public once(event: 'channelCreate' | 'channelDelete', listener: (channel: Channel | PartialChannel) => void): this; - public once(event: 'channelPinsUpdate', listener: (channel: Channel | PartialChannel, time: Date) => void): this; + public once( + event: 'channelCreate' | 'channelDelete', + listener: (channel: ChannelTypes | PartialChannel) => void, + ): this; + public once( + event: 'channelPinsUpdate', + listener: (channel: TextBasedChannelTypes | PartialChannel, time: Date) => void, + ): this; public once( event: 'channelUpdate', - listener: (oldChannel: Channel | PartialChannel, newChannel: Channel | PartialChannel) => void, + listener: (oldChannel: ChannelTypes | PartialChannel, newChannel: ChannelTypes | PartialChannel) => void, ): this; public once(event: 'debug' | 'warn', listener: (info: string) => void): this; public once(event: 'disconnect', listener: (event: any, shardID: number) => void): this; @@ -313,7 +325,7 @@ declare module 'discord.js' { public once(event: 'roleUpdate', listener: (oldRole: Role, newRole: Role) => void): this; public once( event: 'typingStart', - listener: (channel: Channel | PartialChannel, user: User | PartialUser) => void, + listener: (channel: TextBasedChannelTypes | PartialChannel, user: User | PartialUser) => void, ): this; public once( event: 'userUpdate', @@ -710,7 +722,7 @@ declare module 'discord.js' { export class Guild extends Base { constructor(client: Client, data: object); private _sortedRoles(): Collection; - private _sortedChannels(channel: Channel): Collection; + private _sortedChannels(channel: Channel): Collection; private _memberSpeakUpdate(user: Snowflake, speaking: boolean): void; public readonly afkChannel: VoiceChannel | null; @@ -725,7 +737,7 @@ declare module 'discord.js' { public defaultMessageNotifications: DefaultMessageNotifications | number; public deleted: boolean; public description: string | null; - public embedChannel: GuildChannel | null; + public embedChannel: GuildChannelTypes | null; public embedChannelID: Snowflake | null; public embedEnabled: boolean; public emojis: GuildEmojiManager; @@ -857,6 +869,7 @@ declare module 'discord.js' { public readonly members: Collection; public name: string; public readonly parent: CategoryChannel | null; + public readonly partial: false; public parentID: Snowflake | null; public permissionOverwrites: Collection; public readonly permissionsLocked: boolean | null; @@ -882,7 +895,7 @@ declare module 'discord.js' { public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly | null; public setName(name: string, reason?: string): Promise; public setParent( - channel: GuildChannel | Snowflake, + channel: CategoryChannel | Snowflake, options?: { lockPermissions?: boolean; reason?: string }, ): Promise; public setPosition(position: number, options?: { relative?: boolean; reason?: string }): Promise; @@ -989,7 +1002,7 @@ declare module 'discord.js' { export class Invite extends Base { constructor(client: Client, data: object); - public channel: GuildChannel | PartialGroupDMChannel; + public channel: GuildChannelTypes | PartialGroupDMChannel; public code: string; public readonly deletable: boolean; public readonly createdAt: Date | null; @@ -1106,11 +1119,11 @@ declare module 'discord.js' { } export class MessageCollector extends Collector { - constructor(channel: TextChannel | DMChannel, filter: CollectorFilter, options?: MessageCollectorOptions); - private _handleChannelDeletion(channel: GuildChannel): void; + constructor(channel: TextBasedChannelTypes, filter: CollectorFilter, options?: MessageCollectorOptions); + private _handleChannelDeletion(channel: TextBasedChannelTypes): void; private _handleGuildDeletion(guild: Guild): void; - public channel: Channel; + public channel: TextBasedChannelTypes; public options: MessageCollectorOptions; public received: number; @@ -1173,7 +1186,7 @@ declare module 'discord.js' { roles: Snowflake[] | Collection, everyone: boolean, ); - private _channels: Collection | null; + private _channels: Collection | null; private readonly _content: Message; private _members: Collection | null; @@ -1182,7 +1195,7 @@ declare module 'discord.js' { public everyone: boolean; public readonly guild: Guild; public has( - data: User | GuildMember | Role | GuildChannel, + data: User | GuildMember | Role | GuildChannelTypes, options?: { ignoreDirect?: boolean; ignoreRoles?: boolean; @@ -1238,9 +1251,9 @@ declare module 'discord.js' { } export class PermissionOverwrites { - constructor(guildChannel: GuildChannel, data?: object); + constructor(guildChannel: GuildChannelTypes, data?: object); public allow: Readonly; - public readonly channel: GuildChannel; + public readonly channel: GuildChannelTypes; public deny: Readonly; public id: Snowflake; public type: OverwriteType; @@ -1281,7 +1294,7 @@ declare module 'discord.js' { export class ReactionCollector extends Collector { constructor(message: Message, filter: CollectorFilter, options?: ReactionCollectorOptions); - private _handleChannelDeletion(channel: GuildChannel): void; + private _handleChannelDeletion(channel: TextBasedChannelTypes): void; private _handleGuildDeletion(guild: Guild): void; private _handleMessageDeletion(message: Message): void; @@ -1471,6 +1484,7 @@ declare module 'discord.js' { export class StoreChannel extends GuildChannel { constructor(guild: Guild, data?: object); public nsfw: boolean; + public type: 'store'; } class StreamDispatcher extends VolumeMixin(Writable) { @@ -1918,9 +1932,9 @@ declare module 'discord.js' { //#region Managers - export class ChannelManager extends BaseManager { + export class ChannelManager extends BaseManager { constructor(client: Client, iterable: Iterable); - public fetch(id: Snowflake, cache?: boolean): Promise; + public fetch(id: Snowflake, cache?: boolean): Promise; } export abstract class BaseManager { @@ -1935,7 +1949,7 @@ declare module 'discord.js' { public resolveID(resolvable: R): K | null; } - export class GuildChannelManager extends BaseManager { + export class GuildChannelManager extends BaseManager { constructor(guild: Guild, iterable?: Iterable); public guild: Guild; public create(name: string, options: GuildCreateChannelOptions & { type: 'voice' }): Promise; @@ -2276,6 +2290,10 @@ declare module 'discord.js' { position: number; } + type ChannelTypes = DMChannel | CategoryChannel | NewsChannel | StoreChannel | TextChannel | VoiceChannel; + type GuildChannelTypes = CategoryChannel | NewsChannel | StoreChannel | TextChannel | VoiceChannel; + type TextBasedChannelTypes = DMChannel | NewsChannel | TextChannel; + type ChannelResolvable = Channel | Snowflake; interface ClientApplicationAsset { @@ -2864,7 +2882,7 @@ declare module 'discord.js' { [K in keyof Omit]: T[K] | null; }; - interface PartialChannel extends Partialize {} + interface PartialChannel extends Partialize {} interface PartialChannelData { id?: number;