Skip to content

Commit

Permalink
types: fix regressions (#7649)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ImRodry and vladfrangu committed Apr 3, 2022
1 parent ac4bc3a commit 5748dbe
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/builders/src/components/ActionRow.ts
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
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[];

public constructor(data?: Partial<APISelectMenuComponent>) {
const { options, ...initData } = data ?? {};
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/interactions/modals/UnsafeModal.ts
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
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());

let username;
let avatarURL;
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/ModalSubmitInteraction.js
@@ -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
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
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 {
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;
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>)
| 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>)
| APIActionRowComponent<APIMessageActionRowComponent>
)[];
allowedMentions?: MessageMentionOptions;
Expand Down
36 changes: 35 additions & 1 deletion packages/discord.js/typings/index.test-d.ts
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

0 comments on commit 5748dbe

Please sign in to comment.