summaryrefslogtreecommitdiff
path: root/node_modules/discord.js/src/structures/AutocompleteInteraction.js
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2023-09-02 19:12:47 -0400
committersowgro <tpoke.ferrari@gmail.com>2023-09-02 19:12:47 -0400
commite4450c8417624b71d779cb4f41692538f9165e10 (patch)
treeb70826542223ecdf8a7a259f61b0a1abb8a217d8 /node_modules/discord.js/src/structures/AutocompleteInteraction.js
downloadsowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.gz
sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.bz2
sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.zip
first commit
Diffstat (limited to 'node_modules/discord.js/src/structures/AutocompleteInteraction.js')
-rw-r--r--node_modules/discord.js/src/structures/AutocompleteInteraction.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/structures/AutocompleteInteraction.js b/node_modules/discord.js/src/structures/AutocompleteInteraction.js
new file mode 100644
index 0000000..4b7e39e
--- /dev/null
+++ b/node_modules/discord.js/src/structures/AutocompleteInteraction.js
@@ -0,0 +1,102 @@
+'use strict';
+
+const { InteractionResponseType, Routes } = require('discord-api-types/v10');
+const BaseInteraction = require('./BaseInteraction');
+const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
+const { DiscordjsError, ErrorCodes } = require('../errors');
+
+/**
+ * Represents an autocomplete interaction.
+ * @extends {BaseInteraction}
+ */
+class AutocompleteInteraction extends BaseInteraction {
+ 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}
+ */
+ this.commandType = data.data.type;
+
+ /**
+ * The id of the guild the invoked application command is registered to
+ * @type {?Snowflake}
+ */
+ this.commandGuildId = data.data.guild_id ?? null;
+
+ /**
+ * 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 {ApplicationCommandOptionChoiceData[]} options The options for the autocomplete
+ * @returns {Promise<void>}
+ * @example
+ * // respond to autocomplete interaction
+ * interaction.respond([
+ * {
+ * name: 'Option 1',
+ * value: 'option1',
+ * },
+ * ])
+ * .then(() => console.log('Successfully responded to the autocomplete interaction'))
+ * .catch(console.error);
+ */
+ async respond(options) {
+ if (this.responded) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
+
+ await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
+ body: {
+ type: InteractionResponseType.ApplicationCommandAutocompleteResult,
+ data: {
+ choices: options.map(({ nameLocalizations, ...option }) => ({
+ ...this.client.options.jsonTransformer(option),
+ name_localizations: nameLocalizations,
+ })),
+ },
+ },
+ auth: false,
+ });
+ this.responded = true;
+ }
+}
+
+module.exports = AutocompleteInteraction;