feat(commands): attachment options (#7441)

This commit is contained in:
Ryan Munro
2022-02-13 22:41:41 +11:00
committed by GitHub
parent 988a51b764
commit 5bcca8b97f
6 changed files with 37 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
const { Collection } = require('@discordjs/collection');
const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook');
const MessageAttachment = require('./MessageAttachment');
const InteractionResponses = require('./interfaces/InteractionResponses');
const { ApplicationCommandOptionTypes } = require('../util/Constants');
@@ -76,6 +77,7 @@ class BaseCommandInteraction extends Interaction {
* @property {Collection<Snowflake, Role|APIRole>} [roles] The resolved roles
* @property {Collection<Snowflake, Channel|APIChannel>} [channels] The resolved channels
* @property {Collection<Snowflake, Message|APIMessage>} [messages] The resolved messages
* @property {Collection<Snowflake, MessageAttachment>} [attachments] The resolved attachments
*/
/**
@@ -84,7 +86,7 @@ class BaseCommandInteraction extends Interaction {
* @returns {CommandInteractionResolvedData}
* @private
*/
transformResolved({ members, users, channels, roles, messages }) {
transformResolved({ members, users, channels, roles, messages, attachments }) {
const result = {};
if (members) {
@@ -123,6 +125,14 @@ class BaseCommandInteraction extends Interaction {
}
}
if (attachments) {
result.attachments = new Collection();
for (const attachment of Object.values(attachments)) {
const patched = new MessageAttachment(attachment.url, attachment.filename, attachment);
result.attachments.set(attachment.id, patched);
}
}
return result;
}
@@ -139,6 +149,7 @@ class BaseCommandInteraction extends Interaction {
* @property {GuildMember|APIGuildMember} [member] The resolved member
* @property {GuildChannel|ThreadChannel|APIChannel} [channel] The resolved channel
* @property {Role|APIRole} [role] The resolved role
* @property {MessageAttachment} [attachment] The resolved attachment
*/
/**
@@ -169,6 +180,9 @@ class BaseCommandInteraction extends Interaction {
const role = resolved.roles?.[option.value];
if (role) result.role = this.guild?.roles._add(role) ?? role;
const attachment = resolved.attachments?.[option.value];
if (attachment) result.attachment = new MessageAttachment(attachment.url, attachment.filename, attachment);
}
return result;

View File

@@ -251,6 +251,17 @@ class CommandInteractionOptionResolver {
if (!focusedOption) throw new TypeError('AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION');
return getFull ? focusedOption : focusedOption.value;
}
/**
* Gets an attachment 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 {?MessageAttachment} The value of the option, or null if not set and not required.
*/
getAttachment(name, required = false) {
const option = this._getTypedOption(name, 'ATTACHMENT', ['attachment'], required);
return option?.attachment ?? null;
}
}
module.exports = CommandInteractionOptionResolver;