2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-03-24 10:55:32 +00:00
|
|
|
const BaseCommandInteraction = require('./BaseCommandInteraction');
|
2022-03-19 10:37:45 +00:00
|
|
|
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
|
2022-03-24 10:55:32 +00:00
|
|
|
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a context menu interaction.
|
2022-03-24 10:55:32 +00:00
|
|
|
* @extends {BaseCommandInteraction}
|
2022-03-19 10:37:45 +00:00
|
|
|
*/
|
2022-03-24 10:55:32 +00:00
|
|
|
class ContextMenuInteraction extends BaseCommandInteraction {
|
2022-03-19 10:37:45 +00:00
|
|
|
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),
|
|
|
|
this.transformResolved(data.data.resolved),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the target of the interaction
|
|
|
|
* @type {Snowflake}
|
|
|
|
*/
|
|
|
|
this.targetId = data.data.target_id;
|
2022-03-24 10:55:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the target of the interaction; either USER or MESSAGE
|
|
|
|
* @type {ApplicationCommandType}
|
|
|
|
*/
|
|
|
|
this.targetType = ApplicationCommandTypes[data.data.type];
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2022-03-24 10:55:32 +00:00
|
|
|
this.transformOption({ name: 'user', type: ApplicationCommandOptionTypes.USER, value: target_id }, resolved),
|
2022-03-19 10:37:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolved.messages?.[target_id]) {
|
|
|
|
result.push({
|
|
|
|
name: 'message',
|
|
|
|
type: '_MESSAGE',
|
|
|
|
value: target_id,
|
|
|
|
message: this.channel?.messages._add(resolved.messages[target_id]) ?? resolved.messages[target_id],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:55:32 +00:00
|
|
|
module.exports = ContextMenuInteraction;
|