From 942dbd12bf01a58965bd91fcd1450a5840949d3e Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Sun, 4 Aug 2024 02:28:24 +0000 Subject: [PATCH] fix: guard types --- src/structures/Interaction.ts | 116 ++++++++++++++++++++++++++-------- 1 file changed, 90 insertions(+), 26 deletions(-) diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 0ad5dfb..0fd248f 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -237,6 +237,50 @@ export class BaseInteraction< }); } + isButton(): this is ButtonInteraction { + return false; + } + + isChannelSelectMenu(): this is ChannelSelectMenuInteraction { + return false; + } + + isRoleSelectMenu(): this is RoleSelectMenuInteraction { + return false; + } + + isMentionableSelectMenu(): this is MentionableSelectMenuInteraction { + return false; + } + + isUserSelectMenu(): this is UserSelectMenuInteraction { + return false; + } + + isStringSelectMenu(): this is StringSelectMenuInteraction { + return false; + } + + isChatInput(): this is ChatInputCommandInteraction { + return false; + } + + isUser(): this is UserCommandInteraction { + return false; + } + + isMessage(): this is MessageCommandInteraction { + return false; + } + + isAutocomplete(): this is AutocompleteInteraction { + return false; + } + + isModal(): this is ModalSubmitInteraction { + return false; + } + static from(client: UsingClient, gateway: GatewayInteractionCreateDispatchData, __reply?: __InternalReplyFunction) { switch (gateway.type) { case InteractionType.ApplicationCommandAutocomplete: @@ -346,6 +390,10 @@ export class AutocompleteInteraction extend return super.reply({ data: { choices }, type: InteractionResponseType.ApplicationCommandAutocompleteResult }); } + isAutocomplete(): this is AutocompleteInteraction { + return true; + } + /** @intenal */ async reply(..._args: unknown[]) { throw new Error('Cannot use reply in this interaction'); @@ -420,7 +468,7 @@ export class ApplicationCommandInteraction< FromGuild extends boolean = boolean, Type extends APIApplicationCommandInteraction = APIApplicationCommandInteraction, > extends Interaction { - type = ApplicationCommandType.ChatInput; + type!: ApplicationCommandType; respond( data: | APIInteractionResponseChannelMessageWithSource @@ -470,34 +518,14 @@ export class ComponentInteraction< get componentType() { return this.data.componentType; } - - isButton(): this is ButtonInteraction { - return this.data.componentType === ComponentType.Button; - } - - isChannelSelectMenu(): this is ChannelSelectMenuInteraction { - return this.componentType === ComponentType.ChannelSelect; - } - - isRoleSelectMenu(): this is RoleSelectMenuInteraction { - return this.componentType === ComponentType.RoleSelect; - } - - isMentionableSelectMenu(): this is MentionableSelectMenuInteraction { - return this.componentType === ComponentType.MentionableSelect; - } - - isUserSelectMenu(): this is UserSelectMenuInteraction { - return this.componentType === ComponentType.UserSelect; - } - - isStringSelectMenu(): this is StringSelectMenuInteraction { - return this.componentType === ComponentType.StringSelect; - } } export class ButtonInteraction extends ComponentInteraction { declare data: ObjectToLower; + + isButton(): this is ButtonInteraction { + return true; + } } export class SelectMenuInteraction extends ComponentInteraction { @@ -519,11 +547,15 @@ export class SelectMenuInteraction extends ComponentInteraction { export class StringSelectMenuInteraction< T extends any[] = string[], > extends (SelectMenuInteraction as unknown as ToClass< - Omit, + Omit, StringSelectMenuInteraction >) { declare data: OmitInsert, 'values', { values: T }>; declare values: T; + + isStringSelectMenu(): this is StringSelectMenuInteraction { + return true; + } } export class ChannelSelectMenuInteraction extends SelectMenuInteraction { @@ -537,6 +569,10 @@ export class ChannelSelectMenuInteraction extends SelectMenuInteraction { const resolved = (interaction.data as APIMessageChannelSelectInteractionData).resolved; this.channels = this.values.map(x => channelFrom(resolved.channels[x], this.client)); } + + isChannelSelectMenu(): this is ChannelSelectMenuInteraction { + return true; + } } export class MentionableSelectMenuInteraction extends SelectMenuInteraction { @@ -565,6 +601,10 @@ export class MentionableSelectMenuInteraction extends SelectMenuInteraction { : []; this.users = resolved.users ? this.values.map(x => Transformers.User(this.client, resolved.users![x])) : []; } + + isMentionableSelectMenu(): this is MentionableSelectMenuInteraction { + return true; + } } export class RoleSelectMenuInteraction extends SelectMenuInteraction { @@ -578,6 +618,10 @@ export class RoleSelectMenuInteraction extends SelectMenuInteraction { const resolved = (interaction.data as APIMessageRoleSelectInteractionData).resolved; this.roles = this.values.map(x => Transformers.GuildRole(this.client, resolved.roles[x], this.guildId!)); } + + isRoleSelectMenu(): this is RoleSelectMenuInteraction { + return true; + } } export class UserSelectMenuInteraction extends SelectMenuInteraction { @@ -602,6 +646,10 @@ export class UserSelectMenuInteraction extends SelectMenuInteraction { ) : []; } + + isUserSelectMenu(): this is UserSelectMenuInteraction { + return true; + } } export class ChatInputCommandInteraction extends ApplicationCommandInteraction< @@ -609,6 +657,10 @@ export class ChatInputCommandInteraction ex APIChatInputApplicationCommandInteraction > { declare data: ObjectToLower; + + isChatInput(): this is ChatInputCommandInteraction { + return true; + } } export class UserCommandInteraction extends ApplicationCommandInteraction< @@ -617,6 +669,10 @@ export class UserCommandInteraction extends > { declare type: ApplicationCommandType.User; declare data: ObjectToLower; + + isUser(): this is UserCommandInteraction { + return true; + } } export class MessageCommandInteraction extends ApplicationCommandInteraction< @@ -625,6 +681,10 @@ export class MessageCommandInteraction exte > { declare type: ApplicationCommandType.Message; declare data: ObjectToLower; + + isMessage(): this is MessageCommandInteraction { + return true; + } } export interface ModalSubmitInteraction @@ -654,4 +714,8 @@ export class ModalSubmitInteraction extends if (!value && required) throw new Error(`${customId} component doesn't have a value`); return value; } + + isModal(): this is ModalSubmitInteraction { + return true; + } }