/
AutocompleteInteraction.js
99 lines (86 loc) · 2.65 KB
/
AutocompleteInteraction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const { InteractionResponseType, Routes } = require('discord-api-types/v10');
const BaseInteraction = require('./BaseInteraction');
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
const { Error, 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 Error(ErrorCodes.InteractionAlreadyReplied);
await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.ApplicationCommandAutocompleteResult,
data: {
choices: options,
},
},
auth: false,
});
this.responded = true;
}
}
module.exports = AutocompleteInteraction;