fix: guard types

This commit is contained in:
MARCROCK22 2024-08-04 02:28:24 +00:00
parent d064b1b802
commit 942dbd12bf

View File

@ -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) { static from(client: UsingClient, gateway: GatewayInteractionCreateDispatchData, __reply?: __InternalReplyFunction) {
switch (gateway.type) { switch (gateway.type) {
case InteractionType.ApplicationCommandAutocomplete: case InteractionType.ApplicationCommandAutocomplete:
@ -346,6 +390,10 @@ export class AutocompleteInteraction<FromGuild extends boolean = boolean> extend
return super.reply({ data: { choices }, type: InteractionResponseType.ApplicationCommandAutocompleteResult }); return super.reply({ data: { choices }, type: InteractionResponseType.ApplicationCommandAutocompleteResult });
} }
isAutocomplete(): this is AutocompleteInteraction {
return true;
}
/** @intenal */ /** @intenal */
async reply(..._args: unknown[]) { async reply(..._args: unknown[]) {
throw new Error('Cannot use reply in this interaction'); throw new Error('Cannot use reply in this interaction');
@ -420,7 +468,7 @@ export class ApplicationCommandInteraction<
FromGuild extends boolean = boolean, FromGuild extends boolean = boolean,
Type extends APIApplicationCommandInteraction = APIApplicationCommandInteraction, Type extends APIApplicationCommandInteraction = APIApplicationCommandInteraction,
> extends Interaction<FromGuild, Type> { > extends Interaction<FromGuild, Type> {
type = ApplicationCommandType.ChatInput; type!: ApplicationCommandType;
respond( respond(
data: data:
| APIInteractionResponseChannelMessageWithSource | APIInteractionResponseChannelMessageWithSource
@ -470,34 +518,14 @@ export class ComponentInteraction<
get componentType() { get componentType() {
return this.data.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 { export class ButtonInteraction extends ComponentInteraction {
declare data: ObjectToLower<APIMessageButtonInteractionData>; declare data: ObjectToLower<APIMessageButtonInteractionData>;
isButton(): this is ButtonInteraction {
return true;
}
} }
export class SelectMenuInteraction extends ComponentInteraction { export class SelectMenuInteraction extends ComponentInteraction {
@ -519,11 +547,15 @@ export class SelectMenuInteraction extends ComponentInteraction {
export class StringSelectMenuInteraction< export class StringSelectMenuInteraction<
T extends any[] = string[], T extends any[] = string[],
> extends (SelectMenuInteraction as unknown as ToClass< > extends (SelectMenuInteraction as unknown as ToClass<
Omit<SelectMenuInteraction, 'data'>, Omit<SelectMenuInteraction, 'data' | 'isStringSelectMenu'>,
StringSelectMenuInteraction StringSelectMenuInteraction
>) { >) {
declare data: OmitInsert<ObjectToLower<APIMessageStringSelectInteractionData>, 'values', { values: T }>; declare data: OmitInsert<ObjectToLower<APIMessageStringSelectInteractionData>, 'values', { values: T }>;
declare values: T; declare values: T;
isStringSelectMenu(): this is StringSelectMenuInteraction {
return true;
}
} }
export class ChannelSelectMenuInteraction extends SelectMenuInteraction { export class ChannelSelectMenuInteraction extends SelectMenuInteraction {
@ -537,6 +569,10 @@ export class ChannelSelectMenuInteraction extends SelectMenuInteraction {
const resolved = (interaction.data as APIMessageChannelSelectInteractionData).resolved; const resolved = (interaction.data as APIMessageChannelSelectInteractionData).resolved;
this.channels = this.values.map(x => channelFrom(resolved.channels[x], this.client)); this.channels = this.values.map(x => channelFrom(resolved.channels[x], this.client));
} }
isChannelSelectMenu(): this is ChannelSelectMenuInteraction {
return true;
}
} }
export class MentionableSelectMenuInteraction extends SelectMenuInteraction { 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])) : []; 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 { export class RoleSelectMenuInteraction extends SelectMenuInteraction {
@ -578,6 +618,10 @@ export class RoleSelectMenuInteraction extends SelectMenuInteraction {
const resolved = (interaction.data as APIMessageRoleSelectInteractionData).resolved; const resolved = (interaction.data as APIMessageRoleSelectInteractionData).resolved;
this.roles = this.values.map(x => Transformers.GuildRole(this.client, resolved.roles[x], this.guildId!)); 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 { export class UserSelectMenuInteraction extends SelectMenuInteraction {
@ -602,6 +646,10 @@ export class UserSelectMenuInteraction extends SelectMenuInteraction {
) )
: []; : [];
} }
isUserSelectMenu(): this is UserSelectMenuInteraction {
return true;
}
} }
export class ChatInputCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction< export class ChatInputCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction<
@ -609,6 +657,10 @@ export class ChatInputCommandInteraction<FromGuild extends boolean = boolean> ex
APIChatInputApplicationCommandInteraction APIChatInputApplicationCommandInteraction
> { > {
declare data: ObjectToLower<APIChatInputApplicationCommandInteractionData>; declare data: ObjectToLower<APIChatInputApplicationCommandInteractionData>;
isChatInput(): this is ChatInputCommandInteraction {
return true;
}
} }
export class UserCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction< export class UserCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction<
@ -617,6 +669,10 @@ export class UserCommandInteraction<FromGuild extends boolean = boolean> extends
> { > {
declare type: ApplicationCommandType.User; declare type: ApplicationCommandType.User;
declare data: ObjectToLower<APIUserApplicationCommandInteractionData>; declare data: ObjectToLower<APIUserApplicationCommandInteractionData>;
isUser(): this is UserCommandInteraction {
return true;
}
} }
export class MessageCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction< export class MessageCommandInteraction<FromGuild extends boolean = boolean> extends ApplicationCommandInteraction<
@ -625,6 +681,10 @@ export class MessageCommandInteraction<FromGuild extends boolean = boolean> exte
> { > {
declare type: ApplicationCommandType.Message; declare type: ApplicationCommandType.Message;
declare data: ObjectToLower<APIMessageApplicationCommandInteractionData>; declare data: ObjectToLower<APIMessageApplicationCommandInteractionData>;
isMessage(): this is MessageCommandInteraction {
return true;
}
} }
export interface ModalSubmitInteraction<FromGuild extends boolean = boolean> export interface ModalSubmitInteraction<FromGuild extends boolean = boolean>
@ -654,4 +714,8 @@ export class ModalSubmitInteraction<FromGuild extends boolean = boolean> extends
if (!value && required) throw new Error(`${customId} component doesn't have a value`); if (!value && required) throw new Error(`${customId} component doesn't have a value`);
return value; return value;
} }
isModal(): this is ModalSubmitInteraction {
return true;
}
} }