mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: attachment application command option type (#7200)
This commit is contained in:
@@ -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');
|
||||
|
||||
/**
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
11
packages/discord.js/typings/index.d.ts
vendored
11
packages/discord.js/typings/index.d.ts
vendored
@@ -46,6 +46,7 @@ import {
|
||||
APIPartialEmoji,
|
||||
APIPartialGuild,
|
||||
APIRole,
|
||||
APIAttachment,
|
||||
APISelectMenuComponent,
|
||||
APITemplateSerializedSourceGuild,
|
||||
APIUser,
|
||||
@@ -334,6 +335,7 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
|
||||
| 'getFocused'
|
||||
| 'getMentionable'
|
||||
| 'getRole'
|
||||
| 'getAttachment'
|
||||
| 'getNumber'
|
||||
| 'getInteger'
|
||||
| 'getString'
|
||||
@@ -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,
|
||||
@@ -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
|
||||
@@ -3359,6 +3367,7 @@ export type ApplicationCommandOption =
|
||||
| ApplicationCommandChannelOption
|
||||
| ApplicationCommandChoicesOption
|
||||
| ApplicationCommandNumericOption
|
||||
| ApplicationCommandAttachmentOption
|
||||
| ApplicationCommandSubCommand;
|
||||
|
||||
export interface ApplicationCommandOptionChoice {
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user