Skip to content

Commit

Permalink
types(interactions): pass Cached type to return type of methods (#8619
Browse files Browse the repository at this point in the history
)
  • Loading branch information
almeidx authored Sep 15, 2022
1 parent fc9653f commit 053da5b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
24 changes: 15 additions & 9 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,11 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
): Promise<Message<BooleanCache<Cached>>>;
public deferReply(options?: InteractionDeferReplyOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public deleteReply(): Promise<void>;
public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<Message>;
public fetchReply(): Promise<Message>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message>;
public editReply(
options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message<BooleanCache<Cached>>>;
public fetchReply(): Promise<Message<BooleanCache<Cached>>>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
Expand Down Expand Up @@ -1806,14 +1808,16 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
): Promise<Message<BooleanCache<Cached>>>;
public deferUpdate(options?: InteractionDeferUpdateOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public deleteReply(): Promise<void>;
public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<Message>;
public fetchReply(): Promise<Message>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message>;
public editReply(
options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message<BooleanCache<Cached>>>;
public fetchReply(): Promise<Message<BooleanCache<Cached>>>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
public update(options: InteractionUpdateOptions & { fetchReply: true }): Promise<Message>;
public update(options: InteractionUpdateOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public update(
options: string | MessagePayload | InteractionUpdateOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
Expand Down Expand Up @@ -1989,7 +1993,7 @@ export class ModalSubmitInteraction<Cached extends CacheType = CacheType> extend
public message: Message<BooleanCache<Cached>> | null;
public replied: boolean;
public readonly webhook: InteractionWebhook;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message>;
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message<BooleanCache<Cached>>>;
public reply(
options: string | MessagePayload | InteractionReplyOptions,
): Promise<InteractionResponse<BooleanCache<Cached>>>;
Expand All @@ -2003,7 +2007,9 @@ export class ModalSubmitInteraction<Cached extends CacheType = CacheType> extend
public deferReply(options?: InteractionDeferReplyOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public fetchReply(): Promise<Message<BooleanCache<Cached>>>;
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message<BooleanCache<Cached>>>;
public deferUpdate(options: InteractionDeferUpdateOptions & { fetchReply: true }): Promise<Message>;
public deferUpdate(
options: InteractionDeferUpdateOptions & { fetchReply: true },
): Promise<Message<BooleanCache<Cached>>>;
public deferUpdate(options?: InteractionDeferUpdateOptions): Promise<InteractionResponse<BooleanCache<Cached>>>;
public inGuild(): this is ModalSubmitInteraction<'raw' | 'cached'>;
public inCachedGuild(): this is ModalSubmitInteraction<'cached'>;
Expand Down
74 changes: 70 additions & 4 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import {
InteractionWebhook,
GuildAuditLogsActionType,
GuildAuditLogsTargetType,
ModalSubmitInteraction,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -1488,19 +1489,37 @@ client.on('interactionCreate', async interaction => {
expectType<MessageActionRowComponent>(interaction.component);
expectType<Message<true>>(interaction.message);
expectType<Guild>(interaction.guild);
expectAssignable<Promise<Message>>(interaction.reply({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<true>>>(interaction.fetchReply());
expectType<Promise<Message<true>>>(interaction.update({ content: 'a', fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inRawGuild()) {
expectAssignable<MessageComponentInteraction>(interaction);
expectType<APIButtonComponent | APISelectMenuComponent>(interaction.component);
expectType<Message<false>>(interaction.message);
expectType<null>(interaction.guild);
expectType<Promise<Message<false>>>(interaction.reply({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<false>>>(interaction.fetchReply());
expectType<Promise<Message<false>>>(interaction.update({ content: 'a', fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inGuild()) {
expectAssignable<MessageComponentInteraction>(interaction);
expectType<MessageActionRowComponent | APIButtonComponent | APISelectMenuComponent>(interaction.component);
expectType<Message>(interaction.message);
expectType<Guild | null>(interaction.guild);
expectType<Promise<Message>>(interaction.reply({ fetchReply: true }));
expectType<Promise<Message>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message>>(interaction.fetchReply());
expectType<Promise<Message>>(interaction.update({ content: 'a', fetchReply: true }));
expectType<Promise<Message>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message>>(interaction.followUp({ content: 'a' }));
}
}

Expand Down Expand Up @@ -1531,12 +1550,27 @@ client.on('interactionCreate', async interaction => {
expectAssignable<ContextMenuCommandInteraction>(interaction);
expectAssignable<Guild>(interaction.guild);
expectAssignable<CommandInteraction<'cached'>>(interaction);
expectType<Promise<Message<true>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<true>>>(interaction.fetchReply());
expectType<Promise<Message<true>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inRawGuild()) {
expectAssignable<ContextMenuCommandInteraction>(interaction);
expectType<null>(interaction.guild);
expectType<Promise<Message<false>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<false>>>(interaction.fetchReply());
expectType<Promise<Message<false>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inGuild()) {
expectAssignable<ContextMenuCommandInteraction>(interaction);
expectType<Guild | null>(interaction.guild);
expectType<Promise<Message>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message>>(interaction.fetchReply());
expectType<Promise<Message>>(interaction.followUp({ content: 'a' }));
}
}

Expand All @@ -1563,7 +1597,7 @@ client.on('interactionCreate', async interaction => {
expectType<ButtonComponent>(interaction.component);
expectType<Message<true>>(interaction.message);
expectType<Guild>(interaction.guild);
expectAssignable<Promise<Message>>(interaction.reply({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.reply({ fetchReply: true }));
} else if (interaction.inRawGuild()) {
expectAssignable<ButtonInteraction>(interaction);
expectType<APIButtonComponent>(interaction.component);
Expand Down Expand Up @@ -1678,6 +1712,38 @@ client.on('interactionCreate', async interaction => {
expectAssignable<CommandInteraction>(interaction);
expectAssignable<RepliableInteraction>(interaction);
}

if (interaction.type === InteractionType.ModalSubmit && interaction.isRepliable()) {
expectType<ModalSubmitInteraction>(interaction);
if (interaction.inCachedGuild()) {
expectAssignable<ModalSubmitInteraction>(interaction);
expectType<Guild>(interaction.guild);
expectType<Promise<Message<true>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<true>>>(interaction.fetchReply());
expectType<Promise<Message<true>>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message<true>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inRawGuild()) {
expectAssignable<ModalSubmitInteraction>(interaction);
expectType<null>(interaction.guild);
expectType<Promise<Message<false>>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message<false>>>(interaction.fetchReply());
expectType<Promise<Message<false>>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message<false>>>(interaction.followUp({ content: 'a' }));
} else if (interaction.inGuild()) {
expectAssignable<ModalSubmitInteraction>(interaction);
expectType<Guild | null>(interaction.guild);
expectType<Promise<Message>>(interaction.reply({ content: 'a', fetchReply: true }));
expectType<Promise<Message>>(interaction.deferReply({ fetchReply: true }));
expectType<Promise<Message>>(interaction.editReply({ content: 'a' }));
expectType<Promise<Message>>(interaction.fetchReply());
expectType<Promise<Message>>(interaction.deferUpdate({ fetchReply: true }));
expectType<Promise<Message>>(interaction.followUp({ content: 'a' }));
}
}
});

declare const shard: Shard;
Expand Down

0 comments on commit 053da5b

Please sign in to comment.