mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
feat: add support for autocomplete interactions (#6672)
Co-authored-by: Suneet Tipirneni <suneettipirneni@icloud.com>
This commit is contained in:
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -60,7 +60,7 @@ body:
|
|||||||
label: Node.js version
|
label: Node.js version
|
||||||
description: |
|
description: |
|
||||||
Which version of Node.js are you using? Run `node --version` in your project directory and paste the output.
|
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+
|
placeholder: Node.js version 16.6+ is required for version 13.0.0+
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
1
package-lock.json
generated
1
package-lock.json
generated
@@ -5,7 +5,6 @@
|
|||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord.js",
|
|
||||||
"version": "13.3.0-dev",
|
"version": "13.3.0-dev",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
|
const AutocompleteInteraction = require('../../structures/AutocompleteInteraction');
|
||||||
const ButtonInteraction = require('../../structures/ButtonInteraction');
|
const ButtonInteraction = require('../../structures/ButtonInteraction');
|
||||||
const CommandInteraction = require('../../structures/CommandInteraction');
|
const CommandInteraction = require('../../structures/CommandInteraction');
|
||||||
const ContextMenuInteraction = require('../../structures/ContextMenuInteraction');
|
const ContextMenuInteraction = require('../../structures/ContextMenuInteraction');
|
||||||
@@ -51,6 +52,9 @@ class InteractionCreateAction extends Action {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE:
|
||||||
|
InteractionType = AutocompleteInteraction;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`);
|
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ const Messages = {
|
|||||||
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
|
`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: 'No subcommand specified for interaction.',
|
||||||
COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND_GROUP: 'No subcommand group 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',
|
INVITE_MISSING_SCOPES: 'At least one valid scope must be provided for the invite',
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ exports.Activity = require('./structures/Presence').Activity;
|
|||||||
exports.AnonymousGuild = require('./structures/AnonymousGuild');
|
exports.AnonymousGuild = require('./structures/AnonymousGuild');
|
||||||
exports.Application = require('./structures/interfaces/Application');
|
exports.Application = require('./structures/interfaces/Application');
|
||||||
exports.ApplicationCommand = require('./structures/ApplicationCommand');
|
exports.ApplicationCommand = require('./structures/ApplicationCommand');
|
||||||
|
exports.AutocompleteInteraction = require('./structures/AutocompleteInteraction');
|
||||||
exports.Base = require('./structures/Base');
|
exports.Base = require('./structures/Base');
|
||||||
exports.BaseCommandInteraction = require('./structures/BaseCommandInteraction');
|
exports.BaseCommandInteraction = require('./structures/BaseCommandInteraction');
|
||||||
exports.BaseGuild = require('./structures/BaseGuild');
|
exports.BaseGuild = require('./structures/BaseGuild');
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {ApplicationCommandOptionType|number} type The type of the option
|
* @property {ApplicationCommandOptionType|number} type The type of the option
|
||||||
* @property {string} name The name of the option
|
* @property {string} name The name of the option
|
||||||
* @property {string} description The description 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 {boolean} [required] Whether the option is required
|
||||||
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
|
* @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)
|
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
|
||||||
@@ -199,6 +200,7 @@ class ApplicationCommand extends Base {
|
|||||||
command.name !== this.name ||
|
command.name !== this.name ||
|
||||||
('description' in command && command.description !== this.description) ||
|
('description' in command && command.description !== this.description) ||
|
||||||
('version' in command && command.version !== this.version) ||
|
('version' in command && command.version !== this.version) ||
|
||||||
|
('autocomplete' in command && command.autocomplete !== this.autocomplete) ||
|
||||||
(commandType && commandType !== this.type) ||
|
(commandType && commandType !== this.type) ||
|
||||||
// Future proof for options being nullable
|
// Future proof for options being nullable
|
||||||
// TODO: remove ?? 0 on each when nullable
|
// TODO: remove ?? 0 on each when nullable
|
||||||
@@ -254,6 +256,7 @@ class ApplicationCommand extends Base {
|
|||||||
option.name !== existing.name ||
|
option.name !== existing.name ||
|
||||||
optionType !== existing.type ||
|
optionType !== existing.type ||
|
||||||
option.description !== existing.description ||
|
option.description !== existing.description ||
|
||||||
|
option.autocomplete !== existing.autocomplete ||
|
||||||
(option.required ?? (['SUB_COMMAND', 'SUB_COMMAND_GROUP'].includes(optionType) ? undefined : false)) !==
|
(option.required ?? (['SUB_COMMAND', 'SUB_COMMAND_GROUP'].includes(optionType) ? undefined : false)) !==
|
||||||
existing.required ||
|
existing.required ||
|
||||||
option.choices?.length !== existing.choices?.length ||
|
option.choices?.length !== existing.choices?.length ||
|
||||||
@@ -303,6 +306,7 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {string} name The name of the option
|
* @property {string} name The name of the option
|
||||||
* @property {string} description The description of the option
|
* @property {string} description The description of the option
|
||||||
* @property {boolean} [required] Whether the option is required
|
* @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 {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 {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
|
||||||
* @property {ChannelType[]} [channelTypes] When the option type is channel,
|
* @property {ChannelType[]} [channelTypes] When the option type is channel,
|
||||||
@@ -332,6 +336,7 @@ class ApplicationCommand extends Base {
|
|||||||
description: option.description,
|
description: option.description,
|
||||||
required:
|
required:
|
||||||
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
|
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
|
||||||
|
autocomplete: option.autocomplete,
|
||||||
choices: option.choices,
|
choices: option.choices,
|
||||||
options: option.options?.map(o => this.transformOption(o, received)),
|
options: option.options?.map(o => this.transformOption(o, received)),
|
||||||
[channelTypesKey]: 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) {
|
constructor(client, data) {
|
||||||
super(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
|
* The id of the channel this interaction was sent in
|
||||||
* @type {Snowflake}
|
* @type {Snowflake}
|
||||||
@@ -138,6 +131,7 @@ class BaseCommandInteraction extends Interaction {
|
|||||||
* @typedef {Object} CommandInteractionOption
|
* @typedef {Object} CommandInteractionOption
|
||||||
* @property {string} name The name of the option
|
* @property {string} name The name of the option
|
||||||
* @property {ApplicationCommandOptionType} type The type 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 {string|number|boolean} [value] The value of the option
|
||||||
* @property {CommandInteractionOption[]} [options] Additional options if this option is a
|
* @property {CommandInteractionOption[]} [options] Additional options if this option is a
|
||||||
* subcommand (group)
|
* subcommand (group)
|
||||||
|
|||||||
@@ -239,6 +239,18 @@ class CommandInteractionOptionResolver {
|
|||||||
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
|
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
|
||||||
return option?.message ?? null;
|
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;
|
module.exports = CommandInteractionOptionResolver;
|
||||||
|
|||||||
@@ -160,6 +160,14 @@ class Interaction extends Base {
|
|||||||
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId !== 'undefined';
|
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}.
|
* Indicates whether this interaction is a {@link MessageComponentInteraction}.
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
|
|||||||
@@ -971,7 +971,13 @@ exports.ApplicationCommandPermissionTypes = createEnum([null, 'ROLE', 'USER']);
|
|||||||
* @typedef {string} InteractionType
|
* @typedef {string} InteractionType
|
||||||
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type}
|
* @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:
|
* The type of an interaction response:
|
||||||
@@ -992,6 +998,7 @@ exports.InteractionResponseTypes = createEnum([
|
|||||||
'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
|
'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
|
||||||
'DEFERRED_MESSAGE_UPDATE',
|
'DEFERRED_MESSAGE_UPDATE',
|
||||||
'UPDATE_MESSAGE',
|
'UPDATE_MESSAGE',
|
||||||
|
'APPLICATION_COMMAND_AUTOCOMPLETE_RESULT',
|
||||||
]);
|
]);
|
||||||
/* eslint-enable max-len */
|
/* eslint-enable max-len */
|
||||||
|
|
||||||
|
|||||||
2
typings/enums.d.ts
vendored
2
typings/enums.d.ts
vendored
@@ -92,12 +92,14 @@ export const enum InteractionResponseTypes {
|
|||||||
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
|
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
|
||||||
DEFERRED_MESSAGE_UPDATE = 6,
|
DEFERRED_MESSAGE_UPDATE = 6,
|
||||||
UPDATE_MESSAGE = 7,
|
UPDATE_MESSAGE = 7,
|
||||||
|
APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const enum InteractionTypes {
|
export const enum InteractionTypes {
|
||||||
PING = 1,
|
PING = 1,
|
||||||
APPLICATION_COMMAND = 2,
|
APPLICATION_COMMAND = 2,
|
||||||
MESSAGE_COMPONENT = 3,
|
MESSAGE_COMPONENT = 3,
|
||||||
|
APPLICATION_COMMAND_AUTOCOMPLETE = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const enum InviteTargetType {
|
export const enum InviteTargetType {
|
||||||
|
|||||||
71
typings/index.d.ts
vendored
71
typings/index.d.ts
vendored
@@ -277,8 +277,20 @@ export type GuildCacheMessage<Cached extends CacheType> = CacheTypeReducer<
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
export abstract class BaseCommandInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
|
export abstract class BaseCommandInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
|
||||||
public options: CommandInteractionOptionResolver<Cached>;
|
|
||||||
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
||||||
|
public options: Omit<
|
||||||
|
CommandInteractionOptionResolver<Cached>,
|
||||||
|
| 'getFocused'
|
||||||
|
| 'getMentionable'
|
||||||
|
| 'getRole'
|
||||||
|
| 'getNumber'
|
||||||
|
| 'getInteger'
|
||||||
|
| 'getString'
|
||||||
|
| 'getChannel'
|
||||||
|
| 'getBoolean'
|
||||||
|
| 'getSubcommandGroup'
|
||||||
|
| 'getSubcommand'
|
||||||
|
>;
|
||||||
public channelId: Snowflake;
|
public channelId: Snowflake;
|
||||||
public commandId: Snowflake;
|
public commandId: Snowflake;
|
||||||
public commandName: string;
|
public commandName: string;
|
||||||
@@ -589,10 +601,60 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
|
|||||||
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
|
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandInteractionOptionResolver<Cached extends CacheType = CacheType>
|
||||||
|
extends BaseCommandInteractionOptionResolver<Cached> {
|
||||||
|
getSubcommand(required?: true): string;
|
||||||
|
getSubcommand(required: boolean): string | null;
|
||||||
|
getSubcommandGroup(required?: true): string;
|
||||||
|
getSubcommandGroup(required: boolean): string | null;
|
||||||
|
getBoolean(name: string, required: true): boolean;
|
||||||
|
getBoolean(name: string, required?: boolean): boolean | null;
|
||||||
|
getChannel(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['channel']>;
|
||||||
|
getChannel(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['channel']> | null;
|
||||||
|
getString(name: string, required: true): string;
|
||||||
|
getString(name: string, required?: boolean): string | null;
|
||||||
|
getInteger(name: string, required: true): number;
|
||||||
|
getInteger(name: string, required?: boolean): number | null;
|
||||||
|
getNumber(name: string, required: true): number;
|
||||||
|
getNumber(name: string, required?: boolean): number | null;
|
||||||
|
getUser(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['user']>;
|
||||||
|
getUser(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['user']> | null;
|
||||||
|
getMember(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['member']>;
|
||||||
|
getMember(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['member']> | null;
|
||||||
|
getRole(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['role']>;
|
||||||
|
getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['role']> | null;
|
||||||
|
getMentionable(
|
||||||
|
name: string,
|
||||||
|
required: true,
|
||||||
|
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']>;
|
||||||
|
getMentionable(
|
||||||
|
name: string,
|
||||||
|
required?: boolean,
|
||||||
|
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
|
||||||
|
}
|
||||||
|
|
||||||
export class CommandInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
|
export class CommandInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
|
||||||
|
public options: Omit<CommandInteractionOptionResolver<Cached>, 'getMessage' | 'getFocused'>;
|
||||||
|
public inGuild(): this is CommandInteraction<'present'> & this;
|
||||||
|
public inCachedGuild(): this is CommandInteraction<'cached'> & this;
|
||||||
|
public inRawGuild(): this is CommandInteraction<'raw'> & this;
|
||||||
public toString(): string;
|
public toString(): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class AutocompleteInteraction<Cached extends CacheType = CacheType> extends Interaction<Cached> {
|
||||||
|
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
||||||
|
public channelId: Snowflake;
|
||||||
|
public commandId: Snowflake;
|
||||||
|
public commandName: string;
|
||||||
|
public responded: boolean;
|
||||||
|
public options: Omit<CommandInteractionOptionResolver<Cached>, 'getMessage'>;
|
||||||
|
public inGuild(): this is CommandInteraction<'present'> & this;
|
||||||
|
public inCachedGuild(): this is CommandInteraction<'cached'> & this;
|
||||||
|
public inRawGuild(): this is CommandInteraction<'raw'> & this;
|
||||||
|
private transformOption(option: APIApplicationCommandOption): CommandInteractionOption;
|
||||||
|
public respond(options: ApplicationCommandOptionChoice[]): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
export class CommandInteractionOptionResolver<Cached extends CacheType = CacheType> {
|
export class CommandInteractionOptionResolver<Cached extends CacheType = CacheType> {
|
||||||
private constructor(client: Client, options: CommandInteractionOption[], resolved: CommandInteractionResolvedData);
|
private constructor(client: Client, options: CommandInteractionOption[], resolved: CommandInteractionResolvedData);
|
||||||
public readonly client: Client;
|
public readonly client: Client;
|
||||||
@@ -647,7 +709,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
|
|||||||
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
|
): NonNullable<CommandInteractionOption<Cached>['member' | 'role' | 'user']> | null;
|
||||||
public getMessage(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['message']>;
|
public getMessage(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['message']>;
|
||||||
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
|
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
|
||||||
|
public getFocused(getFull: true): ApplicationCommandOptionChoice;
|
||||||
|
public getFocused(getFull?: boolean): string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ContextMenuInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
|
export class ContextMenuInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
|
||||||
public targetId: Snowflake;
|
public targetId: Snowflake;
|
||||||
public targetType: Exclude<ApplicationCommandType, 'CHAT_INPUT'>;
|
public targetType: Exclude<ApplicationCommandType, 'CHAT_INPUT'>;
|
||||||
@@ -1095,6 +1160,7 @@ export class Interaction<Cached extends CacheType = CacheType> extends Base {
|
|||||||
public isApplicationCommand(): this is BaseCommandInteraction<Cached>;
|
public isApplicationCommand(): this is BaseCommandInteraction<Cached>;
|
||||||
public isButton(): this is ButtonInteraction<Cached>;
|
public isButton(): this is ButtonInteraction<Cached>;
|
||||||
public isCommand(): this is CommandInteraction<Cached>;
|
public isCommand(): this is CommandInteraction<Cached>;
|
||||||
|
public isAutocomplete(): this is AutocompleteInteraction;
|
||||||
public isContextMenu(): this is ContextMenuInteraction<Cached>;
|
public isContextMenu(): this is ContextMenuInteraction<Cached>;
|
||||||
public isMessageComponent(): this is MessageComponentInteraction<Cached>;
|
public isMessageComponent(): this is MessageComponentInteraction<Cached>;
|
||||||
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
|
public isSelectMenu(): this is SelectMenuInteraction<Cached>;
|
||||||
@@ -3187,6 +3253,7 @@ export interface BaseApplicationCommandOptionsData {
|
|||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
autocomplete?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserApplicationCommandData extends BaseApplicationCommandData {
|
export interface UserApplicationCommandData extends BaseApplicationCommandData {
|
||||||
@@ -3643,6 +3710,8 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
|
|||||||
name: string;
|
name: string;
|
||||||
type: ApplicationCommandOptionType;
|
type: ApplicationCommandOptionType;
|
||||||
value?: string | number | boolean;
|
value?: string | number | boolean;
|
||||||
|
focused?: boolean;
|
||||||
|
autocomplete?: boolean;
|
||||||
options?: CommandInteractionOption[];
|
options?: CommandInteractionOption[];
|
||||||
user?: User;
|
user?: User;
|
||||||
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
|
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ import {
|
|||||||
ApplicationCommandResolvable,
|
ApplicationCommandResolvable,
|
||||||
ApplicationCommandSubCommandData,
|
ApplicationCommandSubCommandData,
|
||||||
ApplicationCommandSubGroupData,
|
ApplicationCommandSubGroupData,
|
||||||
|
BaseCommandInteraction,
|
||||||
ButtonInteraction,
|
ButtonInteraction,
|
||||||
|
CacheType,
|
||||||
CategoryChannel,
|
CategoryChannel,
|
||||||
Client,
|
Client,
|
||||||
ClientApplication,
|
ClientApplication,
|
||||||
@@ -922,7 +924,7 @@ client.on('interactionCreate', async interaction => {
|
|||||||
if (interaction.inCachedGuild()) {
|
if (interaction.inCachedGuild()) {
|
||||||
assertType<ContextMenuInteraction>(interaction);
|
assertType<ContextMenuInteraction>(interaction);
|
||||||
assertType<Guild>(interaction.guild);
|
assertType<Guild>(interaction.guild);
|
||||||
assertType<CommandInteraction<'cached'>>(interaction);
|
assertType<BaseCommandInteraction<'cached'>>(interaction);
|
||||||
} else if (interaction.inRawGuild()) {
|
} else if (interaction.inRawGuild()) {
|
||||||
assertType<ContextMenuInteraction>(interaction);
|
assertType<ContextMenuInteraction>(interaction);
|
||||||
assertType<null>(interaction.guild);
|
assertType<null>(interaction.guild);
|
||||||
@@ -994,7 +996,6 @@ client.on('interactionCreate', async interaction => {
|
|||||||
|
|
||||||
assertType<APIInteractionDataResolvedChannel>(interaction.options.getChannel('test', true));
|
assertType<APIInteractionDataResolvedChannel>(interaction.options.getChannel('test', true));
|
||||||
assertType<APIRole>(interaction.options.getRole('test', true));
|
assertType<APIRole>(interaction.options.getRole('test', true));
|
||||||
assertType<APIMessage>(interaction.options.getMessage('test', true));
|
|
||||||
} else if (interaction.inCachedGuild()) {
|
} else if (interaction.inCachedGuild()) {
|
||||||
const msg = await interaction.reply({ fetchReply: true });
|
const msg = await interaction.reply({ fetchReply: true });
|
||||||
const btn = await msg.awaitMessageComponent({ componentType: 'BUTTON' });
|
const btn = await msg.awaitMessageComponent({ componentType: 'BUTTON' });
|
||||||
@@ -1010,7 +1011,6 @@ client.on('interactionCreate', async interaction => {
|
|||||||
|
|
||||||
assertType<GuildChannel | ThreadChannel>(interaction.options.getChannel('test', true));
|
assertType<GuildChannel | ThreadChannel>(interaction.options.getChannel('test', true));
|
||||||
assertType<Role>(interaction.options.getRole('test', true));
|
assertType<Role>(interaction.options.getRole('test', true));
|
||||||
assertType<Message>(interaction.options.getMessage('test', true));
|
|
||||||
} else {
|
} else {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
consumeCachedCommand(interaction);
|
consumeCachedCommand(interaction);
|
||||||
@@ -1023,11 +1023,10 @@ client.on('interactionCreate', async interaction => {
|
|||||||
interaction.options.getChannel('test', true),
|
interaction.options.getChannel('test', true),
|
||||||
);
|
);
|
||||||
assertType<APIRole | Role>(interaction.options.getRole('test', true));
|
assertType<APIRole | Role>(interaction.options.getRole('test', true));
|
||||||
assertType<APIMessage | Message>(interaction.options.getMessage('test', true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertType<CommandInteraction>(interaction);
|
assertType<CommandInteraction>(interaction);
|
||||||
assertType<CommandInteractionOptionResolver>(interaction.options);
|
assertType<Omit<CommandInteractionOptionResolver<CacheType>, 'getFocused' | 'getMessage'>>(interaction.options);
|
||||||
assertType<readonly CommandInteractionOption[]>(interaction.options.data);
|
assertType<readonly CommandInteractionOption[]>(interaction.options.data);
|
||||||
|
|
||||||
const optionalOption = interaction.options.get('name');
|
const optionalOption = interaction.options.get('name');
|
||||||
|
|||||||
Reference in New Issue
Block a user