feat: add support for autocomplete interactions (#6672)

Co-authored-by: Suneet Tipirneni <suneettipirneni@icloud.com>
This commit is contained in:
Micah Benac
2021-10-28 18:47:50 -04:00
committed by GitHub
parent 14d9a9901b
commit ddf759c811
14 changed files with 224 additions and 16 deletions

2
typings/enums.d.ts vendored
View File

@@ -92,12 +92,14 @@ export const enum InteractionResponseTypes {
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
DEFERRED_MESSAGE_UPDATE = 6,
UPDATE_MESSAGE = 7,
APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8,
}
export const enum InteractionTypes {
PING = 1,
APPLICATION_COMMAND = 2,
MESSAGE_COMPONENT = 3,
APPLICATION_COMMAND_AUTOCOMPLETE = 4,
}
export const enum InviteTargetType {

71
typings/index.d.ts vendored
View File

@@ -277,8 +277,20 @@ export type GuildCacheMessage<Cached extends CacheType> = CacheTypeReducer<
>;
export abstract class BaseCommandInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
public options: CommandInteractionOptionResolver<Cached>;
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
public options: Omit<
CommandInteractionOptionResolver<Cached>,
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getNumber'
| 'getInteger'
| 'getString'
| 'getChannel'
| 'getBoolean'
| 'getSubcommandGroup'
| 'getSubcommand'
>;
public channelId: Snowflake;
public commandId: Snowflake;
public commandName: string;
@@ -589,10 +601,60 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
}
export interface ApplicationCommandInteractionOptionResolver<Cached extends CacheType = CacheType>
extends BaseCommandInteractionOptionResolver<Cached> {
getSubcommand(required?: true): string;
getSubcommand(required: boolean): string | null;
getSubcommandGroup(required?: true): string;
getSubcommandGroup(required: boolean): string | null;
getBoolean(name: string, required: true): boolean;
getBoolean(name: string, required?: boolean): boolean | null;
getChannel(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['channel']>;
getChannel(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['channel']> | null;
getString(name: string, required: true): string;
getString(name: string, required?: boolean): string | null;
getInteger(name: string, required: true): number;
getInteger(name: string, required?: boolean): number | null;
getNumber(name: string, required: true): number;
getNumber(name: string, required?: boolean): number | null;
getUser(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['user']>;
getUser(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['user']> | null;
getMember(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['member']>;
getMember(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['member']> | null;
getRole(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['role']>;
getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['role']> | null;
getMentionable(
name: string,
required: true,
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']>;
getMentionable(
name: string,
required?: boolean,
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
}
export class CommandInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
public options: Omit<CommandInteractionOptionResolver<Cached>, 'getMessage' | 'getFocused'>;
public inGuild(): this is CommandInteraction<'present'> & this;
public inCachedGuild(): this is CommandInteraction<'cached'> & this;
public inRawGuild(): this is CommandInteraction<'raw'> & this;
public toString(): string;
}
export class AutocompleteInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
public channelId: Snowflake;
public commandId: Snowflake;
public commandName: string;
public responded: boolean;
public options: Omit<CommandInteractionOptionResolver<Cached>, 'getMessage'>;
public inGuild(): this is CommandInteraction<'present'> & this;
public inCachedGuild(): this is CommandInteraction<'cached'> & this;
public inRawGuild(): this is CommandInteraction<'raw'> & this;
private transformOption(option: APIApplicationCommandOption): CommandInteractionOption;
public respond(options: ApplicationCommandOptionChoice[]): Promise<void>;
}
export class CommandInteractionOptionResolver<Cached extends CacheType = CacheType> {
private constructor(client: Client, options: CommandInteractionOption[], resolved: CommandInteractionResolvedData);
public readonly client: Client;
@@ -647,7 +709,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
public getMessage(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['message']>;
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
public getFocused(getFull: true): ApplicationCommandOptionChoice;
public getFocused(getFull?: boolean): string | number;
}
export class ContextMenuInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
public targetId: Snowflake;
public targetType: Exclude<ApplicationCommandType, 'CHAT_INPUT'>;
@@ -1095,6 +1160,7 @@ export class Interaction<Cached extends CacheType = CacheType> extends Base {
public isApplicationCommand(): this is BaseCommandInteraction<Cached>;
public isButton(): this is ButtonInteraction<Cached>;
public isCommand(): this is CommandInteraction<Cached>;
public isAutocomplete(): this is AutocompleteInteraction;
public isContextMenu(): this is ContextMenuInteraction<Cached>;
public isMessageComponent(): this is MessageComponentInteraction<Cached>;
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
@@ -3187,6 +3253,7 @@ export interface BaseApplicationCommandOptionsData {
name: string;
description: string;
required?: boolean;
autocomplete?: boolean;
}
export interface UserApplicationCommandData extends BaseApplicationCommandData {
@@ -3643,6 +3710,8 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
name: string;
type: ApplicationCommandOptionType;
value?: string | number | boolean;
focused?: boolean;
autocomplete?: boolean;
options?: CommandInteractionOption[];
user?: User;
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;

View File

@@ -19,7 +19,9 @@ import {
ApplicationCommandResolvable,
ApplicationCommandSubCommandData,
ApplicationCommandSubGroupData,
BaseCommandInteraction,
ButtonInteraction,
CacheType,
CategoryChannel,
Client,
ClientApplication,
@@ -922,7 +924,7 @@ client.on('interactionCreate', async interaction => {
if (interaction.inCachedGuild()) {
assertType<ContextMenuInteraction>(interaction);
assertType<Guild>(interaction.guild);
assertType<CommandInteraction<'cached'>>(interaction);
assertType<BaseCommandInteraction<'cached'>>(interaction);
} else if (interaction.inRawGuild()) {
assertType<ContextMenuInteraction>(interaction);
assertType<null>(interaction.guild);
@@ -994,7 +996,6 @@ client.on('interactionCreate', async interaction => {
assertType<APIInteractionDataResolvedChannel>(interaction.options.getChannel('test', true));
assertType<APIRole>(interaction.options.getRole('test', true));
assertType<APIMessage>(interaction.options.getMessage('test', true));
} else if (interaction.inCachedGuild()) {
const msg = await interaction.reply({ fetchReply: true });
const btn = await msg.awaitMessageComponent({ componentType: 'BUTTON' });
@@ -1010,7 +1011,6 @@ client.on('interactionCreate', async interaction => {
assertType<GuildChannel | ThreadChannel>(interaction.options.getChannel('test', true));
assertType<Role>(interaction.options.getRole('test', true));
assertType<Message>(interaction.options.getMessage('test', true));
} else {
// @ts-expect-error
consumeCachedCommand(interaction);
@@ -1023,11 +1023,10 @@ client.on('interactionCreate', async interaction => {
interaction.options.getChannel('test', true),
);
assertType<APIRole | Role>(interaction.options.getRole('test', true));
assertType<APIMessage | Message>(interaction.options.getMessage('test', true));
}
assertType<CommandInteraction>(interaction);
assertType<CommandInteractionOptionResolver>(interaction.options);
assertType<Omit<CommandInteractionOptionResolver<CacheType>, 'getFocused' | 'getMessage'>>(interaction.options);
assertType<readonly CommandInteractionOption[]>(interaction.options.data);
const optionalOption = interaction.options.get('name');