Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix regressions #7649

Merged
merged 10 commits into from
Apr 3, 2022
2 changes: 1 addition & 1 deletion packages/builders/src/components/ActionRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ActionRowBuilder<
/**
* The components within this action row
*/
private readonly components: T[];
public readonly components: T[];

public constructor({
components,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class UnsafeSelectMenuBuilder extends ComponentBuilder<
/**
* The options within this select menu
*/
protected readonly options: UnsafeSelectMenuOptionBuilder[];
public readonly options: UnsafeSelectMenuOptionBuilder[];
ImRodry marked this conversation as resolved.
Show resolved Hide resolved

public constructor(data?: Partial<APISelectMenuComponent>) {
const { options, ...initData } = data ?? {};
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/interactions/modals/UnsafeModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import { ActionRowBuilder, createComponentBuilder, JSONEncodable, ModalActionRowComponentBuilder } from '../../index';

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 constructor({ components, ...data }: Partial<APIModalInteractionResponseCallbackData> = {}) {
Expand Down
5 changes: 2 additions & 3 deletions packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { Buffer } = require('node:buffer');
const { isJSONEncodable } = require('@discordjs/builders');
const { MessageFlags } = require('discord-api-types/v10');
const ActionRowBuilder = require('./ActionRowBuilder');
const { RangeError } = require('../errors');
const DataResolver = require('../util/DataResolver');
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
Expand Down Expand Up @@ -131,9 +132,7 @@ class MessagePayload {
}
}

const components = this.options.components?.map(c =>
isJSONEncodable(c) ? c.toJSON() : this.target.client.options.jsonTransformer(c),
);
const components = this.options.components?.map(c => (isJSONEncodable(c) ? c : new ActionRowBuilder(c)).toJSON());
ImRodry marked this conversation as resolved.
Show resolved Hide resolved

let username;
let avatarURL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { createComponent } = require('@discordjs/builders');
const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook');
const ModalSubmitFieldsResolver = require('./ModalSubmitFieldsResolver');
const InteractionResponses = require('./interfaces/InteractionResponses');
const Components = require('../util/Components');

/**
* @typedef {Object} ModalFieldData
Expand Down Expand Up @@ -40,7 +40,7 @@ class ModalSubmitInteraction extends Interaction {
* The components within the modal
* @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
Expand Down
3 changes: 3 additions & 0 deletions packages/discord.js/src/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Components extends null {
return new ButtonComponent(data);
case ComponentType.SelectMenu:
return new SelectMenuComponent(data);
case ComponentType.TextInput:
return new TextInputComponent(data);
default:
throw new Error(`Found unknown component type: ${data.type}`);
}
Expand All @@ -92,3 +94,4 @@ const ActionRow = require('../structures/ActionRow');
const ButtonComponent = require('../structures/ButtonComponent');
const Component = require('../structures/Component');
const SelectMenuComponent = require('../structures/SelectMenuComponent');
const TextInputComponent = require('../structures/TextInputComponent');
14 changes: 7 additions & 7 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export type ActionRowComponentData = MessageActionRowComponentData | ModalAction

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 {
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
components: T[];
}

Expand All @@ -233,7 +235,7 @@ export class ActionRowBuilder<
> extends BuilderActionRow<T> {
constructor(
data?:
| ActionRowData<MessageActionRowComponentData | ModalActionRowComponentData | ActionRowComponent>
| ActionRowData<ActionRowComponentData | ActionRowComponentBuilder>
| (Omit<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>, 'type'> & {
type?: ComponentType.ActionRow;
}),
Expand Down Expand Up @@ -4719,14 +4721,13 @@ export type MessageChannelComponentCollectorOptions<T extends MessageComponentIn
export interface MessageEditOptions {
attachments?: MessageAttachment[];
content?: string | null;
embeds?: (Embed | APIEmbed)[] | null;
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[] | null;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
flags?: BitFieldResolvable<MessageFlagsString, number>;
allowedMentions?: MessageMentionOptions;
components?: (
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
| ActionRow<MessageActionRowComponent>
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponent>)
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponentBuilder>)
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
| APIActionRowComponent<APIMessageActionRowComponent>
)[];
}
Expand Down Expand Up @@ -4767,8 +4768,7 @@ export interface MessageOptions {
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[];
components?: (
| JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>>
| ActionRow<MessageActionRowComponent>
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponent>)
| (Required<BaseComponentData> & ActionRowData<MessageActionRowComponentData | MessageActionRowComponentBuilder>)
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
| APIActionRowComponent<APIMessageActionRowComponent>
)[];
allowedMentions?: MessageMentionOptions;
Expand Down
36 changes: 35 additions & 1 deletion packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ import {
ActionRowBuilder,
ButtonComponent,
SelectMenuComponent,
MessageActionRowComponentBuilder,
InteractionResponseFields,
ThreadChannelType,
Events,
Expand All @@ -118,8 +117,10 @@ import {
TextInputBuilder,
TextInputComponent,
Embed,
MessageActionRowComponentBuilder,
} from '.';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';

// Test type transformation:
declare const serialize: <T>(value: T) => Serialized<T>;
Expand Down Expand Up @@ -727,6 +728,39 @@ client.on('messageCreate', async message => {
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) => {
Expand Down