Skip to content

Commit

Permalink
feat: right-clickybois (context menu support for ApplicationCommand a…
Browse files Browse the repository at this point in the history
…nd CommandInteraction) (#6176)

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 10, 2021
1 parent 779e14e commit 0266f28
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 180 deletions.
19 changes: 17 additions & 2 deletions src/client/actions/InteractionCreate.js
Expand Up @@ -3,8 +3,9 @@
const Action = require('./Action');
const ButtonInteraction = require('../../structures/ButtonInteraction');
const CommandInteraction = require('../../structures/CommandInteraction');
const ContextMenuInteraction = require('../../structures/ContextMenuInteraction');
const SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants');
const { Events, InteractionTypes, MessageComponentTypes, ApplicationCommandTypes } = require('../../util/Constants');

let deprecationEmitted = false;

Expand All @@ -18,7 +19,21 @@ class InteractionCreateAction extends Action {
let InteractionType;
switch (data.type) {
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;
case InteractionTypes.MESSAGE_COMPONENT:
switch (data.data.component_type) {
Expand Down
2 changes: 2 additions & 0 deletions src/managers/ApplicationCommandManager.js
Expand Up @@ -5,6 +5,7 @@ const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermis
const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const ApplicationCommand = require('../structures/ApplicationCommand');
const { ApplicationCommandTypes } = require('../util/Constants');

/**
* Manages API methods for application commands and stores their cache.
Expand Down Expand Up @@ -207,6 +208,7 @@ class ApplicationCommandManager extends CachedManager {
return {
name: command.name,
description: command.description,
type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type],
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
default_permission: command.defaultPermission,
};
Expand Down
9 changes: 8 additions & 1 deletion src/structures/ApplicationCommand.js
Expand Up @@ -2,7 +2,7 @@

const Base = require('./Base');
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
const { ApplicationCommandOptionTypes } = require('../util/Constants');
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');

/**
Expand Down Expand Up @@ -44,6 +44,12 @@ class ApplicationCommand extends Base {
*/
this.permissions = new ApplicationCommandPermissionsManager(this);

/**
* The type of this application command
* @type {ApplicationCommandType}
*/
this.type = ApplicationCommandTypes[data.type];

this._patch(data);
}

Expand Down Expand Up @@ -105,6 +111,7 @@ class ApplicationCommand extends Base {
* @typedef {Object} ApplicationCommandData
* @property {string} name The name 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 {boolean} [defaultPermission] Whether the command is enabled by default when the app is added to a guild
*/
Expand Down
142 changes: 142 additions & 0 deletions src/structures/BaseCommandInteraction.js
@@ -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}
*/
139 changes: 3 additions & 136 deletions src/structures/CommandInteraction.js
@@ -1,51 +1,16 @@
'use strict';

const BaseCommandInteraction = require('./BaseCommandInteraction');
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.
* @extends {Interaction}
* @implements {InteractionResponses}
* @extends {BaseCommandInteraction}
*/
class CommandInteraction extends Interaction {
class CommandInteraction extends BaseCommandInteraction {
constructor(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.
* @type {CommandInteractionOptionResolver}
Expand All @@ -54,105 +19,7 @@ class CommandInteraction extends Interaction {
this.client,
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;

/* 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}
*/

0 comments on commit 0266f28

Please sign in to comment.