Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attachment application command option type #7200

Merged
merged 12 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -4,6 +4,7 @@ const { Collection } = require('@discordjs/collection');
const { ApplicationCommandType } = require('discord-api-types/v9');
const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook');
const MessageAttachment = require('./MessageAttachment');
const InteractionResponses = require('./interfaces/InteractionResponses');

/**
Expand Down Expand Up @@ -76,6 +77,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 @@ -84,7 +86,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 @@ -123,6 +125,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 @@ -139,6 +149,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 @@ -169,6 +180,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, 'ATTACHMENT', ['attachment'], required);
amitojsingh366 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -41,6 +41,7 @@ import {
APIPartialEmoji,
APIPartialGuild,
APIRole,
APIAttachment,
APISelectMenuComponent,
APITemplateSerializedSourceGuild,
APIUser,
Expand Down Expand Up @@ -328,6 +329,7 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getAttachment'
| 'getNumber'
| 'getInteger'
| 'getString'
Expand Down Expand Up @@ -703,6 +705,8 @@ export interface ApplicationCommandInteractionOptionResolver<Cached extends Cach
getMember(name: string, required?: boolean): 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 @@ -3395,6 +3399,10 @@ export interface ApplicationCommandChannelOption extends BaseApplicationCommandO
channelTypes?: (keyof typeof ChannelType)[];
}

export interface ApplicationCommandAttachmentOption extends BaseApplicationCommandOptionsData {
type: 'ATTACHMENT';
amitojsingh366 marked this conversation as resolved.
Show resolved Hide resolved
}

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

export interface ApplicationCommandOptionChoice {
Expand Down Expand Up @@ -3869,6 +3878,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
role?: CacheTypeReducer<Cached, Role, APIRole>;
attachment?: CacheTypeReducer<Cached, MessageAttachment>;
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
message?: GuildCacheMessage<Cached>;
}

Expand All @@ -3878,6 +3888,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, CacheTypeReducer<Cached, MessageAttachment>>;
amitojsingh366 marked this conversation as resolved.
Show resolved Hide resolved
}

export interface ConstantsColors {
Expand Down