Skip to content

Commit

Permalink
refactor: added TextBasedChannels type
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Aug 3, 2021
1 parent 35fa3b3 commit 4f7ad3e
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (client, { d: data }) => {
* Emitted whenever the pins of a channel are updated. Due to the nature of the WebSocket event,
* not much information can be provided easily here - you need to manually check the pins yourself.
* @event Client#channelPinsUpdate
* @param {DMChannel|TextChannel|NewsChannel} channel The channel that the pins update occurred in
* @param {TextBasedChannels} channel The channel that the pins update occurred in
* @param {Date} time The time of the pins update
*/
client.emit(Events.CHANNEL_PINS_UPDATE, channel, time);
Expand Down
2 changes: 1 addition & 1 deletion src/managers/MessageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MessageManager extends CachedManager {

/**
* The channel that the messages belong to
* @type {TextBasedChannel}
* @type {TextBasedChannels}
*/
this.channel = channel;
}
Expand Down
3 changes: 1 addition & 2 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ class Channel extends Base {
}

/**
* Indicates whether this channel is text-based
* ({@link TextChannel}, {@link DMChannel}, {@link NewsChannel} or {@link ThreadChannel}).
* Indicates whether this channel is {@link TextBasedChannels text-based}.
* @returns {boolean}
*/
isText() {
Expand Down
2 changes: 1 addition & 1 deletion src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CommandInteraction extends Interaction {

/**
* The channel this interaction was sent in
* @type {?(TextChannel|NewsChannel|DMChannel)}
* @type {?TextBasedChannels}
* @name CommandInteraction#channel
* @readonly
*/
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Interaction extends Base {

/**
* The channel this interaction was sent in
* @type {?Channel}
* @type {?TextBasedChannels}
* @readonly
*/
get channel() {
Expand Down
4 changes: 2 additions & 2 deletions src/structures/InteractionCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { InteractionTypes, MessageComponentTypes } = require('../util/Constants')

/**
* @typedef {CollectorOptions} InteractionCollectorOptions
* @property {TextChannel|DMChannel|NewsChannel} [channel] The channel to listen to interactions from
* @property {TextBasedChannels} [channel] The channel to listen to interactions from
* @property {MessageComponentType} [componentType] The type of component to listen for
* @property {Guild} [guild] The guild to listen to interactions from
* @property {InteractionType} [interactionType] The type of interaction to listen for
Expand Down Expand Up @@ -39,7 +39,7 @@ class InteractionCollector extends Collector {

/**
* The channel from which to collect interactions, if provided
* @type {?(TextChannel|DMChannel|NewsChannel)}
* @type {?TextBasedChannels}
*/
this.channel = this.message?.channel ?? options.channel ?? null;

Expand Down
4 changes: 2 additions & 2 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class Message extends Base {
/**
* @param {Client} client The instantiating client
* @param {APIMessage} data The data for the message
* @param {TextChannel|DMChannel|NewsChannel|ThreadChannel} channel The channel the message was sent in
* @param {TextBasedChannels} channel The channel the message was sent in
*/
constructor(client, data, channel) {
super(client);

/**
* The channel that the message was sent in
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}
* @type {TextBasedChannels}
*/
this.channel = channel;

Expand Down
4 changes: 2 additions & 2 deletions src/structures/MessageCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { Events } = require('../util/Constants');
*/
class MessageCollector extends Collector {
/**
* @param {TextChannel|DMChannel} channel The channel
* @param {TextBasedChannels} channel The channel
* @param {MessageCollectorOptions} options The options to be applied to this collector
* @emits MessageCollector#message
*/
Expand All @@ -25,7 +25,7 @@ class MessageCollector extends Collector {

/**
* The channel
* @type {TextBasedChannel}
* @type {TextBasedChannels}
*/
this.channel = channel;

Expand Down
4 changes: 2 additions & 2 deletions src/structures/Typing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Base = require('./Base');
*/
class Typing extends Base {
/**
* @param {TextChannel|DMChannel|NewsChannel|ThreadChannel} channel The channel this typing came from
* @param {TextBasedChannels} channel The channel this typing came from
* @param {User} user The user that started typing
* @param {APITypingStart} data The raw data received
*/
Expand All @@ -17,7 +17,7 @@ class Typing extends Base {

/**
* The channel the status is from
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}
* @type {TextBasedChannels}
*/
this.channel = channel;

Expand Down
10 changes: 10 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ exports.ChannelTypes = createEnum([
'GUILD_STAGE_VOICE',
]);

/**
* The channels that are text-based.
* * PartialDMChannel
* * DMChannel
* * TextChannel
* * NewsChannel
* * ThreadChannel
* @typedef {PartialDMChannel|DMChannel|TextChannel|NewsChannel|ThreadChannel} TextBasedChannels
*/

/**
* The types of channels that are text-based. The available types are:
* * DM
Expand Down
48 changes: 18 additions & 30 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class Channel extends Base {
public type: keyof typeof ChannelTypes;
public delete(): Promise<Channel>;
public fetch(force?: boolean): Promise<Channel>;
public isText(): this is TextChannel | DMChannel | NewsChannel | ThreadChannel;
public isText(): this is TextBasedChannels;
public isThread(): this is ThreadChannel;
public toString(): ChannelMention;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
export class CommandInteraction extends Interaction {
public constructor(client: Client, data: RawCommandInteractionData);
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
public readonly channel: TextChannel | DMChannel | NewsChannel | PartialDMChannel | ThreadChannel | null;
public readonly channel: TextBasedChannels | null;
public channelId: Snowflake;
public commandId: Snowflake;
public commandName: string;
Expand Down Expand Up @@ -939,7 +939,7 @@ export class Intents extends BitField<IntentsString> {
export class Interaction extends Base {
public constructor(client: Client, data: RawInteractionData);
public applicationId: Snowflake;
public readonly channel: Channel | PartialDMChannel | null;
public readonly channel: TextBasedChannels | null;
public channelId: Snowflake | null;
public readonly createdAt: Date;
public readonly createdTimestamp: number;
Expand All @@ -964,7 +964,7 @@ export class InteractionCollector<T extends Interaction> extends Collector<Snowf
private _handleChannelDeletion(channel: GuildChannel): void;
private _handleGuildDeletion(guild: Guild): void;

public channel: TextChannel | NewsChannel | DMChannel | null;
public channel: TextBasedChannels | null;
public componentType: MessageComponentType | null;
public readonly endReason: string | null;
public guild: Guild | null;
Expand Down Expand Up @@ -1048,11 +1048,7 @@ export class LimitedCollection<K, V> extends Collection<K, V> {
}

export class Message extends Base {
public constructor(
client: Client,
data: RawMessageData,
channel: TextChannel | DMChannel | NewsChannel | ThreadChannel,
);
public constructor(client: Client, data: RawMessageData, channel: TextBasedChannels);
private _patch(data: RawPartialMessageData, partial: true): Message;
private _patch(data: RawMessageData, partial?: boolean): Message;
private _update(data: RawPartialMessageData, partial: true): Message;
Expand All @@ -1062,7 +1058,7 @@ export class Message extends Base {
public applicationId: Snowflake | null;
public attachments: Collection<Snowflake, MessageAttachment>;
public author: User;
public channel: TextChannel | DMChannel | NewsChannel | ThreadChannel;
public channel: TextBasedChannels;
public readonly cleanContent: string;
public components: MessageActionRow[];
public content: string;
Expand Down Expand Up @@ -1175,11 +1171,11 @@ export class MessageButton extends BaseMessageComponent {
}

export class MessageCollector extends Collector<Snowflake, Message> {
public constructor(channel: TextChannel | DMChannel | ThreadChannel, options?: MessageCollectorOptions);
public constructor(channel: TextBasedChannels, options?: MessageCollectorOptions);
private _handleChannelDeletion(channel: GuildChannel): void;
private _handleGuildDeletion(guild: Guild): void;

public channel: TextChannel | DMChannel | ThreadChannel;
public channel: TextBasedChannels;
public readonly endReason: string | null;
public options: MessageCollectorOptions;
public received: number;
Expand All @@ -1190,7 +1186,7 @@ export class MessageCollector extends Collector<Snowflake, Message> {

export class MessageComponentInteraction extends Interaction {
public constructor(client: Client, data: RawMessageComponentInteractionData);
public readonly channel: TextChannel | DMChannel | NewsChannel | PartialDMChannel | ThreadChannel | null;
public readonly channel: TextBasedChannels | null;
public readonly component: MessageActionRowComponent | Exclude<APIMessageComponent, APIActionRowComponent> | null;
public componentType: MessageComponentType;
public customId: string;
Expand Down Expand Up @@ -1813,12 +1809,8 @@ export class ThreadMemberFlags extends BitField<ThreadMemberFlagsString> {
}

export class Typing extends Base {
public constructor(
channel: TextChannel | PartialDMChannel | NewsChannel | ThreadChannel,
user: PartialUser,
data?: RawTypingData,
);
public channel: TextChannel | PartialDMChannel | NewsChannel | ThreadChannel;
public constructor(channel: TextBasedChannels, user: PartialUser, data?: RawTypingData);
public channel: TextBasedChannels;
public user: PartialUser;
public startedTimestamp: number;
public readonly startedAt: Date;
Expand Down Expand Up @@ -2499,8 +2491,8 @@ export class GuildMemberRoleManager extends DataManager<Snowflake, Role, RoleRes
}

export class MessageManager extends CachedManager<Snowflake, Message, MessageResolvable> {
public constructor(channel: TextChannel | DMChannel | ThreadChannel, iterable?: Iterable<RawMessageData>);
public channel: TextBasedChannelFields;
public constructor(channel: TextBasedChannels, iterable?: Iterable<RawMessageData>);
public channel: TextBasedChannels;
public cache: Collection<Snowflake, Message>;
public crosspost(message: MessageResolvable): Promise<Message>;
public delete(message: MessageResolvable): Promise<void>;
Expand Down Expand Up @@ -3039,7 +3031,7 @@ export interface ClientEvents {
applicationCommandUpdate: [oldCommand: ApplicationCommand | null, newCommand: ApplicationCommand];
channelCreate: [channel: GuildChannel];
channelDelete: [channel: DMChannel | GuildChannel];
channelPinsUpdate: [channel: TextChannel | NewsChannel | DMChannel | PartialDMChannel, date: Date];
channelPinsUpdate: [channel: TextBasedChannels, date: Date];
channelUpdate: [oldChannel: DMChannel | GuildChannel, newChannel: DMChannel | GuildChannel];
debug: [message: string];
warn: [message: string];
Expand Down Expand Up @@ -3788,7 +3780,7 @@ export interface IntegrationAccount {
}

export interface InteractionCollectorOptions<T extends Interaction> extends CollectorOptions<[T]> {
channel?: TextChannel | DMChannel | NewsChannel | ThreadChannel;
channel?: TextBasedChannels;
componentType?: MessageComponentType | MessageComponentTypes;
guild?: Guild;
interactionType?: InteractionType | InteractionTypes;
Expand Down Expand Up @@ -4421,13 +4413,9 @@ export interface LimitedCollectionOptions<K, V> {
sweepInterval?: number;
}

export type TextBasedChannelTypes =
| 'DM'
| 'GUILD_TEXT'
| 'GUILD_NEWS'
| 'GUILD_NEWS_THREAD'
| 'GUILD_PUBLIC_THREAD'
| 'GUILD_PRIVATE_THREAD';
export type TextBasedChannels = PartialDMChannel | DMChannel | TextChannel | NewsChannel | ThreadChannel;

export type TextBasedChannelTypes = TextBasedChannels['type'];

export type TextChannelResolvable = Snowflake | TextChannel;

Expand Down

0 comments on commit 4f7ad3e

Please sign in to comment.