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

1
.gitignore vendored
View File

@@ -28,3 +28,4 @@ docs/docs.json
.tmp/ .tmp/
.idea/ .idea/
.DS_Store .DS_Store
.yarn/

View File

@@ -3,6 +3,7 @@
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
const Interaction = require('./Interaction'); const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook'); const InteractionWebhook = require('./InteractionWebhook');
const MessageAttachment = require('./MessageAttachment');
const InteractionResponses = require('./interfaces/InteractionResponses'); const InteractionResponses = require('./interfaces/InteractionResponses');
const { ApplicationCommandOptionTypes } = require('../util/Constants'); 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, Role|APIRole>} [roles] The resolved roles
* @property {Collection<Snowflake, Channel|APIChannel>} [channels] The resolved channels * @property {Collection<Snowflake, Channel|APIChannel>} [channels] The resolved channels
* @property {Collection<Snowflake, Message|APIMessage>} [messages] The resolved messages * @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} * @returns {CommandInteractionResolvedData}
* @private * @private
*/ */
transformResolved({ members, users, channels, roles, messages }) { transformResolved({ members, users, channels, roles, messages, attachments }) {
const result = {}; const result = {};
if (members) { 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; return result;
} }
@@ -139,6 +149,7 @@ class BaseCommandInteraction extends Interaction {
* @property {GuildMember|APIGuildMember} [member] The resolved member * @property {GuildMember|APIGuildMember} [member] The resolved member
* @property {GuildChannel|ThreadChannel|APIChannel} [channel] The resolved channel * @property {GuildChannel|ThreadChannel|APIChannel} [channel] The resolved channel
* @property {Role|APIRole} [role] The resolved role * @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]; const role = resolved.roles?.[option.value];
if (role) result.role = this.guild?.roles._add(role) ?? role; 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; return result;

View File

@@ -251,6 +251,17 @@ class CommandInteractionOptionResolver {
if (!focusedOption) throw new TypeError('AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION'); if (!focusedOption) throw new TypeError('AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION');
return getFull ? focusedOption : focusedOption.value; 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; module.exports = CommandInteractionOptionResolver;

View File

@@ -1040,6 +1040,7 @@ exports.ApplicationCommandOptionTypes = createEnum([
'ROLE', 'ROLE',
'MENTIONABLE', 'MENTIONABLE',
'NUMBER', 'NUMBER',
'ATTACHMENT',
]); ]);
/** /**

1
typings/enums.d.ts vendored
View File

@@ -27,6 +27,7 @@ export const enum ApplicationCommandOptionTypes {
ROLE = 8, ROLE = 8,
MENTIONABLE = 9, MENTIONABLE = 9,
NUMBER = 10, NUMBER = 10,
ATTACHMENT = 11,
} }
export const enum ApplicationCommandPermissionTypes { export const enum ApplicationCommandPermissionTypes {

8
typings/index.d.ts vendored
View File

@@ -335,6 +335,7 @@ export abstract class BaseCommandInteraction<Cached extends CacheType = CacheTyp
| 'getBoolean' | 'getBoolean'
| 'getSubcommandGroup' | 'getSubcommandGroup'
| 'getSubcommand' | 'getSubcommand'
| 'getAttachment'
>; >;
public channelId: Snowflake; public channelId: Snowflake;
public commandId: Snowflake; public commandId: Snowflake;
@@ -798,6 +799,11 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null; public getMessage(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['message']> | null;
public getFocused(getFull: true): ApplicationCommandOptionChoice; public getFocused(getFull: true): ApplicationCommandOptionChoice;
public getFocused(getFull?: boolean): string | number; 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> { export class ContextMenuInteraction<Cached extends CacheType = CacheType> extends BaseCommandInteraction<Cached> {
@@ -4102,6 +4108,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>; channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
role?: CacheTypeReducer<Cached, Role, APIRole>; role?: CacheTypeReducer<Cached, Role, APIRole>;
message?: GuildCacheMessage<Cached>; message?: GuildCacheMessage<Cached>;
attachment?: MessageAttachment;
} }
export interface CommandInteractionResolvedData<Cached extends CacheType = CacheType> { export interface CommandInteractionResolvedData<Cached extends CacheType = CacheType> {
@@ -4110,6 +4117,7 @@ export interface CommandInteractionResolvedData<Cached extends CacheType = Cache
roles?: Collection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>; roles?: Collection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
channels?: Collection<Snowflake, CacheTypeReducer<Cached, AnyChannel, APIInteractionDataResolvedChannel>>; channels?: Collection<Snowflake, CacheTypeReducer<Cached, AnyChannel, APIInteractionDataResolvedChannel>>;
messages?: Collection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>; messages?: Collection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
attachments?: Collection<Snowflake, MessageAttachment>;
} }
export interface ConstantsClientApplicationAssetTypes { export interface ConstantsClientApplicationAssetTypes {