Skip to content

Commit

Permalink
feat: add typeguard to BaseInteraction#isRepliable (#8565)
Browse files Browse the repository at this point in the history
* feat: add typeguard to BaseInteraction#isRepliable

* fix: redefine RepliableInteraaction

...to exclude impossible interactions rather than specify possible ones

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* fix: remove InteractionResponseFields

* fix: replace InteractionResponseFields with RepliableInteraction in index test

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 6, 2022
1 parent c334157 commit 55c3ee2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
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> {
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

0 comments on commit 55c3ee2

Please sign in to comment.