Skip to content

Commit

Permalink
feat(Types): support type-guarding using Channel#type string li… (#3918)
Browse files Browse the repository at this point in the history
* 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 <icrawltogo@gmail.com>
  • Loading branch information
3 people authored Mar 14, 2020
1 parent 44ac5fe commit 69d69f2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
9 changes: 9 additions & 0 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
72 changes: 45 additions & 27 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ declare module 'discord.js' {
}

export class CategoryChannel extends GuildChannel {
public readonly children: Collection<Snowflake, GuildChannel>;
public readonly children: Collection<Snowflake, Exclude<GuildChannelTypes, CategoryChannel>>;
public type: 'category';
}

Expand All @@ -143,8 +143,8 @@ declare module 'discord.js' {
public deleted: boolean;
public id: Snowflake;
public type: keyof typeof ChannelType;
public delete(reason?: string): Promise<Channel>;
public fetch(): Promise<Channel>;
public delete(reason?: string): Promise<ChannelTypes>;
public fetch(): Promise<ChannelTypes>;
public toString(): string;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -710,7 +722,7 @@ declare module 'discord.js' {
export class Guild extends Base {
constructor(client: Client, data: object);
private _sortedRoles(): Collection<Snowflake, Role>;
private _sortedChannels(channel: Channel): Collection<Snowflake, GuildChannel>;
private _sortedChannels(channel: Channel): Collection<Snowflake, GuildChannelTypes>;
private _memberSpeakUpdate(user: Snowflake, speaking: boolean): void;

public readonly afkChannel: VoiceChannel | null;
Expand All @@ -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;
Expand Down Expand Up @@ -857,6 +869,7 @@ declare module 'discord.js' {
public readonly members: Collection<Snowflake, GuildMember>;
public name: string;
public readonly parent: CategoryChannel | null;
public readonly partial: false;
public parentID: Snowflake | null;
public permissionOverwrites: Collection<Snowflake, PermissionOverwrites>;
public readonly permissionsLocked: boolean | null;
Expand All @@ -882,7 +895,7 @@ declare module 'discord.js' {
public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly<Permissions> | null;
public setName(name: string, reason?: string): Promise<this>;
public setParent(
channel: GuildChannel | Snowflake,
channel: CategoryChannel | Snowflake,
options?: { lockPermissions?: boolean; reason?: string },
): Promise<this>;
public setPosition(position: number, options?: { relative?: boolean; reason?: string }): Promise<this>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1106,11 +1119,11 @@ declare module 'discord.js' {
}

export class MessageCollector extends Collector<Snowflake, Message> {
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;

Expand Down Expand Up @@ -1173,7 +1186,7 @@ declare module 'discord.js' {
roles: Snowflake[] | Collection<Snowflake, Role>,
everyone: boolean,
);
private _channels: Collection<Snowflake, GuildChannel> | null;
private _channels: Collection<Snowflake, GuildChannelTypes> | null;
private readonly _content: Message;
private _members: Collection<Snowflake, GuildMember> | null;

Expand All @@ -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;
Expand Down Expand Up @@ -1238,9 +1251,9 @@ declare module 'discord.js' {
}

export class PermissionOverwrites {
constructor(guildChannel: GuildChannel, data?: object);
constructor(guildChannel: GuildChannelTypes, data?: object);
public allow: Readonly<Permissions>;
public readonly channel: GuildChannel;
public readonly channel: GuildChannelTypes;
public deny: Readonly<Permissions>;
public id: Snowflake;
public type: OverwriteType;
Expand Down Expand Up @@ -1281,7 +1294,7 @@ declare module 'discord.js' {

export class ReactionCollector extends Collector<Snowflake, MessageReaction> {
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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1918,9 +1932,9 @@ declare module 'discord.js' {

//#region Managers

export class ChannelManager extends BaseManager<Snowflake, Channel, ChannelResolvable> {
export class ChannelManager extends BaseManager<Snowflake, ChannelTypes, ChannelResolvable> {
constructor(client: Client, iterable: Iterable<any>);
public fetch(id: Snowflake, cache?: boolean): Promise<Channel>;
public fetch(id: Snowflake, cache?: boolean): Promise<ChannelTypes>;
}

export abstract class BaseManager<K, Holds, R> {
Expand All @@ -1935,7 +1949,7 @@ declare module 'discord.js' {
public resolveID(resolvable: R): K | null;
}

export class GuildChannelManager extends BaseManager<Snowflake, GuildChannel, GuildChannelResolvable> {
export class GuildChannelManager extends BaseManager<Snowflake, GuildChannelTypes, GuildChannelResolvable> {
constructor(guild: Guild, iterable?: Iterable<any>);
public guild: Guild;
public create(name: string, options: GuildCreateChannelOptions & { type: 'voice' }): Promise<VoiceChannel>;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2864,7 +2882,7 @@ declare module 'discord.js' {
[K in keyof Omit<T, 'id' | 'partial'>]: T[K] | null;
};

interface PartialChannel extends Partialize<Channel> {}
interface PartialChannel extends Partialize<ChannelTypes> {}

interface PartialChannelData {
id?: number;
Expand Down

0 comments on commit 69d69f2

Please sign in to comment.