Skip to content

Commit

Permalink
feat: add support for autocomplete interactions (#6672)
Browse files Browse the repository at this point in the history
Co-authored-by: Suneet Tipirneni <suneettipirneni@icloud.com>
  • Loading branch information
OfficialSirH and suneettipirneni committed Oct 28, 2021
1 parent 14d9a99 commit ddf759c
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/client/actions/InteractionCreate.js
@@ -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');
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/errors/Messages.js
Expand Up @@ -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',

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions src/structures/ApplicationCommand.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions src/structures/AutocompleteInteraction.js
@@ -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;
8 changes: 1 addition & 7 deletions src/structures/BaseCommandInteraction.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/structures/CommandInteractionOptionResolver.js
Expand Up @@ -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;
8 changes: 8 additions & 0 deletions src/structures/Interaction.js
Expand Up @@ -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}
Expand Down
9 changes: 8 additions & 1 deletion src/util/Constants.js
Expand Up @@ -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:
Expand All @@ -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 */

Expand Down
2 changes: 2 additions & 0 deletions typings/enums.d.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit ddf759c

Please sign in to comment.