mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
feat(interaction): add isRepliable type guard (#7259)
Co-authored-by: Almeida <almeidx@pm.me> Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com>
This commit is contained in:
@@ -226,6 +226,14 @@ class Interaction extends Base {
|
||||
ComponentType[this.componentType] === ComponentType.SelectMenu
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this interaction can be replied to.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isRepliable() {
|
||||
return ![InteractionType.Ping, InteractionType.ApplicationCommandAutocomplete].includes(InteractionType[this.type]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Interaction;
|
||||
|
||||
12
packages/discord.js/typings/index.d.ts
vendored
12
packages/discord.js/typings/index.d.ts
vendored
@@ -324,6 +324,17 @@ export type GuildCacheMessage<Cached extends CacheType> = CacheTypeReducer<
|
||||
Message | APIMessage
|
||||
>;
|
||||
|
||||
export interface InteractionResponseFields<Cached extends CacheType = CacheType> {
|
||||
reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<GuildCacheMessage<Cached>>;
|
||||
reply(options: string | MessagePayload | InteractionReplyOptions): Promise<void>;
|
||||
deleteReply(): Promise<void>;
|
||||
editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<GuildCacheMessage<Cached>>;
|
||||
deferReply(options: InteractionDeferReplyOptions & { fetchReply: true }): Promise<GuildCacheMessage<Cached>>;
|
||||
deferReply(options?: InteractionDeferReplyOptions): Promise<void>;
|
||||
fetchReply(): Promise<GuildCacheMessage<Cached>>;
|
||||
followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<GuildCacheMessage<Cached>>;
|
||||
}
|
||||
|
||||
export abstract class CommandInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
|
||||
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
||||
public options: Omit<
|
||||
@@ -1327,6 +1338,7 @@ export class Interaction<Cached extends CacheType = CacheType> extends Base {
|
||||
public isUserContextMenuCommand(): this is UserContextMenuCommandInteraction<Cached>;
|
||||
public isMessageComponent(): this is MessageComponentInteraction<Cached>;
|
||||
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
|
||||
public isRepliable(): this is this & InteractionResponseFields<Cached>;
|
||||
}
|
||||
|
||||
export class InteractionCollector<T extends Interaction> extends Collector<Snowflake, T> {
|
||||
|
||||
@@ -92,6 +92,7 @@ import {
|
||||
ButtonComponent,
|
||||
SelectMenuComponent,
|
||||
ActionRowComponent,
|
||||
InteractionResponseFields,
|
||||
} from '.';
|
||||
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||
|
||||
@@ -1124,6 +1125,16 @@ client.on('interactionCreate', async interaction => {
|
||||
expectType<string | null>(interaction.options.getSubcommandGroup(booleanValue));
|
||||
expectType<string | null>(interaction.options.getSubcommandGroup(false));
|
||||
}
|
||||
|
||||
if (interaction.isRepliable()) {
|
||||
expectAssignable<InteractionResponseFields>(interaction);
|
||||
interaction.reply('test');
|
||||
}
|
||||
|
||||
if (interaction.isChatInputCommand() && interaction.isRepliable()) {
|
||||
expectAssignable<CommandInteraction>(interaction);
|
||||
expectAssignable<InteractionResponseFields>(interaction);
|
||||
}
|
||||
});
|
||||
|
||||
declare const shard: Shard;
|
||||
|
||||
Reference in New Issue
Block a user