mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
feat: attachment application command option type (#7200)
This commit is contained in:
@@ -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');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,6 +82,7 @@ class CommandInteraction 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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,7 +91,7 @@ class CommandInteraction 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) {
|
||||||
@@ -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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,6 +156,7 @@ class CommandInteraction 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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -176,6 +187,9 @@ class CommandInteraction 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;
|
||||||
|
|||||||
@@ -216,6 +216,17 @@ class CommandInteractionOptionResolver {
|
|||||||
return option?.role ?? null;
|
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.
|
* Gets a mentionable option.
|
||||||
* @param {string} name The name of the 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,
|
APIPartialEmoji,
|
||||||
APIPartialGuild,
|
APIPartialGuild,
|
||||||
APIRole,
|
APIRole,
|
||||||
|
APIAttachment,
|
||||||
APISelectMenuComponent,
|
APISelectMenuComponent,
|
||||||
APITemplateSerializedSourceGuild,
|
APITemplateSerializedSourceGuild,
|
||||||
APIUser,
|
APIUser,
|
||||||
@@ -334,6 +335,7 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
|
|||||||
| 'getFocused'
|
| 'getFocused'
|
||||||
| 'getMentionable'
|
| 'getMentionable'
|
||||||
| 'getRole'
|
| 'getRole'
|
||||||
|
| 'getAttachment'
|
||||||
| 'getNumber'
|
| 'getNumber'
|
||||||
| 'getInteger'
|
| 'getInteger'
|
||||||
| 'getString'
|
| 'getString'
|
||||||
@@ -740,6 +742,8 @@ export interface ApplicationCommandInteractionOptionResolver<Cached extends Cach
|
|||||||
getMember(name: string): NonNullable<CommandInteractionOption<Cached>['member']> | null;
|
getMember(name: string): NonNullable<CommandInteractionOption<Cached>['member']> | null;
|
||||||
getRole(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['role']>;
|
getRole(name: string, required: true): NonNullable<CommandInteractionOption<Cached>['role']>;
|
||||||
getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption<Cached>['role']> | null;
|
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(
|
getMentionable(
|
||||||
name: string,
|
name: string,
|
||||||
required: true,
|
required: true,
|
||||||
@@ -3276,6 +3280,10 @@ export interface ApplicationCommandChannelOption extends BaseApplicationCommandO
|
|||||||
channelTypes?: ChannelType[];
|
channelTypes?: ChannelType[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandAttachmentOption extends BaseApplicationCommandOptionsData {
|
||||||
|
type: ApplicationCommandOptionType.Attachment;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandAutocompleteOption extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
|
export interface ApplicationCommandAutocompleteOption extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
|
||||||
type:
|
type:
|
||||||
| ApplicationCommandOptionType.String
|
| ApplicationCommandOptionType.String
|
||||||
@@ -3359,6 +3367,7 @@ export type ApplicationCommandOption =
|
|||||||
| ApplicationCommandChannelOption
|
| ApplicationCommandChannelOption
|
||||||
| ApplicationCommandChoicesOption
|
| ApplicationCommandChoicesOption
|
||||||
| ApplicationCommandNumericOption
|
| ApplicationCommandNumericOption
|
||||||
|
| ApplicationCommandAttachmentOption
|
||||||
| ApplicationCommandSubCommand;
|
| ApplicationCommandSubCommand;
|
||||||
|
|
||||||
export interface ApplicationCommandOptionChoice {
|
export interface ApplicationCommandOptionChoice {
|
||||||
@@ -3711,6 +3720,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
|
|||||||
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
|
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
|
||||||
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
|
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
|
||||||
role?: CacheTypeReducer<Cached, Role, APIRole>;
|
role?: CacheTypeReducer<Cached, Role, APIRole>;
|
||||||
|
attachment?: Collection<Snowflake, MessageAttachment>;
|
||||||
message?: GuildCacheMessage<Cached>;
|
message?: GuildCacheMessage<Cached>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3720,6 +3730,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 declare const Colors: {
|
export declare const Colors: {
|
||||||
|
|||||||
Reference in New Issue
Block a user