93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const { InteractionResponseType, Routes } = require('discord-api-types/v9');
|
|
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
|
|
const Interaction = require('./Interaction');
|
|
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* The invoked application command's type
|
|
* @type {ApplicationCommandType.ChatInput}
|
|
*/
|
|
this.commandType = data.data.type;
|
|
|
|
/**
|
|
* 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 ?? []);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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({
|
|
body: {
|
|
type: InteractionResponseType.ApplicationCommandAutocompleteResult,
|
|
data: {
|
|
choices: options,
|
|
},
|
|
},
|
|
auth: false,
|
|
})
|
|
this.responded = true;
|
|
}
|
|
}
|
|
|
|
module.exports = AutocompleteInteraction;
|