mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: file upload component (#11114)
* feat: file upload component * docs: correct min values * chore: rebase * chore: resolve attachments * chore: typings * chore: whyyyyyyyyy * types: `Snowflake` * types: `Snowflake` * docs: `Snowflake` --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -96,6 +96,7 @@ class CommandInteraction extends BaseInteraction {
|
||||
* @property {Collection<Snowflake, GuildMember|APIGuildMember>} [members] The resolved guild members
|
||||
* @property {Collection<Snowflake, Role|APIRole>} [roles] The resolved roles
|
||||
* @property {Collection<Snowflake, BaseChannel|APIChannel>} [channels] The resolved channels
|
||||
* @property {Collection<Snowflake, Attachment>} [attachments] The resolved attachments
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -103,7 +104,6 @@ class CommandInteraction extends BaseInteraction {
|
||||
*
|
||||
* @typedef {BaseInteractionResolvedData} CommandInteractionResolvedData
|
||||
* @property {Collection<Snowflake, Message|APIMessage>} [messages] The resolved messages
|
||||
* @property {Collection<Snowflake, Attachment>} [attachments] The resolved attachments
|
||||
*/
|
||||
|
||||
/**
|
||||
|
||||
@@ -222,6 +222,17 @@ class ModalComponentResolver {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets file upload component
|
||||
*
|
||||
* @param {string} customId The custom id of the component
|
||||
* @param {boolean} [required=false] Whether to throw an error if the component value is not found or empty
|
||||
* @returns {?Collection<Snowflake, Attachment>} The uploaded files, or null if none were uploaded and not required
|
||||
*/
|
||||
getUploadedFiles(customId, required = false) {
|
||||
return this._getTypedComponent(customId, [ComponentType.FileUpload], ['attachments'], required).attachments ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
exports.ModalComponentResolver = ModalComponentResolver;
|
||||
|
||||
@@ -9,6 +9,7 @@ const { ModalComponentResolver } = require('./ModalComponentResolver.js');
|
||||
const { InteractionResponses } = require('./interfaces/InteractionResponses.js');
|
||||
|
||||
const getMessage = lazy(() => require('./Message.js').Message);
|
||||
const getAttachment = lazy(() => require('./Attachment.js').Attachment);
|
||||
|
||||
/**
|
||||
* @typedef {Object} BaseModalData
|
||||
@@ -26,6 +27,13 @@ const getMessage = lazy(() => require('./Message.js').Message);
|
||||
* @property {Collection<string, BaseChannel|APIChannel>} [channels] The resolved channels
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {BaseModalData} FileUploadModalData
|
||||
* @property {string} customId The custom id of the file upload
|
||||
* @property {string[]} values The values of the file upload
|
||||
* @property {Collection<string, Attachment>} [attachments] The resolved attachments
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {BaseModalData} TextInputModalData
|
||||
* @property {string} customId The custom id of the component
|
||||
@@ -37,7 +45,7 @@ const getMessage = lazy(() => require('./Message.js').Message);
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {SelectMenuModalData|TextInputModalData} ModalData
|
||||
* @typedef {SelectMenuModalData|TextInputModalData|FileUploadModalData} ModalData
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -155,7 +163,7 @@ class ModalSubmitInteraction extends BaseInteraction {
|
||||
if (rawComponent.values) {
|
||||
data.values = rawComponent.values;
|
||||
if (resolved) {
|
||||
const { members, users, channels, roles } = resolved;
|
||||
const { members, users, channels, roles, attachments } = resolved;
|
||||
const valueSet = new Set(rawComponent.values);
|
||||
|
||||
if (users) {
|
||||
@@ -198,6 +206,15 @@ class ModalSubmitInteraction extends BaseInteraction {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (attachments) {
|
||||
data.attachments = new Collection();
|
||||
for (const [id, attachment] of Object.entries(attachments)) {
|
||||
if (valueSet.has(id)) {
|
||||
data.attachments.set(id, new (getAttachment())(attachment));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ const { ComponentType } = require('discord-api-types/v10');
|
||||
|
||||
/**
|
||||
* @typedef {StringSelectMenuComponentData|TextInputComponentData|UserSelectMenuComponentData|
|
||||
* RoleSelectMenuComponentData|MentionableSelectMenuComponentData|ChannelSelectMenuComponentData} ComponentInLabelData
|
||||
* RoleSelectMenuComponentData|MentionableSelectMenuComponentData|ChannelSelectMenuComponentData|FileUploadComponentData} ComponentInLabelData
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -44,6 +44,14 @@ const { ComponentType } = require('discord-api-types/v10');
|
||||
* @property {string} [url] The URL of the button
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {BaseComponentData} FileUploadComponentData
|
||||
* @property {string} customId The custom id of the file upload
|
||||
* @property {number} [minValues] The minimum number of files that can be uploaded (0-10)
|
||||
* @property {number} [maxValues] The maximum number of files that can be uploaded (1-10)
|
||||
* @property {boolean} [required] Whether this component is required in modals
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {BaseComponentData} BaseSelectMenuComponentData
|
||||
* @property {string} customId The custom id of the select menu
|
||||
|
||||
21
packages/discord.js/typings/index.d.ts
vendored
21
packages/discord.js/typings/index.d.ts
vendored
@@ -274,11 +274,13 @@ export interface ActionRowData<ComponentType extends ActionRowComponentData | JS
|
||||
|
||||
export type ComponentInLabelData =
|
||||
| ChannelSelectMenuComponentData
|
||||
| FileUploadComponentData
|
||||
| MentionableSelectMenuComponentData
|
||||
| RoleSelectMenuComponentData
|
||||
| StringSelectMenuComponentData
|
||||
| TextInputComponentData
|
||||
| UserSelectMenuComponentData;
|
||||
|
||||
export interface LabelData extends BaseComponentData {
|
||||
component: ComponentInLabelData;
|
||||
description?: string;
|
||||
@@ -2580,7 +2582,12 @@ export interface SelectMenuModalData<Cached extends CacheType = CacheType>
|
||||
values: readonly string[];
|
||||
}
|
||||
|
||||
export type ModalData = SelectMenuModalData | TextInputModalData;
|
||||
export interface FileUploadModalData extends BaseModalData<ComponentType.FileUpload> {
|
||||
customId: string;
|
||||
files: readonly Attachment[];
|
||||
}
|
||||
|
||||
export type ModalData = FileUploadModalData | SelectMenuModalData | TextInputModalData;
|
||||
|
||||
export interface LabelModalData extends BaseModalData<ComponentType.Label> {
|
||||
component: readonly ModalData[];
|
||||
@@ -2653,6 +2660,8 @@ export class ModalComponentResolver<Cached extends CacheType = CacheType> {
|
||||
|
||||
public getSelectedMentionables(customId: string, required: true): ModalSelectedMentionables<Cached>;
|
||||
public getSelectedMentionables(customId: string, required?: boolean): ModalSelectedMentionables<Cached> | null;
|
||||
public getUploadedFiles(customId: string, required: true): ReadonlyCollection<Snowflake, Attachment>;
|
||||
public getUploadedFiles(customId: string, required?: boolean): ReadonlyCollection<Snowflake, Attachment> | null;
|
||||
}
|
||||
|
||||
export interface ModalMessageModalSubmitInteraction<Cached extends CacheType = CacheType>
|
||||
@@ -5621,6 +5630,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
|
||||
}
|
||||
|
||||
export interface BaseInteractionResolvedData<Cached extends CacheType = CacheType> {
|
||||
attachments?: ReadonlyCollection<Snowflake, Attachment>;
|
||||
channels?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Channel, APIInteractionDataResolvedChannel>>;
|
||||
members?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>>;
|
||||
roles?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
|
||||
@@ -5629,7 +5639,6 @@ export interface BaseInteractionResolvedData<Cached extends CacheType = CacheTyp
|
||||
|
||||
export interface CommandInteractionResolvedData<Cached extends CacheType = CacheType>
|
||||
extends BaseInteractionResolvedData<Cached> {
|
||||
attachments?: ReadonlyCollection<Snowflake, Attachment>;
|
||||
messages?: ReadonlyCollection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
|
||||
}
|
||||
|
||||
@@ -6838,6 +6847,14 @@ export interface TextInputComponentData extends BaseComponentData {
|
||||
value?: string;
|
||||
}
|
||||
|
||||
export interface FileUploadComponentData extends BaseComponentData {
|
||||
customId: string;
|
||||
maxValues?: number;
|
||||
minValues?: number;
|
||||
required?: number;
|
||||
type: ComponentType.FileUpload;
|
||||
}
|
||||
|
||||
export type MessageTarget =
|
||||
| ChannelManager
|
||||
| Interaction
|
||||
|
||||
Reference in New Issue
Block a user