mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
types: fix regressions (#7649)
* types: fix regressions * fix: make requested changes * Apply suggestions from code review Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> * types: action row data should take builders * types: remove redundant overload * fix(MessagePayload): ensure components are serialized correctly * fix(MessagePayload): don't always create new action row * refactor(UnsafeModalBuilder): make data public * types: use type union Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> * types: fix types and add tests Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -34,7 +34,7 @@ export class ActionRowBuilder<
|
|||||||
/**
|
/**
|
||||||
* The components within this action row
|
* The components within this action row
|
||||||
*/
|
*/
|
||||||
private readonly components: T[];
|
public readonly components: T[];
|
||||||
|
|
||||||
public constructor({
|
public constructor({
|
||||||
components,
|
components,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export class UnsafeSelectMenuBuilder extends ComponentBuilder<
|
|||||||
/**
|
/**
|
||||||
* The options within this select menu
|
* The options within this select menu
|
||||||
*/
|
*/
|
||||||
protected readonly options: UnsafeSelectMenuOptionBuilder[];
|
public readonly options: UnsafeSelectMenuOptionBuilder[];
|
||||||
|
|
||||||
public constructor(data?: Partial<APISelectMenuComponent>) {
|
public constructor(data?: Partial<APISelectMenuComponent>) {
|
||||||
const { options, ...initData } = data ?? {};
|
const { options, ...initData } = data ?? {};
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type {
|
|||||||
import { ActionRowBuilder, createComponentBuilder, JSONEncodable, ModalActionRowComponentBuilder } from '../../index';
|
import { ActionRowBuilder, createComponentBuilder, JSONEncodable, ModalActionRowComponentBuilder } from '../../index';
|
||||||
|
|
||||||
export class UnsafeModalBuilder implements JSONEncodable<APIModalInteractionResponseCallbackData> {
|
export class UnsafeModalBuilder implements JSONEncodable<APIModalInteractionResponseCallbackData> {
|
||||||
protected readonly data: Partial<Omit<APIModalInteractionResponseCallbackData, 'components'>>;
|
public readonly data: Partial<Omit<APIModalInteractionResponseCallbackData, 'components'>>;
|
||||||
public readonly components: ActionRowBuilder<ModalActionRowComponentBuilder>[] = [];
|
public readonly components: ActionRowBuilder<ModalActionRowComponentBuilder>[] = [];
|
||||||
|
|
||||||
public constructor({ components, ...data }: Partial<APIModalInteractionResponseCallbackData> = {}) {
|
public constructor({ components, ...data }: Partial<APIModalInteractionResponseCallbackData> = {}) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
const { Buffer } = require('node:buffer');
|
const { Buffer } = require('node:buffer');
|
||||||
const { isJSONEncodable } = require('@discordjs/builders');
|
const { isJSONEncodable } = require('@discordjs/builders');
|
||||||
const { MessageFlags } = require('discord-api-types/v10');
|
const { MessageFlags } = require('discord-api-types/v10');
|
||||||
|
const ActionRowBuilder = require('./ActionRowBuilder');
|
||||||
const { RangeError } = require('../errors');
|
const { RangeError } = require('../errors');
|
||||||
const DataResolver = require('../util/DataResolver');
|
const DataResolver = require('../util/DataResolver');
|
||||||
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
||||||
@@ -131,9 +132,7 @@ class MessagePayload {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const components = this.options.components?.map(c =>
|
const components = this.options.components?.map(c => (isJSONEncodable(c) ? c : new ActionRowBuilder(c)).toJSON());
|
||||||
isJSONEncodable(c) ? c.toJSON() : this.target.client.options.jsonTransformer(c),
|
|
||||||
);
|
|
||||||
|
|
||||||
let username;
|
let username;
|
||||||
let avatarURL;
|
let avatarURL;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { createComponent } = require('@discordjs/builders');
|
|
||||||
const Interaction = require('./Interaction');
|
const Interaction = require('./Interaction');
|
||||||
const InteractionWebhook = require('./InteractionWebhook');
|
const InteractionWebhook = require('./InteractionWebhook');
|
||||||
const ModalSubmitFieldsResolver = require('./ModalSubmitFieldsResolver');
|
const ModalSubmitFieldsResolver = require('./ModalSubmitFieldsResolver');
|
||||||
const InteractionResponses = require('./interfaces/InteractionResponses');
|
const InteractionResponses = require('./interfaces/InteractionResponses');
|
||||||
|
const Components = require('../util/Components');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} ModalFieldData
|
* @typedef {Object} ModalFieldData
|
||||||
@@ -40,7 +40,7 @@ class ModalSubmitInteraction extends Interaction {
|
|||||||
* The components within the modal
|
* The components within the modal
|
||||||
* @type {ActionRow[]}
|
* @type {ActionRow[]}
|
||||||
*/
|
*/
|
||||||
this.components = data.data.components?.map(c => createComponent(c)) ?? [];
|
this.components = data.data.components?.map(c => Components.createComponent(c)) ?? [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The fields within the modal
|
* The fields within the modal
|
||||||
|
|||||||
@@ -80,6 +80,8 @@ class Components extends null {
|
|||||||
return new ButtonComponent(data);
|
return new ButtonComponent(data);
|
||||||
case ComponentType.SelectMenu:
|
case ComponentType.SelectMenu:
|
||||||
return new SelectMenuComponent(data);
|
return new SelectMenuComponent(data);
|
||||||
|
case ComponentType.TextInput:
|
||||||
|
return new TextInputComponent(data);
|
||||||
default:
|
default:
|
||||||
throw new Error(`Found unknown component type: ${data.type}`);
|
throw new Error(`Found unknown component type: ${data.type}`);
|
||||||
}
|
}
|
||||||
@@ -92,3 +94,4 @@ const ActionRow = require('../structures/ActionRow');
|
|||||||
const ButtonComponent = require('../structures/ButtonComponent');
|
const ButtonComponent = require('../structures/ButtonComponent');
|
||||||
const Component = require('../structures/Component');
|
const Component = require('../structures/Component');
|
||||||
const SelectMenuComponent = require('../structures/SelectMenuComponent');
|
const SelectMenuComponent = require('../structures/SelectMenuComponent');
|
||||||
|
const TextInputComponent = require('../structures/TextInputComponent');
|
||||||
|
|||||||
14
packages/discord.js/typings/index.d.ts
vendored
14
packages/discord.js/typings/index.d.ts
vendored
@@ -222,7 +222,9 @@ export type ActionRowComponentData = MessageActionRowComponentData | ModalAction
|
|||||||
|
|
||||||
export type ActionRowComponent = MessageActionRowComponent | ModalActionRowComponent;
|
export type ActionRowComponent = MessageActionRowComponent | ModalActionRowComponent;
|
||||||
|
|
||||||
export interface ActionRowData<T extends ActionRowComponent | ActionRowComponentData> extends BaseComponentData {
|
export type ActionRowComponentBuilder = MessageActionRowComponentBuilder | ModalActionRowComponentBuilder;
|
||||||
|
|
||||||
|
export interface ActionRowData<T extends ActionRowComponentBuilder | ActionRowComponentData> extends BaseComponentData {
|
||||||
components: T[];
|
components: T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,7 +235,7 @@ export class ActionRowBuilder<
|
|||||||
> extends BuilderActionRow<T> {
|
> extends BuilderActionRow<T> {
|
||||||
constructor(
|
constructor(
|
||||||
data?:
|
data?:
|
||||||
| ActionRowData<MessageActionRowComponentData | ModalActionRowComponentData | ActionRowComponent>
|
| ActionRowData<ActionRowComponentData | ActionRowComponentBuilder>
|
||||||
| (Omit<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>, 'type'> & {
|
| (Omit<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>, 'type'> & {
|
||||||
type?: ComponentType.ActionRow;
|
type?: ComponentType.ActionRow;
|
||||||
}),
|
}),
|
||||||
@@ -4719,14 +4721,13 @@ export type MessageChannelComponentCollectorOptions<T extends MessageComponentIn
|
|||||||
export interface MessageEditOptions {
|
export interface MessageEditOptions {
|
||||||
attachments?: MessageAttachment[];
|
attachments?: MessageAttachment[];
|
||||||
content?: string | null;
|
content?: string | null;
|
||||||
embeds?: (Embed | APIEmbed)[] | null;
|
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[] | null;
|
||||||
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
|
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
|
||||||
flags?: BitFieldResolvable<MessageFlagsString, number>;
|
flags?: BitFieldResolvable<MessageFlagsString, number>;
|
||||||
allowedMentions?: MessageMentionOptions;
|
allowedMentions?: MessageMentionOptions;
|
||||||
components?: (
|
components?: (
|
||||||
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
|
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
|
||||||
| ActionRow<MessageActionRowComponent>
|
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponentBuilder>)
|
||||||
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponent>)
|
|
||||||
| APIActionRowComponent<APIMessageActionRowComponent>
|
| APIActionRowComponent<APIMessageActionRowComponent>
|
||||||
)[];
|
)[];
|
||||||
}
|
}
|
||||||
@@ -4767,8 +4768,7 @@ export interface MessageOptions {
|
|||||||
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[];
|
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[];
|
||||||
components?: (
|
components?: (
|
||||||
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
|
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
|
||||||
| ActionRow<MessageActionRowComponent>
|
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponentBuilder>)
|
||||||
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponent>)
|
|
||||||
| APIActionRowComponent<APIMessageActionRowComponent>
|
| APIActionRowComponent<APIMessageActionRowComponent>
|
||||||
)[];
|
)[];
|
||||||
allowedMentions?: MessageMentionOptions;
|
allowedMentions?: MessageMentionOptions;
|
||||||
|
|||||||
@@ -100,7 +100,6 @@ import {
|
|||||||
ActionRowBuilder,
|
ActionRowBuilder,
|
||||||
ButtonComponent,
|
ButtonComponent,
|
||||||
SelectMenuComponent,
|
SelectMenuComponent,
|
||||||
MessageActionRowComponentBuilder,
|
|
||||||
InteractionResponseFields,
|
InteractionResponseFields,
|
||||||
ThreadChannelType,
|
ThreadChannelType,
|
||||||
Events,
|
Events,
|
||||||
@@ -118,8 +117,10 @@ import {
|
|||||||
TextInputBuilder,
|
TextInputBuilder,
|
||||||
TextInputComponent,
|
TextInputComponent,
|
||||||
Embed,
|
Embed,
|
||||||
|
MessageActionRowComponentBuilder,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||||
|
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
||||||
|
|
||||||
// Test type transformation:
|
// Test type transformation:
|
||||||
declare const serialize: <T>(value: T) => Serialized<T>;
|
declare const serialize: <T>(value: T) => Serialized<T>;
|
||||||
@@ -727,6 +728,39 @@ client.on('messageCreate', async message => {
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Check that both builders and builder data can be sent in messages
|
||||||
|
const row = new ActionRowBuilder<MessageActionRowComponentBuilder>();
|
||||||
|
const buttonsRow = {
|
||||||
|
type: ComponentType.ActionRow,
|
||||||
|
components: [
|
||||||
|
new ButtonBuilder(),
|
||||||
|
new UnsafeButtonBuilder(),
|
||||||
|
{ type: ComponentType.Button, label: 'test', style: ButtonStyle.Primary, customId: 'test' },
|
||||||
|
{
|
||||||
|
type: ComponentType.Button,
|
||||||
|
label: 'another test',
|
||||||
|
style: ButtonStyle.Link as const,
|
||||||
|
url: 'https://discord.js.org',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const selectsRow = {
|
||||||
|
type: ComponentType.ActionRow,
|
||||||
|
components: [
|
||||||
|
new SelectMenuBuilder(),
|
||||||
|
new UnsafeSelectMenuBuilder(),
|
||||||
|
{
|
||||||
|
type: ComponentType.SelectMenu,
|
||||||
|
label: 'select menu',
|
||||||
|
options: [{ label: 'test', value: 'test' }],
|
||||||
|
customId: 'test',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const buildersEmbed = new UnsafeEmbedBuilder();
|
||||||
|
const embedData = { description: 'test', color: 0xff0000 };
|
||||||
|
channel.send({ components: [row, buttonsRow, selectsRow], embeds: [embed, buildersEmbed, embedData] });
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('threadMembersUpdate', (thread, addedMembers, removedMembers) => {
|
client.on('threadMembersUpdate', (thread, addedMembers, removedMembers) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user