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(commands): attachment options #7441

Merged
merged 1 commit into from Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -28,3 +28,4 @@ docs/docs.json
.tmp/
.idea/
.DS_Store
.yarn/
16 changes: 15 additions & 1 deletion src/structures/BaseCommandInteraction.js
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');
const { ApplicationCommandOptionTypes } = require('../util/Constants');

Expand Down Expand Up @@ -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
*/

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
*/

/**
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/structures/CommandInteractionOptionResolver.js
Expand Up @@ -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;
1 change: 1 addition & 0 deletions src/util/Constants.js
Expand Up @@ -1040,6 +1040,7 @@ exports.ApplicationCommandOptionTypes = createEnum([
'ROLE',
'MENTIONABLE',
'NUMBER',
'ATTACHMENT',
]);

/**
Expand Down
1 change: 1 addition & 0 deletions typings/enums.d.ts
Expand Up @@ -27,6 +27,7 @@ export const enum ApplicationCommandOptionTypes {
ROLE = 8,
MENTIONABLE = 9,
NUMBER = 10,
ATTACHMENT = 11,
}

export const enum ApplicationCommandPermissionTypes {
Expand Down
8 changes: 8 additions & 0 deletions typings/index.d.ts
Expand Up @@ -335,6 +335,7 @@ export abstract class BaseCommandInteraction<Cached extends CacheType = CacheTyp
| 'getBoolean'
| 'getSubcommandGroup'
| 'getSubcommand'
| 'getAttachment'
>;
public channelId: Snowflake;
public commandId: Snowflake;
Expand Down Expand Up @@ -798,6 +799,11 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
public getFocused(getFull: true): ApplicationCommandOptionChoice;
public getFocused(getFull?: boolean): string | number;
public getAttachment(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['attachment']>;
public getAttachment(
name: string,
required?: boolean,
): NonNullable<CommandInteractionOption<Cached>['attachment']> | null;
}

export class ContextMenuInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
Expand Down Expand Up @@ -4102,6 +4108,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
role?: CacheTypeReducer<Cached, Role, APIRole>;
message?: GuildCacheMessage<Cached>;
attachment?: MessageAttachment;
}

export interface CommandInteractionResolvedData<Cached extends CacheType = CacheType> {
Expand All @@ -4110,6 +4117,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 interface ConstantsClientApplicationAssetTypes {
Expand Down