mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
feat: right-clickybois (context menu support for ApplicationCommand and CommandInteraction) (#6176)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
@@ -3,8 +3,9 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
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 SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
|
const SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
|
||||||
const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants');
|
const { Events, InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../../util/Constants');
|
||||||
|
|
||||||
let deprecationEmitted = false;
|
let deprecationEmitted = false;
|
||||||
|
|
||||||
@@ -18,7 +19,21 @@ class InteractionCreateAction extends Action {
|
|||||||
let InteractionType;
|
let InteractionType;
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case InteractionTypes.APPLICATION_COMMAND:
|
case InteractionTypes.APPLICATION_COMMAND:
|
||||||
InteractionType = CommandInteraction;
|
switch (data.data.type) {
|
||||||
|
case ApplicationCommandTypes.CHAT_INPUT:
|
||||||
|
InteractionType = CommandInteraction;
|
||||||
|
break;
|
||||||
|
case ApplicationCommandTypes.USER:
|
||||||
|
case ApplicationCommandTypes.MESSAGE:
|
||||||
|
InteractionType = ContextMenuInteraction;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
client.emit(
|
||||||
|
Events.DEBUG,
|
||||||
|
`[INTERACTION] Received application command interaction with unknown type: ${data.data.type}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case InteractionTypes.MESSAGE_COMPONENT:
|
case InteractionTypes.MESSAGE_COMPONENT:
|
||||||
switch (data.data.component_type) {
|
switch (data.data.component_type) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermis
|
|||||||
const CachedManager = require('./CachedManager');
|
const CachedManager = require('./CachedManager');
|
||||||
const { TypeError } = require('../errors');
|
const { TypeError } = require('../errors');
|
||||||
const ApplicationCommand = require('../structures/ApplicationCommand');
|
const ApplicationCommand = require('../structures/ApplicationCommand');
|
||||||
|
const { ApplicationCommandTypes } = require('../util/Constants');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages API methods for application commands and stores their cache.
|
* Manages API methods for application commands and stores their cache.
|
||||||
@@ -207,6 +208,7 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
return {
|
return {
|
||||||
name: command.name,
|
name: command.name,
|
||||||
description: command.description,
|
description: command.description,
|
||||||
|
type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type],
|
||||||
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
|
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
|
||||||
default_permission: command.defaultPermission,
|
default_permission: command.defaultPermission,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const Base = require('./Base');
|
const Base = require('./Base');
|
||||||
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
||||||
const { ApplicationCommandOptionTypes } = require('../util/Constants');
|
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
|
||||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,6 +44,12 @@ class ApplicationCommand extends Base {
|
|||||||
*/
|
*/
|
||||||
this.permissions = new ApplicationCommandPermissionsManager(this);
|
this.permissions = new ApplicationCommandPermissionsManager(this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of this application command
|
||||||
|
* @type {ApplicationCommandType}
|
||||||
|
*/
|
||||||
|
this.type = ApplicationCommandTypes[data.type];
|
||||||
|
|
||||||
this._patch(data);
|
this._patch(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +111,7 @@ class ApplicationCommand extends Base {
|
|||||||
* @typedef {Object} ApplicationCommandData
|
* @typedef {Object} ApplicationCommandData
|
||||||
* @property {string} name The name of the command
|
* @property {string} name The name of the command
|
||||||
* @property {string} description The description of the command
|
* @property {string} description The description of the command
|
||||||
|
* @property {ApplicationCommandTypes} [type] The type of the command
|
||||||
* @property {ApplicationCommandOptionData[]} [options] Options for the command
|
* @property {ApplicationCommandOptionData[]} [options] Options for the command
|
||||||
* @property {boolean} [defaultPermission] Whether the command is enabled by default when the app is added to a guild
|
* @property {boolean} [defaultPermission] Whether the command is enabled by default when the app is added to a guild
|
||||||
*/
|
*/
|
||||||
|
|||||||
142
src/structures/BaseCommandInteraction.js
Normal file
142
src/structures/BaseCommandInteraction.js
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Interaction = require('./Interaction');
|
||||||
|
const InteractionWebhook = require('./InteractionWebhook');
|
||||||
|
const InteractionResponses = require('./interfaces/InteractionResponses');
|
||||||
|
const { ApplicationCommandOptionTypes } = require('../util/Constants');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a command interaction.
|
||||||
|
* @extends {Interaction}
|
||||||
|
* @implements {InteractionResponses}
|
||||||
|
* @abstract
|
||||||
|
*/
|
||||||
|
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}
|
||||||
|
* @name BaseCommandInteraction#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 the reply to this interaction has been deferred
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
this.deferred = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this interaction has already been replied to
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
this.replied = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the reply to this interaction is ephemeral
|
||||||
|
* @type {?boolean}
|
||||||
|
*/
|
||||||
|
this.ephemeral = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An associated interaction webhook, can be used to further interact with this interaction
|
||||||
|
* @type {InteractionWebhook}
|
||||||
|
*/
|
||||||
|
this.webhook = new InteractionWebhook(this.client, this.applicationId, this.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an option of a received command interaction.
|
||||||
|
* @typedef {Object} CommandInteractionOption
|
||||||
|
* @property {string} name The name of the option
|
||||||
|
* @property {ApplicationCommandOptionType} type The type of the option
|
||||||
|
* @property {string|number|boolean} [value] The value of the option
|
||||||
|
* @property {CommandInteractionOption[]} [options] Additional options if this option is a
|
||||||
|
* subcommand (group)
|
||||||
|
* @property {User} [user] The resolved user
|
||||||
|
* @property {GuildMember|APIGuildMember} [member] The resolved member
|
||||||
|
* @property {GuildChannel|APIChannel} [channel] The resolved channel
|
||||||
|
* @property {Role|APIRole} [role] The resolved role
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms an option received from the API.
|
||||||
|
* @param {APIApplicationCommandOption} option The received option
|
||||||
|
* @param {APIInteractionDataResolved} resolved The resolved interaction data
|
||||||
|
* @returns {CommandInteractionOption}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
transformOption(option, resolved) {
|
||||||
|
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, resolved));
|
||||||
|
|
||||||
|
if (resolved) {
|
||||||
|
const user = resolved.users?.[option.value];
|
||||||
|
if (user) result.user = this.client.users._add(user);
|
||||||
|
|
||||||
|
const member = resolved.members?.[option.value];
|
||||||
|
if (member) result.member = this.guild?.members._add({ user, ...member }) ?? member;
|
||||||
|
|
||||||
|
const channel = resolved.channels?.[option.value];
|
||||||
|
if (channel) result.channel = this.client.channels._add(channel, this.guild) ?? channel;
|
||||||
|
|
||||||
|
const role = resolved.roles?.[option.value];
|
||||||
|
if (role) result.role = this.guild?.roles._add(role) ?? role;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// These are here only for documentation purposes - they are implemented by InteractionResponses
|
||||||
|
/* eslint-disable no-empty-function */
|
||||||
|
defer() {}
|
||||||
|
reply() {}
|
||||||
|
fetchReply() {}
|
||||||
|
editReply() {}
|
||||||
|
deleteReply() {}
|
||||||
|
followUp() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
InteractionResponses.applyToClass(BaseCommandInteraction, ['deferUpdate', 'update']);
|
||||||
|
|
||||||
|
module.exports = BaseCommandInteraction;
|
||||||
|
|
||||||
|
/* eslint-disable max-len */
|
||||||
|
/**
|
||||||
|
* @external APIInteractionDataResolved
|
||||||
|
* @see {@link https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-resolved-data-structure}
|
||||||
|
*/
|
||||||
@@ -1,51 +1,16 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const BaseCommandInteraction = require('./BaseCommandInteraction');
|
||||||
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
|
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
|
||||||
const Interaction = require('./Interaction');
|
|
||||||
const InteractionWebhook = require('./InteractionWebhook');
|
|
||||||
const InteractionResponses = require('./interfaces/InteractionResponses');
|
|
||||||
const { ApplicationCommandOptionTypes } = require('../util/Constants');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a command interaction.
|
* Represents a command interaction.
|
||||||
* @extends {Interaction}
|
* @extends {BaseCommandInteraction}
|
||||||
* @implements {InteractionResponses}
|
|
||||||
*/
|
*/
|
||||||
class CommandInteraction extends Interaction {
|
class CommandInteraction extends BaseCommandInteraction {
|
||||||
constructor(client, data) {
|
constructor(client, data) {
|
||||||
super(client, data);
|
super(client, data);
|
||||||
|
|
||||||
/**
|
|
||||||
* The channel this interaction was sent in
|
|
||||||
* @type {?TextBasedChannels}
|
|
||||||
* @name CommandInteraction#channel
|
|
||||||
* @readonly
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The id of the channel this interaction was sent in
|
|
||||||
* @type {Snowflake}
|
|
||||||
* @name CommandInteraction#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 the reply to this interaction has been deferred
|
|
||||||
* @type {boolean}
|
|
||||||
*/
|
|
||||||
this.deferred = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The options passed to the command.
|
* The options passed to the command.
|
||||||
* @type {CommandInteractionOptionResolver}
|
* @type {CommandInteractionOptionResolver}
|
||||||
@@ -54,105 +19,7 @@ class CommandInteraction extends Interaction {
|
|||||||
this.client,
|
this.client,
|
||||||
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
|
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this interaction has already been replied to
|
|
||||||
* @type {boolean}
|
|
||||||
*/
|
|
||||||
this.replied = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether the reply to this interaction is ephemeral
|
|
||||||
* @type {?boolean}
|
|
||||||
*/
|
|
||||||
this.ephemeral = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An associated interaction webhook, can be used to further interact with this interaction
|
|
||||||
* @type {InteractionWebhook}
|
|
||||||
*/
|
|
||||||
this.webhook = new InteractionWebhook(this.client, this.applicationId, this.token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an option of a received command interaction.
|
|
||||||
* @typedef {Object} CommandInteractionOption
|
|
||||||
* @property {string} name The name of the option
|
|
||||||
* @property {ApplicationCommandOptionType} type The type of the option
|
|
||||||
* @property {string|number|boolean} [value] The value of the option
|
|
||||||
* @property {CommandInteractionOption[]} [options] Additional options if this option is a
|
|
||||||
* subcommand (group)
|
|
||||||
* @property {User} [user] The resolved user
|
|
||||||
* @property {GuildMember|APIInteractionDataResolvedOption} [member] The resolved member
|
|
||||||
* @property {GuildChannel|APIInteractionDataResolvedOption} [channel] The resolved channel
|
|
||||||
* @property {Role|APIRole} [role] The resolved role
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transforms an option received from the API.
|
|
||||||
* @param {APIApplicationCommandOption} option The received option
|
|
||||||
* @param {APIApplicationCommandOptionResolved} resolved The resolved interaction data
|
|
||||||
* @returns {CommandInteractionOption}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
transformOption(option, resolved) {
|
|
||||||
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, resolved));
|
|
||||||
|
|
||||||
if (resolved) {
|
|
||||||
const user = resolved.users?.[option.value];
|
|
||||||
if (user) result.user = this.client.users._add(user);
|
|
||||||
|
|
||||||
const member = resolved.members?.[option.value];
|
|
||||||
if (member) result.member = this.guild?.members._add({ user, ...member }) ?? member;
|
|
||||||
|
|
||||||
const channel = resolved.channels?.[option.value];
|
|
||||||
if (channel) {
|
|
||||||
result.channel = this.client.channels._add(channel, this.guild, { fromInteraction: true }) ?? channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
const role = resolved.roles?.[option.value];
|
|
||||||
if (role) result.role = this.guild?.roles._add(role) ?? role;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// These are here only for documentation purposes - they are implemented by InteractionResponses
|
|
||||||
/* eslint-disable no-empty-function */
|
|
||||||
deferReply() {}
|
|
||||||
reply() {}
|
|
||||||
fetchReply() {}
|
|
||||||
editReply() {}
|
|
||||||
deleteReply() {}
|
|
||||||
followUp() {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InteractionResponses.applyToClass(CommandInteraction, ['deferUpdate', 'update']);
|
|
||||||
|
|
||||||
module.exports = CommandInteraction;
|
module.exports = CommandInteraction;
|
||||||
|
|
||||||
/* eslint-disable max-len */
|
|
||||||
/**
|
|
||||||
* @external APIApplicationCommandOptionResolved
|
|
||||||
* @see {@link https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondataresolved}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @external APIInteractionDataResolvedOption
|
|
||||||
* @see {@link https://discord.com/developers/docs/interactions/slash-commands#sample-application-command-interaction-application-command-interaction-data-resolved-structure}
|
|
||||||
*/
|
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* Gets a channel option.
|
* Gets a channel option.
|
||||||
* @param {string} name The name of the option.
|
* @param {string} name The name of the option.
|
||||||
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
||||||
* @returns {?(GuildChannel|APIInteractionDataResolvedOption)}
|
* @returns {?(GuildChannel|APIGuildChannel)}
|
||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getChannel(name, required = false) {
|
getChannel(name, required = false) {
|
||||||
@@ -190,7 +190,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* Gets a member option.
|
* Gets a member option.
|
||||||
* @param {string} name The name of the option.
|
* @param {string} name The name of the option.
|
||||||
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
||||||
* @returns {?(GuildMember|APIInteractionDataResolvedOption)}
|
* @returns {?(GuildMember|APIGuildMember)}
|
||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getMember(name, required = false) {
|
getMember(name, required = false) {
|
||||||
@@ -213,13 +213,25 @@ class CommandInteractionOptionResolver {
|
|||||||
* Gets a mentionable option.
|
* Gets a mentionable option.
|
||||||
* @param {string} name The name of the option.
|
* @param {string} name The name of the option.
|
||||||
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
||||||
* @returns {?(User|GuildMember|APIInteractionDataResolvedOption|Role|APIRole)}
|
* @returns {?(User|GuildMember|APIGuildMember|Role|APIRole)}
|
||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getMentionable(name, required = false) {
|
getMentionable(name, required = false) {
|
||||||
const option = this._getTypedOption(name, 'MENTIONABLE', ['user', 'member', 'role'], required);
|
const option = this._getTypedOption(name, 'MENTIONABLE', ['user', 'member', 'role'], required);
|
||||||
return option?.member ?? option?.user ?? option?.role ?? null;
|
return option?.member ?? option?.user ?? option?.role ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a message option.
|
||||||
|
* @param {string} name The name of the option.
|
||||||
|
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
||||||
|
* @returns {?(Message|APIMessage)}
|
||||||
|
* The value of the option, or null if not set and not required.
|
||||||
|
*/
|
||||||
|
getMessage(name, required = false) {
|
||||||
|
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
|
||||||
|
return option?.message ?? null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CommandInteractionOptionResolver;
|
module.exports = CommandInteractionOptionResolver;
|
||||||
|
|||||||
61
src/structures/ContextMenuInteraction.js
Normal file
61
src/structures/ContextMenuInteraction.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const BaseCommandInteraction = require('./BaseCommandInteraction');
|
||||||
|
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
|
||||||
|
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a context menu interaction.
|
||||||
|
* @extends {BaseCommandInteraction}
|
||||||
|
*/
|
||||||
|
class ContextMenuInteraction extends BaseCommandInteraction {
|
||||||
|
constructor(client, data) {
|
||||||
|
super(client, data);
|
||||||
|
/**
|
||||||
|
* The target of the interaction, parsed into options
|
||||||
|
* @type {CommandInteractionOptionResolver}
|
||||||
|
*/
|
||||||
|
this.options = new CommandInteractionOptionResolver(this.client, this.resolveContextMenuOptions(data.data));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The id of the target of the interaction
|
||||||
|
* @type {Snowflake}
|
||||||
|
*/
|
||||||
|
this.targetId = data.data.target_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the target of the interaction; either USER or MESSAGE
|
||||||
|
* @type {ApplicationCommandType}
|
||||||
|
*/
|
||||||
|
this.targetType = ApplicationCommandTypes[data.data.type];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves and transforms options received from the API for a context menu interaction.
|
||||||
|
* @param {APIApplicationCommandInteractionData} data The interaction data
|
||||||
|
* @returns {CommandInteractionOption[]}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
resolveContextMenuOptions({ target_id, resolved }) {
|
||||||
|
const result = [];
|
||||||
|
|
||||||
|
if (resolved.users?.[target_id]) {
|
||||||
|
result.push(
|
||||||
|
this.transformOption({ name: 'user', type: ApplicationCommandOptionTypes.USER, value: target_id }, resolved),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resolved.messages?.[target_id]) {
|
||||||
|
result.push({
|
||||||
|
name: 'message',
|
||||||
|
type: '_MESSAGE',
|
||||||
|
value: target_id,
|
||||||
|
message: this.channel?.messages._add(resolved.messages[target_id]),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ContextMenuInteraction;
|
||||||
@@ -118,7 +118,15 @@ class Interaction extends Base {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
isCommand() {
|
isCommand() {
|
||||||
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND;
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId === 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether this interaction is a {@link ContextMenuInteraction}
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
isContextMenu() {
|
||||||
|
return InteractionTypes[this.type] === InteractionTypes.APPLICATION_COMMAND && typeof this.targetId !== 'undefined';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -887,6 +887,15 @@ exports.StickerFormatTypes = createEnum([null, 'PNG', 'APNG', 'LOTTIE']);
|
|||||||
exports.OverwriteTypes = createEnum(['role', 'member']);
|
exports.OverwriteTypes = createEnum(['role', 'member']);
|
||||||
|
|
||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
|
/**
|
||||||
|
* The type of an {@link ApplicationCommand} object:
|
||||||
|
* * CHAT_INPUT
|
||||||
|
* * USER
|
||||||
|
* * MESSAGE
|
||||||
|
* @typedef {string} ApplicationCommandType
|
||||||
|
*/
|
||||||
|
exports.ApplicationCommandTypes = createEnum([null, 'CHAT_INPUT', 'USER', 'MESSAGE']);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type of an {@link ApplicationCommandOption} object:
|
* The type of an {@link ApplicationCommandOption} object:
|
||||||
* * SUB_COMMAND
|
* * SUB_COMMAND
|
||||||
|
|||||||
6
typings/enums.d.ts
vendored
6
typings/enums.d.ts
vendored
@@ -10,6 +10,12 @@ export enum ActivityTypes {
|
|||||||
COMPETING = 5,
|
COMPETING = 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum ApplicationCommandTypes {
|
||||||
|
CHAT_INPUT = 1,
|
||||||
|
USER = 2,
|
||||||
|
MESSAGE = 3,
|
||||||
|
}
|
||||||
|
|
||||||
export enum ApplicationCommandOptionTypes {
|
export enum ApplicationCommandOptionTypes {
|
||||||
SUB_COMMAND = 1,
|
SUB_COMMAND = 1,
|
||||||
SUB_COMMAND_GROUP = 2,
|
SUB_COMMAND_GROUP = 2,
|
||||||
|
|||||||
85
typings/index.d.ts
vendored
85
typings/index.d.ts
vendored
@@ -21,6 +21,7 @@ import {
|
|||||||
APIApplicationCommand,
|
APIApplicationCommand,
|
||||||
APIApplicationCommandInteractionData,
|
APIApplicationCommandInteractionData,
|
||||||
APIApplicationCommandInteractionDataOption,
|
APIApplicationCommandInteractionDataOption,
|
||||||
|
APIApplicationCommandOption,
|
||||||
APIApplicationCommandPermission,
|
APIApplicationCommandPermission,
|
||||||
APIAuditLogChange,
|
APIAuditLogChange,
|
||||||
APIEmoji,
|
APIEmoji,
|
||||||
@@ -48,6 +49,7 @@ import {
|
|||||||
ActivityTypes,
|
ActivityTypes,
|
||||||
ApplicationCommandOptionTypes,
|
ApplicationCommandOptionTypes,
|
||||||
ApplicationCommandPermissionTypes,
|
ApplicationCommandPermissionTypes,
|
||||||
|
ApplicationCommandTypes,
|
||||||
ChannelTypes,
|
ChannelTypes,
|
||||||
DefaultMessageNotificationLevels,
|
DefaultMessageNotificationLevels,
|
||||||
ExplicitContentFilterLevels,
|
ExplicitContentFilterLevels,
|
||||||
@@ -209,6 +211,7 @@ export class ApplicationCommand<PermissionsFetchType = {}> extends Base {
|
|||||||
Guild | null,
|
Guild | null,
|
||||||
Snowflake
|
Snowflake
|
||||||
>;
|
>;
|
||||||
|
public type: ApplicationCommandType;
|
||||||
public delete(): Promise<ApplicationCommand<PermissionsFetchType>>;
|
public delete(): Promise<ApplicationCommand<PermissionsFetchType>>;
|
||||||
public edit(data: ApplicationCommandData): Promise<ApplicationCommand<PermissionsFetchType>>;
|
public edit(data: ApplicationCommandData): Promise<ApplicationCommand<PermissionsFetchType>>;
|
||||||
private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown;
|
private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown;
|
||||||
@@ -240,6 +243,30 @@ export class BaseClient extends EventEmitter {
|
|||||||
public toJSON(...props: Record<string, boolean | string>[]): unknown;
|
public toJSON(...props: Record<string, boolean | string>[]): unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export abstract class BaseCommandInteraction extends Interaction {
|
||||||
|
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
||||||
|
public readonly channel: TextBasedChannels | null;
|
||||||
|
public channelId: Snowflake;
|
||||||
|
public commandId: Snowflake;
|
||||||
|
public commandName: string;
|
||||||
|
public deferred: boolean;
|
||||||
|
public ephemeral: boolean | null;
|
||||||
|
public replied: boolean;
|
||||||
|
public webhook: InteractionWebhook;
|
||||||
|
public deferReply(options: InteractionDeferReplyOptions & { fetchReply: true }): Promise<Message | APIMessage>;
|
||||||
|
public deferReply(options?: InteractionDeferReplyOptions): Promise<void>;
|
||||||
|
public deleteReply(): Promise<void>;
|
||||||
|
public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<Message | APIMessage>;
|
||||||
|
public fetchReply(): Promise<Message | APIMessage>;
|
||||||
|
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message | APIMessage>;
|
||||||
|
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message | APIMessage>;
|
||||||
|
public reply(options: string | MessagePayload | InteractionReplyOptions): Promise<void>;
|
||||||
|
private transformOption(
|
||||||
|
option: APIApplicationCommandOption,
|
||||||
|
resolved: APIApplicationCommandInteractionData['resolved'],
|
||||||
|
): CommandInteractionOption;
|
||||||
|
}
|
||||||
|
|
||||||
export abstract class BaseGuild extends Base {
|
export abstract class BaseGuild extends Base {
|
||||||
public constructor(client: Client, data: RawBaseGuildData);
|
public constructor(client: Client, data: RawBaseGuildData);
|
||||||
public readonly createdAt: Date;
|
public readonly createdAt: Date;
|
||||||
@@ -494,30 +521,8 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
|
|||||||
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaited<void>): this;
|
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaited<void>): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CommandInteraction extends Interaction {
|
export class CommandInteraction extends BaseCommandInteraction {
|
||||||
public constructor(client: Client, data: RawCommandInteractionData);
|
|
||||||
public readonly command: ApplicationCommand | ApplicationCommand<{ guild: GuildResolvable }> | null;
|
|
||||||
public readonly channel: TextBasedChannels | null;
|
|
||||||
public channelId: Snowflake;
|
|
||||||
public commandId: Snowflake;
|
|
||||||
public commandName: string;
|
|
||||||
public deferred: boolean;
|
|
||||||
public ephemeral: boolean | null;
|
|
||||||
public options: CommandInteractionOptionResolver;
|
public options: CommandInteractionOptionResolver;
|
||||||
public replied: boolean;
|
|
||||||
public webhook: InteractionWebhook;
|
|
||||||
public deferReply(options: InteractionDeferReplyOptions & { fetchReply: true }): Promise<Message | APIMessage>;
|
|
||||||
public deferReply(options?: InteractionDeferReplyOptions): Promise<void>;
|
|
||||||
public deleteReply(): Promise<void>;
|
|
||||||
public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise<Message | APIMessage>;
|
|
||||||
public fetchReply(): Promise<Message | APIMessage>;
|
|
||||||
public followUp(options: string | MessagePayload | InteractionReplyOptions): Promise<Message | APIMessage>;
|
|
||||||
public reply(options: InteractionReplyOptions & { fetchReply: true }): Promise<Message | APIMessage>;
|
|
||||||
public reply(options: string | MessagePayload | InteractionReplyOptions): Promise<void>;
|
|
||||||
private transformOption(
|
|
||||||
option: APIApplicationCommandInteractionDataOption,
|
|
||||||
resolved: APIApplicationCommandInteractionData['resolved'],
|
|
||||||
): CommandInteractionOption;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CommandInteractionOptionResolver {
|
export class CommandInteractionOptionResolver {
|
||||||
@@ -571,6 +576,15 @@ export class CommandInteractionOptionResolver {
|
|||||||
name: string,
|
name: string,
|
||||||
required?: boolean,
|
required?: boolean,
|
||||||
): NonNullable<CommandInteractionOption['member' | 'role' | 'user']> | null;
|
): NonNullable<CommandInteractionOption['member' | 'role' | 'user']> | null;
|
||||||
|
public getMessage(name: string, required: true): NonNullable<CommandInteractionOption['message']>;
|
||||||
|
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption['message']> | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ContextMenuInteraction extends BaseCommandInteraction {
|
||||||
|
public options: CommandInteractionOptionResolver;
|
||||||
|
public targetId: Snowflake;
|
||||||
|
public targetType: Exclude<ApplicationCommandType, 'CHAT_INPUT'>;
|
||||||
|
private resolveContextMenuOptions(data: APIApplicationCommandInteractionData): CommandInteractionOption[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DataResolver extends null {
|
export class DataResolver extends null {
|
||||||
@@ -976,6 +990,7 @@ export class Interaction extends Base {
|
|||||||
public inGuild(): this is this & { guildId: Snowflake; member: GuildMember | APIInteractionGuildMember };
|
public inGuild(): this is this & { guildId: Snowflake; member: GuildMember | APIInteractionGuildMember };
|
||||||
public isButton(): this is ButtonInteraction;
|
public isButton(): this is ButtonInteraction;
|
||||||
public isCommand(): this is CommandInteraction;
|
public isCommand(): this is CommandInteraction;
|
||||||
|
public isContextMenu(): this is ContextMenuInteraction;
|
||||||
public isMessageComponent(): this is MessageComponentInteraction;
|
public isMessageComponent(): this is MessageComponentInteraction;
|
||||||
public isSelectMenu(): this is SelectMenuInteraction;
|
public isSelectMenu(): this is SelectMenuInteraction;
|
||||||
}
|
}
|
||||||
@@ -2256,11 +2271,11 @@ export abstract class CachedManager<K, Holds, R> extends DataManager<K, Holds, R
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ApplicationCommandManager<
|
export class ApplicationCommandManager<
|
||||||
ApplicationCommandType = ApplicationCommand<{ guild: GuildResolvable }>,
|
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
|
||||||
PermissionsOptionsExtras = { guild: GuildResolvable },
|
PermissionsOptionsExtras = { guild: GuildResolvable },
|
||||||
PermissionsGuildType = null,
|
PermissionsGuildType = null,
|
||||||
> extends CachedManager<Snowflake, ApplicationCommandType, ApplicationCommandResolvable> {
|
> extends CachedManager<Snowflake, ApplicationCommandScope, ApplicationCommandResolvable> {
|
||||||
public constructor(client: Client, iterable?: Iterable<RawApplicationCommandData>);
|
public constructor(client: Client, iterable?: Iterable<unknown>);
|
||||||
public permissions: ApplicationCommandPermissionsManager<
|
public permissions: ApplicationCommandPermissionsManager<
|
||||||
{ command?: ApplicationCommandResolvable } & PermissionsOptionsExtras,
|
{ command?: ApplicationCommandResolvable } & PermissionsOptionsExtras,
|
||||||
{ command: ApplicationCommandResolvable } & PermissionsOptionsExtras,
|
{ command: ApplicationCommandResolvable } & PermissionsOptionsExtras,
|
||||||
@@ -2269,10 +2284,10 @@ export class ApplicationCommandManager<
|
|||||||
null
|
null
|
||||||
>;
|
>;
|
||||||
private commandPath({ id, guildId }: { id?: Snowflake; guildId?: Snowflake }): unknown;
|
private commandPath({ id, guildId }: { id?: Snowflake; guildId?: Snowflake }): unknown;
|
||||||
public create(command: ApplicationCommandData): Promise<ApplicationCommandType>;
|
public create(command: ApplicationCommandData): Promise<ApplicationCommandScope>;
|
||||||
public create(command: ApplicationCommandData, guildId: Snowflake): Promise<ApplicationCommand>;
|
public create(command: ApplicationCommandData, guildId: Snowflake): Promise<ApplicationCommand>;
|
||||||
public delete(command: ApplicationCommandResolvable, guildId?: Snowflake): Promise<ApplicationCommandType | null>;
|
public delete(command: ApplicationCommandResolvable, guildId?: Snowflake): Promise<ApplicationCommandScope | null>;
|
||||||
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommandType>;
|
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommandScope>;
|
||||||
public edit(
|
public edit(
|
||||||
command: ApplicationCommandResolvable,
|
command: ApplicationCommandResolvable,
|
||||||
data: ApplicationCommandData,
|
data: ApplicationCommandData,
|
||||||
@@ -2282,12 +2297,12 @@ export class ApplicationCommandManager<
|
|||||||
id: Snowflake,
|
id: Snowflake,
|
||||||
options: FetchApplicationCommandOptions & { guildId: Snowflake },
|
options: FetchApplicationCommandOptions & { guildId: Snowflake },
|
||||||
): Promise<ApplicationCommand>;
|
): Promise<ApplicationCommand>;
|
||||||
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommandType>;
|
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommandScope>;
|
||||||
public fetch(
|
public fetch(
|
||||||
id?: Snowflake,
|
id?: Snowflake,
|
||||||
options?: FetchApplicationCommandOptions,
|
options?: FetchApplicationCommandOptions,
|
||||||
): Promise<Collection<Snowflake, ApplicationCommandType>>;
|
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||||
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommandType>>;
|
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||||
public set(
|
public set(
|
||||||
commands: ApplicationCommandData[],
|
commands: ApplicationCommandData[],
|
||||||
guildId: Snowflake,
|
guildId: Snowflake,
|
||||||
@@ -2868,7 +2883,8 @@ export interface ApplicationAsset {
|
|||||||
|
|
||||||
export interface ApplicationCommandData {
|
export interface ApplicationCommandData {
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description?: string;
|
||||||
|
type?: ApplicationCommandType | ApplicationCommandTypes;
|
||||||
options?: ApplicationCommandOptionData[];
|
options?: ApplicationCommandOptionData[];
|
||||||
defaultPermission?: boolean;
|
defaultPermission?: boolean;
|
||||||
}
|
}
|
||||||
@@ -2892,6 +2908,8 @@ export interface ApplicationCommandOptionChoice {
|
|||||||
value: string | number;
|
value: string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ApplicationCommandType = keyof typeof ApplicationCommandTypes;
|
||||||
|
|
||||||
export type ApplicationCommandOptionType = keyof typeof ApplicationCommandOptionTypes;
|
export type ApplicationCommandOptionType = keyof typeof ApplicationCommandOptionTypes;
|
||||||
|
|
||||||
export interface ApplicationCommandPermissionData {
|
export interface ApplicationCommandPermissionData {
|
||||||
@@ -3234,6 +3252,7 @@ export interface CommandInteractionOption {
|
|||||||
member?: GuildMember | APIInteractionDataResolvedGuildMember;
|
member?: GuildMember | APIInteractionDataResolvedGuildMember;
|
||||||
channel?: GuildChannel | APIInteractionDataResolvedChannel;
|
channel?: GuildChannel | APIInteractionDataResolvedChannel;
|
||||||
role?: Role | APIRole;
|
role?: Role | APIRole;
|
||||||
|
message?: Message | APIMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConstantsClientApplicationAssetTypes {
|
export interface ConstantsClientApplicationAssetTypes {
|
||||||
|
|||||||
@@ -610,17 +610,17 @@ declare const applicationCommandData: ApplicationCommandData;
|
|||||||
declare const applicationCommandResolvable: ApplicationCommandResolvable;
|
declare const applicationCommandResolvable: ApplicationCommandResolvable;
|
||||||
declare const applicationCommandManager: ApplicationCommandManager;
|
declare const applicationCommandManager: ApplicationCommandManager;
|
||||||
{
|
{
|
||||||
type ApplicationCommandType = ApplicationCommand<{ guild: GuildResolvable }>;
|
type ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>;
|
||||||
|
|
||||||
assertType<Promise<ApplicationCommandType>>(applicationCommandManager.create(applicationCommandData));
|
assertType<Promise<ApplicationCommandScope>>(applicationCommandManager.create(applicationCommandData));
|
||||||
assertType<Promise<ApplicationCommand>>(applicationCommandManager.create(applicationCommandData, '0'));
|
assertType<Promise<ApplicationCommand>>(applicationCommandManager.create(applicationCommandData, '0'));
|
||||||
assertType<Promise<ApplicationCommandType>>(
|
assertType<Promise<ApplicationCommandScope>>(
|
||||||
applicationCommandManager.edit(applicationCommandResolvable, applicationCommandData),
|
applicationCommandManager.edit(applicationCommandResolvable, applicationCommandData),
|
||||||
);
|
);
|
||||||
assertType<Promise<ApplicationCommand>>(
|
assertType<Promise<ApplicationCommand>>(
|
||||||
applicationCommandManager.edit(applicationCommandResolvable, applicationCommandData, '0'),
|
applicationCommandManager.edit(applicationCommandResolvable, applicationCommandData, '0'),
|
||||||
);
|
);
|
||||||
assertType<Promise<Collection<Snowflake, ApplicationCommandType>>>(
|
assertType<Promise<Collection<Snowflake, ApplicationCommandScope>>>(
|
||||||
applicationCommandManager.set([applicationCommandData]),
|
applicationCommandManager.set([applicationCommandData]),
|
||||||
);
|
);
|
||||||
assertType<Promise<Collection<Snowflake, ApplicationCommand>>>(
|
assertType<Promise<Collection<Snowflake, ApplicationCommand>>>(
|
||||||
|
|||||||
Reference in New Issue
Block a user