Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typeguard to BaseInteraction#isRepliable #8565

Merged
merged 12 commits into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 2 additions & 23 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -398,28 +398,6 @@ export type GuildCacheMessage<Cached extends CacheType> = CacheTypeReducer<
Message | APIMessage
>;

export interface InteractionResponseFields<Cached extends CacheType = CacheType> {
MaksiRose marked this conversation as resolved.
Show resolved Hide resolved
deferred: boolean;
ephemeral: boolean | null;
replied: boolean;
webhook: InteractionWebhook;
reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message>;
reply(options: string | MessagePayload | InteractionReplyOptions): Promise<void>;
deleteReply(): Promise<void>;
editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<Message>;
deferReply(options: InteractionDeferReplyOptions & { fetchReply: true }): Promise<Message>;
deferReply(options?: InteractionDeferReplyOptions): Promise<void>;
fetchReply(): Promise<Message>;
followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message>;
showModal(
modal:
| JSONEncodable<APIModalInteractionResponseCallbackData>
| ModalComponentData
| APIModalInteractionResponseCallbackData,
): Promise<void>;
awaitModalSubmit(options: AwaitModalSubmitOptions<ModalSubmitInteraction>): Promise<ModalSubmitInteraction<Cached>>;
}

export type BooleanCache<T extends CacheType> = T extends 'cached' ? true : false;

export abstract class CommandInteraction<Cached extends CacheType = CacheType> extends BaseInteraction<Cached> {
Expand Down Expand Up @@ -1504,6 +1482,7 @@ export type Interaction<Cached extends CacheType = CacheType> =
| AutocompleteInteraction<Cached>
| ModalSubmitInteraction<Cached>;

export type RepliableInteraction<Cached extends CacheType = CacheType> = Exclude<Interaction, AutocompleteInteraction>;
export class BaseInteraction<Cached extends CacheType = CacheType> extends Base {
// This a technique used to brand different cached types. Or else we'll get `never` errors on typeguard checks.
private readonly _cacheType: Cached;
Expand Down Expand Up @@ -1544,7 +1523,7 @@ export class BaseInteraction<Cached extends CacheType = CacheType> extends Base
public isModalSubmit(): this is ModalSubmitInteraction<Cached>;
public isUserContextMenuCommand(): this is UserContextMenuCommandInteraction<Cached>;
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
public isRepliable(): this is this & InteractionResponseFields<Cached>;
public isRepliable(): this is RepliableInteraction<Cached>;
}

export class InteractionCollector<T extends CollectedInteraction> extends Collector<
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/typings/index.test-d.ts
Expand Up @@ -100,7 +100,7 @@ import {
ActionRowBuilder,
ButtonComponent,
SelectMenuComponent,
InteractionResponseFields,
RepliableInteraction,
ThreadChannelType,
Events,
WebSocketShardEvents,
Expand Down Expand Up @@ -1519,7 +1519,7 @@ client.on('interactionCreate', async interaction => {
}

if (interaction.isRepliable()) {
expectAssignable<InteractionResponseFields>(interaction);
expectAssignable<RepliableInteraction>(interaction);
interaction.reply('test');
}

Expand All @@ -1529,7 +1529,7 @@ client.on('interactionCreate', async interaction => {
interaction.isRepliable()
) {
expectAssignable<CommandInteraction>(interaction);
expectAssignable<InteractionResponseFields>(interaction);
expectAssignable<RepliableInteraction>(interaction);
}
});

Expand Down