ContextMenuInteraction.js
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'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),
this.transformResolved(data.data.resolved),
);
/**
* 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]) ?? resolved.messages[target_id],
});
}
return result;
}
}
module.exports = ContextMenuInteraction;