mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53: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:
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;
|
||||
Reference in New Issue
Block a user