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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 224 additions and 16 deletions

View file

@ -60,7 +60,7 @@ body:
label: Node.js version
description: |
Which version of Node.js are you using? Run `node --version` in your project directory and paste the output.
If you are using TypeScript, please include its version (`npm list typescript`) as well.
If you are using TypeScript, please include its version (`npm list typescript`) as well.
placeholder: Node.js version 16.6+ is required for version 13.0.0+
validations:
required: true

1
package-lock.json generated
View file

@ -5,7 +5,6 @@
"requires": true,
"packages": {
"": {
"name": "discord.js",
"version": "13.3.0-dev",
"license": "Apache-2.0",
"dependencies": {

View file

@ -1,6 +1,7 @@
'use strict';
const Action = require('./Action');
const AutocompleteInteraction = require('../../structures/AutocompleteInteraction');
const ButtonInteraction = require('../../structures/ButtonInteraction');
const CommandInteraction = require('../../structures/CommandInteraction');
const ContextMenuInteraction = require('../../structures/ContextMenuInteraction');
@ -51,6 +52,9 @@ class InteractionCreateAction extends Action {
return;
}
break;
case InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE:
InteractionType = AutocompleteInteraction;
break;
default:
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`);
return;

View file

@ -143,6 +143,7 @@ const Messages = {
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND: 'No subcommand specified for interaction.',
COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND_GROUP: 'No subcommand group specified for interaction.',
AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION: 'No focused option for autocomplete interaction.',
INVITE_MISSING_SCOPES: 'At least one valid scope must be provided for the invite',

View file

@ -69,6 +69,7 @@ exports.Activity = require('./structures/Presence').Activity;
exports.AnonymousGuild = require('./structures/AnonymousGuild');
exports.Application = require('./structures/interfaces/Application');
exports.ApplicationCommand = require('./structures/ApplicationCommand');
exports.AutocompleteInteraction = require('./structures/AutocompleteInteraction');
exports.Base = require('./structures/Base');
exports.BaseCommandInteraction = require('./structures/BaseCommandInteraction');
exports.BaseGuild = require('./structures/BaseGuild');

View file

@ -140,6 +140,7 @@ class ApplicationCommand extends Base {
* @property {ApplicationCommandOptionType|number} type The type of the option
* @property {string} name The name of the option
* @property {string} description The description of the option
* @property {boolean} [autocomplete] Whether the option is an autocomplete option
* @property {boolean} [required] Whether the option is required
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
@ -199,6 +200,7 @@ class ApplicationCommand extends Base {
command.name !== this.name ||
('description' in command && command.description !== this.description) ||
('version' in command && command.version !== this.version) ||
('autocomplete' in command && command.autocomplete !== this.autocomplete) ||
(commandType && commandType !== this.type) ||
// Future proof for options being nullable
// TODO: remove ?? 0 on each when nullable
@ -254,6 +256,7 @@ class ApplicationCommand extends Base {
option.name !== existing.name ||
optionType !== existing.type ||
option.description !== existing.description ||
option.autocomplete !== existing.autocomplete ||
(option.required ?? (['SUB_COMMAND', 'SUB_COMMAND_GROUP'].includes(optionType) ? undefined : false)) !==
existing.required ||
option.choices?.length !== existing.choices?.length ||
@ -303,6 +306,7 @@ class ApplicationCommand extends Base {
* @property {string} name The name of the option
* @property {string} description The description of the option
* @property {boolean} [required] Whether the option is required
* @property {boolean} [autocomplete] Whether the option is an autocomplete option
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
* @property {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
* @property {ChannelType[]} [channelTypes] When the option type is channel,
@ -332,6 +336,7 @@ class ApplicationCommand extends Base {
description: option.description,
required:
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
autocomplete: option.autocomplete,
choices: option.choices,
options: option.options?.map(o => this.transformOption(o, received)),
[channelTypesKey]: received

View file

@ -0,0 +1,107 @@
'use strict';
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
const Interaction = require('./Interaction');
const { InteractionResponseTypes, ApplicationCommandOptionTypes } = require('../util/Constants');
/**
* Represents an autocomplete interaction.
* @extends {Interaction}
*/
class AutocompleteInteraction extends Interaction {
constructor(client, data) {
super(client, data);
/**
* The id of the channel this interaction was sent in
* @type {Snowflake}
* @name AutocompleteInteraction#channelId
*/
/**
* The invoked application command's id
* @type {Snowflake}
*/
this.commandId = data.data.id;
/**
* The invoked application command's name
* @type {string}
*/
this.commandName = data.data.name;
/**
* Whether this interaction has already received a response
* @type {boolean}
*/
this.responded = false;
/**
* The options passed to the command
* @type {CommandInteractionOptionResolver}
*/
this.options = new CommandInteractionOptionResolver(
this.client,
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
);
}
/**
* The invoked application command, if it was fetched before
* @type {?ApplicationCommand}
*/
get command() {
const id = this.commandId;
return this.guild?.commands.cache.get(id) ?? this.client.application.commands.cache.get(id) ?? null;
}
/**
* Transforms an option received from the API.
* @param {APIApplicationCommandOption} option The received option
* @returns {CommandInteractionOption}
* @private
*/
transformOption(option) {
const result = {
name: option.name,
type: ApplicationCommandOptionTypes[option.type],
};
if ('value' in option) result.value = option.value;
if ('options' in option) result.options = option.options.map(opt => this.transformOption(opt));
if ('focused' in option) result.focused = option.focused;
return result;
}
/**
* Sends results for the autocomplete of this interaction.
* @param {ApplicationCommandOptionChoice[]} options The options for the autocomplete
* @returns {Promise<void>}
* @example
* // respond to autocomplete interaction
* interaction.respond([
* {
* name: 'Option 1',
* value: 'option1',
* },
* ])
* .then(console.log)
* .catch(console.error);
*/
async respond(options) {
if (this.responded) throw new Error('INTERACTION_ALREADY_REPLIED');
await this.client.api.interactions(this.id, this.token).callback.post({
data: {
type: InteractionResponseTypes.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT,
data: {
choices: options,
},
},
});
this.responded = true;
}
}
module.exports = AutocompleteInteraction;

View file

@ -16,13 +16,6 @@ class BaseCommandInteraction extends Interaction {
constructor(client, data) {
super(client, data);
/**
* The channel this interaction was sent in
* @type {?TextBasedChannels}
* @name BaseCommandInteraction#channel
* @readonly
*/
/**
* The id of the channel this interaction was sent in
* @type {Snowflake}
@ -138,6 +131,7 @@ class BaseCommandInteraction extends Interaction {
* @typedef {Object} CommandInteractionOption
* @property {string} name The name of the option
* @property {ApplicationCommandOptionType} type The type of the option
* @property {boolean} [autocomplete] Whether the option is an autocomplete option
* @property {string|number|boolean} [value] The value of the option
* @property {CommandInteractionOption[]} [options] Additional options if this option is a
* subcommand (group)

View file

@ -239,6 +239,18 @@ class CommandInteractionOptionResolver {
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
return option?.message ?? null;
}
/**
* Gets the focused option.
* @param {boolean} [getFull=false] Whether to get the full option object
* @returns {string|number|ApplicationCommandOptionChoice}
* The value of the option, or the whole option if getFull is true
*/
getFocused(getFull = false) {
const focusedOption = this._hoistedOptions.find(option => option.focused);
if (!focusedOption) throw new TypeError('AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION');
return getFull ? focusedOption : focusedOption.value;
}
}
module.exports = CommandInteractionOptionResolver;

View file

@ -160,6 +160,14 @@ class Interaction extends Base {
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId !== 'undefined';
}
/**
* Indicates whether this interaction is an {@link AutocompleteInteraction}
* @returns {boolean}
*/
isAutocomplete() {
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE;
}
/**
* Indicates whether this interaction is a {@link MessageComponentInteraction}.
* @returns {boolean}

View file

@ -971,7 +971,13 @@ exports.ApplicationCommandPermissionTypes = createEnum([null, 'ROLE', 'USER']);
* @typedef {string} InteractionType
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type}
*/
exports.InteractionTypes = createEnum([null, 'PING', 'APPLICATION_COMMAND', 'MESSAGE_COMPONENT']);
exports.InteractionTypes = createEnum([
null,
'PING',
'APPLICATION_COMMAND',
'MESSAGE_COMPONENT',
'APPLICATION_COMMAND_AUTOCOMPLETE',
]);
/**
* The type of an interaction response:
@ -992,6 +998,7 @@ exports.InteractionResponseTypes = createEnum([
'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
'DEFERRED_MESSAGE_UPDATE',
'UPDATE_MESSAGE',
'APPLICATION_COMMAND_AUTOCOMPLETE_RESULT',
]);
/* eslint-enable max-len */

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');