mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
feat: add support for autocomplete interactions (#6672)
Co-authored-by: Suneet Tipirneni <suneettipirneni@icloud.com>
This commit is contained in:
@@ -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
|
||||
|
||||
107
src/structures/AutocompleteInteraction.js
Normal file
107
src/structures/AutocompleteInteraction.js
Normal 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;
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user