Skip to content

Commit

Permalink
feat: attachment application command option type (#7200)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitojsingh366 committed Feb 14, 2022
1 parent 0dfdb2c commit 0034396
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/discord.js/src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -81,6 +82,7 @@ class CommandInteraction 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
*/

/**
Expand All @@ -89,7 +91,7 @@ class CommandInteraction extends Interaction {
* @returns {CommandInteractionResolvedData}
* @private
*/
transformResolved({ members, users, channels, roles, messages }) {
transformResolved({ members, users, channels, roles, messages, attachments }) {
const result = {};

if (members) {
Expand Down Expand Up @@ -128,6 +130,14 @@ class CommandInteraction 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;
}

Expand All @@ -146,6 +156,7 @@ class CommandInteraction 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
*/

/**
Expand Down Expand Up @@ -176,6 +187,9 @@ class CommandInteraction 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ class CommandInteractionOptionResolver {
return option?.role ?? null;
}

/**
* 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, ApplicationCommandOptionType.Attachment, ['attachment'], required);
return option?.attachment ?? null;
}

/**
* Gets a mentionable option.
* @param {string} name The name of the option.
Expand Down
11 changes: 11 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
APIPartialEmoji,
APIPartialGuild,
APIRole,
APIAttachment,
APISelectMenuComponent,
APITemplateSerializedSourceGuild,
APIUser,
Expand Down Expand Up @@ -334,6 +335,7 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getAttachment'
| 'getNumber'
| 'getInteger'
| 'getString'
Expand Down Expand Up @@ -740,6 +742,8 @@ export interface ApplicationCommandInteractionOptionResolver<Cached extends Cach
getMember(name: string): NonNullable<CommandInteractionOption<Cached>['member']> | null;
getRole(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['role']>;
getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['role']> | null;
getAttachment(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['attachment']>;
getAttachment(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['attachment']> | null;
getMentionable(
name: string,
required: true,
Expand Down Expand Up @@ -3276,6 +3280,10 @@ export interface ApplicationCommandChannelOption extends BaseApplicationCommandO
channelTypes?: ChannelType[];
}

export interface ApplicationCommandAttachmentOption extends BaseApplicationCommandOptionsData {
type: ApplicationCommandOptionType.Attachment;
}

export interface ApplicationCommandAutocompleteOption extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type:
| ApplicationCommandOptionType.String
Expand Down Expand Up @@ -3359,6 +3367,7 @@ export type ApplicationCommandOption =
| ApplicationCommandChannelOption
| ApplicationCommandChoicesOption
| ApplicationCommandNumericOption
| ApplicationCommandAttachmentOption
| ApplicationCommandSubCommand;

export interface ApplicationCommandOptionChoice {
Expand Down Expand Up @@ -3711,6 +3720,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
role?: CacheTypeReducer<Cached, Role, APIRole>;
attachment?: Collection<Snowflake, MessageAttachment>;
message?: GuildCacheMessage<Cached>;
}

Expand All @@ -3720,6 +3730,7 @@ export interface CommandInteractionResolvedData<Cached extends CacheType = Cache
roles?: Collection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
channels?: Collection<Snowflake, CacheTypeReducer<Cached, AnyChannel, APIInteractionDataResolvedChannel>>;
messages?: Collection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
attachments?: Collection<Snowflake, MessageAttachment>;
}

export declare const Colors: {
Expand Down

0 comments on commit 0034396

Please sign in to comment.